New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Express.Request] Trying to type a req.locals field #765
Comments
|
I'm not super familiar with Express, but |
|
Yes, the Response object has a E.g: route.get('/:projectId',
validateRequest, // This middleware validates the structure of the projectId value
hydrate, // In this middleware I would like to retrieve from the DB the minimum information of the project and put it in the req.locals
checkPermission, // Here I take the req.locals.project and check for the permissions
async (req, res, next) => { // The actual route here, permissions checked and req.locals fullfilled } |
|
Ahh, that makes sense. I think you can update your ambient module declaration to just be FWIW, I went through a similar exploration in our Koa app, and ended up moving all of the request validation logic into the endpoint with a few helper functions to reduce boilerplate since middleware doesn't make it easy to be type-safe since it doesn't "return" which is the only thing that is easily type-able. |
|
@Lzok going to close for now, but feel free to continue the discussion here if you think of something that is Zod-specific. |


Hi! How are you?
I am trying to type an express
request.localsfield without success by the moment. I can see the types when I hover on the element but Typescript is complaining me.Here in the images you can see the typing is correct (or I think so), but the VS Code is complaining.
// request.locals.project seems to be okBut TS is complaining:
Here is an exact copy of my code at this moment
What my main goal is? To be able to type a
request.localsobject where I will store data related to the request.Any clue? Thank you!
The text was updated successfully, but these errors were encountered: