Skip to content
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

@types/express request's body object to T (generic) #16958

Closed
lilling opened this issue Jun 5, 2017 · 3 comments
Closed

@types/express request's body object to T (generic) #16958

lilling opened this issue Jun 5, 2017 · 3 comments

Comments

@lilling
Copy link

lilling commented Jun 5, 2017

hey there!

why not change the body of the Request to T? that way I can be sure what my object contains.

in express-serve-static-core/index.d.ts line 438:

interface Request<T> extends http.IncomingMessage, Express.Request {
...
...
body: T
...
...

and I use it like this:

updateUser = (req: Request<User>, res: Response, next: NextFunction) => {
   const userToUpdate = req.body;
...
...
}
@alex3165
Copy link
Contributor

alex3165 commented Oct 6, 2017

Agreed, this should be changed for peoples that want to type their Request body, same should apply to cookies, params, query, @borisyankov what do you think?

@watzon
Copy link

watzon commented Oct 17, 2017

I don't hate this idea, but at the same time there is this issue from types/express which explains that body was left out for a reason. That reason being that body doesn't exist on the Express Request object natively, but rather is added because of body-parser.

Unfortunately the DefinitelyTyped version of body-parser doesn't work with the workaround they proposed. So either body needs to be added here, or DefinitelyTyped/body-parser needs to be updated.

@alex3165
Copy link
Contributor

alex3165 commented Oct 18, 2017

Make sense, also I think there is a way to include this feature without breaking changes by using default generics, ex:

interfare Request<B = any, Q = any, P = any, C = any> {
  body: B;
  query: Q;
  params: P;
  cookies: C;
}

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants