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

docs: fix header and body generic types #2103

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ const opts: fastify.RouteShorthandOptions = {
server.get<Query, Params, Headers, Body>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type Query!
console.log(request.params) // this is of type Params!
console.log(request.body) // this is of type Body!
console.log(request.headers) // this is of type Headers!
console.log(request.body) // this is of type Body!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These re-orders aren't required, but I think it's more clear to follow the order of types. Body being last one.

reply.code(200).send({ pong: 'it worked!' })
})
```
Expand Down Expand Up @@ -143,8 +143,8 @@ const opts: fastify.RouteShorthandOptions = {
server.get<fastify.DefaultQuery, Params, unknown>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type fastify.DefaultQuery!
console.log(request.params) // this is of type Params!
console.log(request.body) // this is of type unknown!
console.log(request.headers) // this is of type fastify.DefaultHeader because typescript will use the default type value!
console.log(request.headers) // this is of type unknown!
console.log(request.body) // this is of type fastify.DefaultBody because typescript will use the default type value!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the easiest correction to make to be valid, however I think it does take the value of example down a bit.
I thought the example was trying to make the case for Body being unknown and because no value was given for headers it defaults to DefaultHeaders; however, when the type ordering is corrected if we keep the example there is not a way to specify unknown for body without defining type for headers which means the example is no longer applicable. This is why I only switched the order.

reply.code(200).send({ pong: 'it worked!' })
})

Expand All @@ -155,8 +155,8 @@ server.get<fastify.DefaultQuery, Params, unknown>('/ping/:bar', opts, (request,
server.get<unknown, Params, unknown, unknown>('/ping/:bar', opts, (request, reply) => {
console.log(request.query) // this is of type unknown!
console.log(request.params) // this is of type Params!
console.log(request.body) // this is of type unknown!
console.log(request.headers) // this is of type unknown!
console.log(request.body) // this is of type unknown!
reply.code(200).send({ pong: 'it worked!' })
})
```
Expand Down