TypeScript · Senior · Question 15 of 75

How do you add a user field to the Express library's Request type?

declare module 'express-serve-static-core' {
  interface Request { user?: User }
}

Explanation

Augmentation lets you extend a third-party module's types without forking it: the interfaces merge. The file must be a module (contain import/export) and be part of the project. Edits in node_modules are lost on reinstall.

Further reading

Problem with a question

TypeScript · Senior · question 15

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz