You need to make a Declaration Merging:
"Declaration merging means that the compiler merges two separate
declarations declared with the same name into a single definition."
To do that you can creat a file called type.d.ts at your project src folder (or wherever you want) with the following content:
declare namespace Express {
export interface Request {
user: any;
}
export interface Response {
user: any;
}
}
Here we are telling the compiler to add user propertie to our Request and Response definiton.
Next, we need to attach this to our tsconfig.json.
Example:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./lib"
},
"include": [
"lib/**/*.ts"
],
"exclude": [
"node_modules"
],
"files":["types.d.ts"]
}
Now, the typescript compiler know that Request, has a propertie called user that in my case can accept any json object. You can restrict the type for string if you want.
(req: any, res: any) => ...
otherwise you might need to use some type info for express – Ovidiu Dolha