Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property '' does not exist on type 'Request<ParamsDictionary>'

When trying to extend the Request interface from the package express 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?

like image 859
oktapodia Avatar asked Nov 20 '19 14:11

oktapodia


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Does not exist on type request?

The "Property does not exist on type Request" error occurs when we access a property that does not exist in the Request interface. To solve the error, extend the Request interface in a . d. ts file adding the property you intend to access on the request object.

What is req user in Express?

You generally use the req. body object to receive data through POST and PUT requests in the Express server. In your index.js file, set a POST request to the route '/login' : // POST https://example.com/login // // { // "email": "[email protected]", // "password": "helloworld" // } app.


3 Answers

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.

like image 138
Shahar Sharron Avatar answered Oct 10 '22 05:10

Shahar Sharron


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[];
    }
}
like image 20
oktapodia Avatar answered Oct 10 '22 04:10

oktapodia


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
    }
  }
}
like image 24
Rishav Sinha Avatar answered Oct 10 '22 06:10

Rishav Sinha