Home custom Express request object in typescript
Post
Cancel

custom Express request object in typescript

To add a custom request head for deserialize user in authentication

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

To fix the error,two things needs to be done

  1. add type roots in tsconfig.json
1
2
3
4
5
// tsconfig.json
"typeRoots": [
      "./typings",
      "./node_modules/@types"
    ] /* List of folders to include type definitions from. */,
  1. add the custom type here
1
2
3
4
5
6
7
8
// typings/express/index.d.ts
declare namespace Express {
  interface Request {
    customTypes: {
      // add the custom types here
    };
  }
}

more infos refers to the link

This post is licensed under CC BY 4.0 by the author.