turkerdev / fastify-type-provider-zod

MIT License
413 stars 25 forks source link

Sending blank response body does not provide runtime type safety #88

Closed tzezar closed 4 months ago

tzezar commented 5 months ago

having schema set like this

export const schema = {
    response: {
        201: z.object({
            status: z.literal('success'),
            data: z.object({
                id: z.number()
            })
        })
    }
}

sending blank response DOES NOT raise ts error

export const testCommandHandler = async (
    req: FastifyRequestZod<typeof schema >,
    reply: FastifyReplyZod<typeof schema >
) => {
    return reply.status(201).send()
}

changing it to object does raise ts err

return reply.status(201).send({})
JonRC commented 5 months ago

It's a behavior from Fastify. The send method param is optional, so you can pass whatever was inferred from the schema or undefined (nothing).

I've already opened an issue to discuss this point https://github.com/fastify/fastify/issues/5534