Isak Ågren Isak Ågren - 8 months ago 194
Node.js Question

Extend Express Request object using Typescript

I’m trying to add a property to express request object from a middleware using typescript. However I can’t figure out how to add extra properties to the object. I’d prefer to not use bracket notation if possible.

I’m looking for a solution that would allow me to write something similar to this (if possible):

app.use((req, res, next) => { = setProperty();


In TypeScript interfaces are open ended. That means you can add properties to them from anywhere just by redefining them.

Considering that you are using this express.d.ts file you should be able to redefine the Request interface to add the extra field.

interface Request {
  property: string

Then in your middleware function the res parameter should have this property as well. You should be able to use it without any changes to your code.