DEV Community

Kwabena Bio Berko
Kwabena Bio Berko

Posted on Edited on

Extend Express's Request Object with Typescript Declaration Merging.

Express has been my go to server side node web framework for the past few years. Its fast, unopinionated and so easy to get up and running. I really love using it along with Typescript too, I should say. It enhances code quality and understand-ability. Refactoring your code in Typescript is also much more easier and faster. Plus, you get the added advantage of code completion and IntelliSense when using modern text editors like Visual Studio Code. ๐Ÿ˜‹

One of the concepts of Typescript which I've recently began using is Declaration Merging.
Declaration Merging allows you to merge two or more distinct declaration or types declared with the same name into a single definition. This concept allows you to attach your own custom property onto another Typescript interface type. Lets take a look at a typical Express middleware.

Alt Text

The above code is an Express middleware that is used to ensure that a user is authenticated when he or she tries to access a protected resource. It decodes the user's token from the authorization property of the request headers and attaches the user to the Request object. But see that red squiggly line?
Thats because the property currentUser does not exist on Express's Request interface type. Let's fix that. ๐Ÿ˜ƒ

The first thing we need to do is to create a new declaration file @types > express > index.d.ts in the root of our project.

You would notice this is the exact same file name and path in our node_modules/@types folder. For Typescript declaration merging to work, the file name and its path must match the original declaration file and path.

Next we need to make some few changes in the project's tsconfig.json file. Let's update the typeRoots value to the following:

...
"typeRoots": [
      "@types",
      "./node_modules/@types",
    ]                    
...
๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026 ๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026

By default, the Typescript compiler looks for type definitions in the node_modules/@types folder. The above code instructs the compiler to look for type definitions in this folder as well as our custom @types folder in our project root.

It's now time to add our custom currentUser property to Express's Request interface type by modifying the index.d.ts file we created earlier:


import { UserModel } from "../../src/user/user.model";

declare global{
    namespace Express {
        interface Request {
            currentUser: UserModel
        }
    }
}

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026 ๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026

Lets take a look again at our middleware file and we immediately notice that the red squiggly line is gone! This is because the Typescript compiler now recognizes the currentUser property as a valid property on the Request type interface.

Alt Text

Happy Coding, everyone!

Top comments (44)

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
aashishclikfin profile image
aashishClikFin • • Edited

First of all thanks a lot for your post, I finally resolved the problem I faced since the last 3 months.

Just would like to add one more bit, since I was using passport alongside passport-jwt hence your solution didn't exactly work for me.

Here is what I used in the index.d.ts file

declare module 'express-serve-static-core' {
export interface Request {
user?: yourCustomType;
}
}

It might help someone else as well, but thanks a lot for guiding me in the right direction.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
itsjinendrajain profile image
Jinendra Jain •

Thanks Bro

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
justicebringer profile image
Gabriel •

NOTE that you have to add typeRoots inside compilerOptions in the tsconfig.json file.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
alphakennybudy profile image
Adil • • Edited

Thank you, it helped. But as far as I understood, it adds currentUser field to all Requests? What if I want it to be present only in the current file and not declared in the other files where I use Request?

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
lucamartins profile image
Luca S. Martins •

You could declare currentUser as optional and use it when needed

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
playground profile image
playground •

Great article, thanks for sharing. Good to know there is another way of doing things.
I have been doing it the inverse by extending Request

For example:
export interface IRequest extends Request {
type: string;
sensorId: string;
timestamp: ITime;
}

in the server.ts
...
app.get("/test", (req: IRequest, res: IResponse) => {
...
})

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
vkyrychenko profile image
Vitalii •

Nice article! You saved my day ๐Ÿ˜Ž๐Ÿ‘

I see a lot of libraries using this approach.

But It looks a bit strange for me. Just imagine that you have a lot of middleware that attaches something to the request object. In the end, you have a bloated interface and you lose the power of TypeScript because you don't know which middlewares were executed before handler/controller. So you don't know if the required data was attached and need to double-check if it's there ๐Ÿคทโ€โ™‚๏ธ.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
peterkitonga profile image
Peter Kitonga • • Edited

If anyone is still having errors with this, don't forget to import Express at the top of your file.

import { Express } from 'express'

For some reason, I couldn't get it to work without the import

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
lucamartins profile image
Luca S. Martins •

Thanks! Same problem here.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
tronginc profile image
Nguyแป…n Cรดng Trแปng •

Thanks!

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
pendar747 profile image
Pendar •

This was awesome! I was missing the step with having to create the same folder path structure and adding the "typeRoots"

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
danfmaia profile image
Dan Maia •

The best article about that I've found today in the Internet.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
tjsarkka profile image
TJSarkka •

Thank you! I spent the last hours scratching my head and trying different combinations of config files and .d.ts files provided in other solutions.. and this was the only one that worked for me for both ts-node and tsc. :)

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
kwabenberko profile image
Kwabena Bio Berko •

You're welcome.
Glad to be of help.

๐Ÿ”ด Live TV: kwabenberko/extend express s request object with typescript declaration merging nn - Info Live Streaming 2026
 
kira56 profile image
Diego CS •

Thanks!!!!!!!! I was looking for information, but all that I found was not useful, but your explanation goes straight to the point. Than you very much.