Typescript override

Asked

Viewed 375 times

1

I want to add a property user for the Express request. For this I created a file called express.d.ts with the following content:

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

While trying to access in my middleware the property request user. created, I have the following mistake:

- error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

Middleware class code class name.ts:

export default function methodName(request: Request, response: Response, next: NextFunction): void{
    request.user = {
        id: 10,
    }
}

Folder structure:

src
     @types
          express.d.ts
     middlewares
          className.ts

2 answers

1

Try to do so in the express declaration file you created:

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

putting as optional

-4

Add this line in your package.json scripts

"dev:server": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules src/server.ts",

Browser other questions tagged

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