Skip to content

Conversation

@smith558
Copy link
Contributor

@smith558 smith558 commented Oct 29, 2025

Description

This fixes the currently very ineffective and (imo) broken TypeScript use example.

Issues addressed:

  • example of actually using the imported TS types
  • making use of the previously unused variable port to show how one would get the port

old version:
image

Related Issues

Check List

Signed-off-by: Stanislav (Stanley) Modrak <[email protected]>
Signed-off-by: Stanislav (Stanley) Modrak <[email protected]>
@Fdawgs Fdawgs requested a review from Copilot October 31, 2025 18:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the TypeScript example in the QuickStart component to improve type safety and add server startup logging.

  • Added explicit generic type parameters to FastifyInstance for better TypeScript type safety
  • Enabled logger in the Fastify initialization
  • Added server startup log message to inform users when the server is listening

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

import { Server, IncomingMessage, ServerResponse } from 'http'
const server: FastifyInstance = Fastify({})
const server: FastifyInstance<Server, IncomingMessage, ServerResponse> = Fastify({ logger: true })
Copy link

Copilot AI Oct 31, 2025

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 simpler const server: FastifyInstance = Fastify({ logger: true }) is the recommended approach.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

IDK 😂

Copy link
Contributor Author

@smith558 smith558 Nov 1, 2025

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".

Copy link
Contributor Author

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 them

Copy link
Contributor Author

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

const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port
server.log.info(`Server listening on port ${port}`)
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

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

Error: Unexpected token

Copilot uses AI. Check for mistakes.
const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port
server.log.info(`Server listening on port ${port}`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
server.log.info(`Server listening on port ${port}`)
server.log.info('Server listening on port %s', port)

Copy link
Contributor Author

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.

import { Server, IncomingMessage, ServerResponse } from 'http'
const server: FastifyInstance = Fastify({})
const server: FastifyInstance<Server, IncomingMessage, ServerResponse> = Fastify({ logger: true })
Copy link
Member

Choose a reason for hiding this comment

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

IDK 😂

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

Successfully merging this pull request may close these issues.

2 participants