'How to modify `request.params.id` type with declaration merging in Typescript/Express?
I've successfully added currentUser to Express' Request interface:
declare global {
namespace Express {
export interface Request {
currentUser?: User;
}
}
}
However, I also want to specify that the Request's path param req.params.id (where req is of type Express.Request) is a Number and not a String (which is the default type of params from the Request interface). But I'm not sure how to do this as it is nested inside of the ParamsDictionary interface. Here's a snippet of what I tried unsuccessfully:
declare global {
namespace Express {
export interface ParamsDictionary extends Request {
id: number;
}
}
}
For reference, Request is defined like this in the default Express/Typescript config file:
interface Request<
P = core.ParamsDictionary,
ResBody = any,
ReqBody = any,
ReqQuery = core.Query,
Locals extends Record<string, any> = Record<string, any>
> extends core.Request<P, ResBody, ReqBody, ReqQuery, Locals> {}
This is why I've been trying to modify the ParamsDictionary, which I assume contains the path params that can be found in req.params of a middleware or route function.
I still get errors such as this one, which proves that Typescript still assumes that the id is a string (which I confirm it isn't. it is passed as number): 
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
