-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
bugConfirmed bugConfirmed buggood first issueGood for newcomersGood for newcomerstypescriptTypeScript relatedTypeScript related
Description
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'
})Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugConfirmed bugConfirmed buggood first issueGood for newcomersGood for newcomerstypescriptTypeScript relatedTypeScript related