Property '' does not exist on type 'Request<ParamsDictionary>'
Solution 1
Since a recent update of its typings and dependencies, I found that the following should fix the errors in your application.
In your tsconfig.json
{
"compilerOptions": {
//...
"typeRoots": [
"./custom_typings",
"./node_modules/@types"
],
}
// ...
}
And in your custom typings
// custom_typings/express/index.d.ts
declare namespace Express {
interface Request {
customProperties: string[];
}
}
Solution 2
Just add the following, what this does is it simply adds a custom property to the express Request interface
declare global {
namespace Express {
interface Request {
propertyName: string; //or can be anythin
}
}
}
Solution 3
I recently had the same issue, I followed the solution in the previous comments and this repo and I still had the same issue. After doing more digging it seems like it's a bug with ts-node.
To solve this you need to run your server with a --files flag
So if you normally run your server
ts-node ./src/server.ts
or nodemon ./src/server.ts
Change it to
ts-node --files ./src/server.ts
or nodemon --files ./src/server.ts
After that, I was able to get rid of both the VScode errors and errors while starting the server.
oktapodia
Technical architect - Fullstack web developer / Blockchain developer (Contractor)
Updated on June 20, 2022Comments
-
oktapodia almost 2 years
When trying to extend the
Request
interface from the packageexpress
to add some custom properties, I'm getting the following typescript error:TS2339: Property '' does not exist on type 'Request<ParamsDictionary>'.
Do you know how to solve that?
-
Rohit Ambre over 3 yearsyeah, it does fix but when just created
index.d.ts
file on root folder my VScode stopped showing error but I was still getting error on terminal, so I did as you described but directly createdindex.d.ts
file insidecustom_typings
and still it was giving error, it went after creatingexpress
folder, so why is thatexpress
folder mandatory ? -
oktapodia over 3 yearsYou can either create a directory named
express
or having a file namedexpress.d.ts
to tell typescript that you are aiming this specific module -
danish.ahmad over 3 years@RohitAmbre did you figure out how to fix it. Vscode seems to have no issues but compiler showing some errors.
-
Rohit Ambre over 3 years@danie_man, I did like what I have explained in my comment and it works without any compiler issue you can check here
-
Tank12 over 3 yearsThank you for this, could you explain how this works a bit more? I've never seen someone use declare global