-
-
Notifications
You must be signed in to change notification settings - Fork 67
docs(examples): Fix ineffective TypeScript example #350
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -128,7 +128,7 @@ fastify.listen({ port: 3000 }, (err) => { | |||||
| const typescript = `import Fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify' | ||||||
| import { Server, IncomingMessage, ServerResponse } from 'http' | ||||||
|
|
||||||
| const server: FastifyInstance = Fastify({}) | ||||||
| const server: FastifyInstance<Server, IncomingMessage, ServerResponse> = Fastify({ logger: true }) | ||||||
|
|
||||||
| const opts: RouteShorthandOptions = { | ||||||
| schema: { | ||||||
|
|
@@ -155,6 +155,7 @@ const start = async () => { | |||||
|
|
||||||
| const address = server.server.address() | ||||||
| const port = typeof address === 'string' ? address : address?.port | ||||||
| server.log.info(`Server listening on port ${port}`) | ||||||
|
||||||
| server.log.info(`Server listening on port ${port}`) | |
| server.log.info('Server listening on port %s', port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure? All other examples in the docs prefer to use ${x} type of interpolation (e.g. https://fastify.dev/docs/latest/Guides/Getting-Started/). We should ensure consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explicit generic type parameters
<Server, IncomingMessage, ServerResponse>are unnecessary in modern Fastify TypeScript usage. Fastify v4+ has improved type inference and these generics are deprecated. The simplerconst server: FastifyInstance = Fastify({ logger: true })is the recommended approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK 😂
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Eomm I know that's the case (the types are inferred since v4+). But this code block is meant to be a trivial code example. Of which the whole purpose was just to demonstrate TypeScript typing compatibility of Fastify. This is still hugely better than it was before, which had floating unused imports appearing in a code block, with no apparent use (and breaking TypeScript lint rules). As well as the accompanying text not logically relating to the code example. Otherwise, the whole code block should just be removed completely (as it doesn't demonstrate anything and actually breaks good practices).
I am not sure how much Copilot feedback is relevant here as this is meant to be a training article. It likely misses that the point here is "demonstration".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be useful to add a comment line
// from Fastify v4 these types are inferred automatically, you may omit themThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Eomm btw, Copilot is wrong here, this generic is not deprecated, it's just inferred and not necessary to type in, but the type generic is still used and is very much valid