Closed Drarig29 closed 2 years ago
I fixed this by removing the ParamsDictionary
, which was too losely typed to be compatible with { myProperty: number; }
.
To do this, you can specify a custom parameter to Express's Request
:
- const checkUserAuth = (req: Request, res: Response, next: NextFunction) => { }
+ const checkUserAuth = (req: Request<unknown>, res: Response, next: NextFunction) => { }
Okay, little follow-up. The fact that I only needed to have one unknown
was because my custom schema was only specifying params. But I added a query in my schema and it broke again.
This time with the error:
Property 'myQueryProperty' is missing in type 'ParsedQs' but required in type '{ myQueryProperty: number; }'.
So I needed to have Request<unknown, unknown, unknown, unknown>
instead, to be sure. And it's not a problem semantically because in such a middleware, we actually don't know a lot about a request, and we don't want to.
I created a new type not to have to repeat this everywhere:
import { Request } from 'express';
export type AnyRequest = Request<unknown, unknown, unknown, unknown>
So I finally have:
- const checkUserAuth = (req: Request, res: Response, next: NextFunction) => { }
+ const checkUserAuth = (req: AnyRequest, res: Response, next: NextFunction) => { }
I seem to have the same (or similar error). If i add a session auth handler with the same signature you used I still get an untyped request. Removing the auth handler from the route fixes this. But i dont know how to fix the actual problem.
I had the following route, which was compiling perfectly:
But as soon as I add a middleware, in my case to check that the user is authenticated, the compilation breaks:
The compilation error:
Here is the signature of my middleware: