Skip to content

Suggestion for Fastify 5: Required param to .send #5534

@JonRC

Description

@JonRC

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

I propose to make the param of the send method 'required' in the type declaration.
This way, if you do not use any RouteGenericInterface or TypeProvider, you will be free to pass anything to send, including nothing.
But, if you use any RouteGenericInterface or TypeProvider, the type checker will be more strict. You will not be able to reply undefined for a required reply's payload specified through TypesProvider or RouteGenericInterface.

Motivation

My motivation was this issue turkerdev/fastify-type-provider-zod#88

In this issue, the user asks about a more restricted type checking for replies.

Example

const server = fastify()

interface ReplyHttpCodes {
  Reply: {
    201: string
    204: undefined
  }
}

server.get<ReplyHttpCodes>('/', async function handler (request, reply) {
  reply.status(201).send('some string') // No Error
  reply.status(204).send() // No Error
  reply.status(204).send(undefined) //No Error

  reply.status(201).send() // TS Error: Expected 1 arguments, but got 0 (My PR introduced this checking)
  reply.status(201).send(123) // TS Error: Argument of type 'number' is not assignable to parameter of type 'string'
  reply.status(204).send('543534') //TS Error: Argument of type 'string' is not assignable to parameter of type 'void' 
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions