I want to create custom express.Request type that attaches logger to the request.
I don't want to override the original express.Request express instead create a new one that extends it, as some parts of the app have other middleware that attaches other custom properties to the request.
So in a global.d.ts I define the extended Request interface.
import { NextFunction, Request, Response } from "express";
import { Logger } from "pino";
declare global {
interface AppRequest extends Request {
log: Logger;
}
interface AppResponse extends Response {}
interface AppNextFunction extends NextFunction {}
}
Then in some-app-router.ts:
import { Router } from "express";
const router = Router();
function validateSecretMiddleWare(req: AppRequest, res: AppResponse, next: AppNextFunction):void {
if (process.env.STATS_SECRET !== req.query.secret) {
res.sendStatus(404);
return;
}
next();
}
router.use(validateSecretMiddleWare);
export default router;
But I receive this error:
Argument of type '(req: AppRequest, res: AppResponse, next: AppNextFunction) => void' is not assignable to parameter of type 'PathParams'. Type '(req: AppRequest, res: AppResponse, next: AppNextFunction) => void' is missing the following properties from type '(string | RegExp)[]': pop, push, concat, join, and 25 more.
I inspected express.RouterHandler and saw that it uses express.RequestHandlerParams, that uses Request, Response, NextFunction types, doesn't my AppRequest as valid express.Request?