This answer will be beneficial to those who rely on npm package ts-node
.
I was also struggling with the same concern of extending request object, I followed a lot of answers in stack-overflow & ended with following the below-mentioned strategy.
I declared extended typing for express in the following directory. ${PROJECT_ROOT}/api/@types/express/index.d.ts
declare namespace Express {
interface Request {
decoded?: any;
}
}
then updating my tsconfig.json
to something like this.
{
"compilerOptions": {
"typeRoots": ["api/@types", "node_modules/@types"]
...
}
}
even after doing the above steps, the visual studio stopped complaining, but unfortunately, the ts-node
compiler still used to throw.
Property 'decoded' does not exist on type 'Request'.
Apparently, the ts-node
was not able to locate the extended type definitions for request object.
Eventually after spending hours, as I knew the VS Code was not complaining & was able to locate the typing definitions, implying something is wrong with ts-node
complier.
Updating start script
in package.json
fixed it for me.
"start": "ts-node --files api/index.ts",
the --files
arguments play a key role here find determining the custom type definitions.
For further information please visit: https://github.com/TypeStrong/ts-node#help-my-types-are-missing