TypeScript + Express : req.params
It seems that the req.params
is any
so the compiler has no way of knowing that the value for id
is string, BUT of course it's a string as it comes as a path param which are always strings.
You should be able to deal with it using a router middleware, something like:
router.use(function (req, res, next) { if (req.params && req.params.id && typeof req.params.id === "string") { let num = Number(req.params.id); if (!isNaN(num)) { req.params.id = Number(req.params.id); } } next();});
That should convert all params named id (which are strings) into numbers.