I am currently using typescript along with express and attempting to enhance the request object in express.
Below is my server.ts file:
import express, { Request, Response } from "express";
const app = express();
app.use(function(req: Request, res: Response, next) {
req.customProperty = 200;
});
And here is my extends.d.ts file:
declare namespace Express {
export interface Request {
customProperty: number;
}
}
While developing, I am utilizing VSCode. The IDE does not show any warnings.
Compiling the code using tsc
also works without any issues.
However, when running the code using ts-node
, an error message is encountered:
src/server.ts:19:9 - error TS2339: Property 'customProperty' does not exist on type 'Request<ParamsDictionary>'.
19 req.customProperty = 200;
~~~~~~~~~~~