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 }
}- ✓Correct answer: Module augmentation via
declare moduleand merging - 2Edit the declarations directly in
node_modules/@types/express - 3Create your own
Requesttype and override the import viapaths
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.