Typescript error Property 'user' does not exist on type 'Request<Paramsdictionary, any, any, Parsedqs>'

Asked

Viewed 2,360 times

-2

How can I fix the error

Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>

I created a @types folder at the root, inside it had a file express.d.ts with the following code

declare namespace Express {
    export interface Request {
      user: {
        id: string;
      };
    }
  }

when I type request. already gives me the request.user option but when I run the error code just on that line.

5 answers

2

Hello, as it was not mentioned I suggest that you are using a newer version of Typescript, if this is the case the problem can be solved as follows:

- raiz
  - @types
    - express
      - index.d.ts

Above is just a suggestion on how to organize folders and should not influence the functionality of the code below:

declare global {
    namespace Express {
        interface Request{
            user: { ... }
        }
    }
}

The above version is tested and works with version 4.0.

0

Dude, I had the same problem as you, by researching here I found the solution: in tsconfig.json use this:

"typeRoots": [
  "./src/@types",
  "./custom_typings",
  "./@types",
  "./node_modules/@types"
]

at the root of your project create a folder called custom_typings, within it create a folder called express, within the express folder create index.d.ts and enter the following code:

declare namespace Express {
  interface Request {
    user: {
      id: string;
    };
  }
}

save and run the project.. It’s gone!

-1

Create a file .d.ts and add the following code:

import * as http from 'http';

declare module 'express-serve-static-core' {
  export interface Request extends http.IncomingMessage, Express.Request {
    userId: number;
  }
}

-2

In your package.json in scripts leave it as follows: "dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules src/server.ts"

-2

I was doing Bootcamp like you, it seems by the question,

It was 2 simple steps.

1- Inside tsConfig.json I added:

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

2- I changed the directory organization to @types/express and the file inside it called index.d.ts

Ai was good :D Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.