There is going to be a ton of existing code that accesses params via req.params.whatever. This no longer works with version 4.16.8 of @types/express-server-static-core.
The solutions provisional is add "@types/express-serve-static-core": "4.16.2" in devDependencies, so, in this post them explain the reasons of the change
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37502
I offered a migration guide here: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37502#issuecomment-522155668
I've just put in a PR To revert these changes. I think we can fix what this was trying to solve in a non-breaking or minimally-breaking way: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37696
This change is breaking lot of builds that is depending on req.param as a any type.
PR have to be approved and merge very soon..
The PR #37684 still will cause builds to break. The type definition assumes that all params will be string, which is not the case if someone has a middle ware that sanitizes the input.
Something similar like this was tried a couple of years ago #20820, and I believe was reverted due to how big of an impact the change was going to be.
From that thread, it looks like the impact was that all consumers would need to update to at least typescript 2.3? That was one of the changes made in the PR, and is hopefully not a huge issue now that this version has been live for years.
The typedef does assume that all params are string by default, but it's generic. Consumers with sanitizing middlewares can provide a more accurate type themselves, even defining an interface if they want. Still a breakage, but the fix is easy and doesn't involve unsafe casts or runtime type guards.
I am trying to get a pull request together to relax the Params type a bit.
The problem I am having running into when I use an array of middlewares, req ends up being infered as any and not Request
const auth_middleware = [ isLoggedIn, ensurePayment ]; // These are in another file
// This does nto work
app.get<any>("/someRoute", auth_middleware, (req /*<-- This is be infered as any */, res) => {
});
// This does
app.get<any>("/someRoute", isLoggedIn, ensurePayment, (req /*Request<any>*/, res) => {
});
The files in a PR I am working towards submitting
This is biting me too. I have a middleware-like library that validates incoming parameters, and handles things such changing a string parameter that is expected to be a number into a number.
Is there any alternative to wrapping in any?