Linked Questions

Popular Questions

I'm passing some object as helpers to my express app. I attach them to the app instance:

app.set('logger', logger); // logger has Logger type

Having that allows me to access a particular helper while handling some request:

const logger = req.app.get('logger') as Logger;
// or
const logger: Logger = req.app.get('logger');

What I want to achieve is having generic Application.get method exposed by req.app object. The code I'd like to start using looks like this:

const logger = req.app.get<Logger>('logger');

I created @types/express.d.ts file in project root directory, below its contents:

import * as express from 'express';

declare module 'express' {
  export interface Application {
    get<T>(name: string): T;
  }
}

My tsconfig.json covers the directory I've put the typings extension in:

"typeRoots": ["node_modules/@types", "@types"],

Even though I declared the typings extension TS still sees only @types/express definition:

(property) Application.get: (name: string) => any (+3 overloads)

I expect it to also cover the definition I created.

Related Questions