-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Does not type check if withTypeProvider is not fluent #55
Comments
you need to do const typeboxServer = server.withTypeProvider<TypeBoxTypeProvider>(); or add the
|
That's correct. We can't mutate the server instance without returning a new instance with the typebox definitions. A good strategy always calls it in the server creation: const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>() |
@mcollina Hi! The TypeProvider setup does require functions to chain in this way. Each call to Type Mapping Statements (An Experiment)Interestingly, it is actually possible to use TypeScript assertion predicates to map generic arguments with line by line statements (so non-fluent - mutable generics). As a proof of concept, the following implements statement assertions to map Type Providers. TypeScript Link Here (Note: Type inference may take a little while to load) const server = Fastify()
// -----------------------------------------------------------------------
Fastify.withTypeProvider<TypeBoxTypeProvider>(server)
server.post('/', {
schema: {
body: Type.Object({
name: Type.String()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<JsonSchemaToTsProvider>(server)
server.post('/', {
schema: {
body: {
type: 'object',
required: ['name'],
properties: { name: { type: 'string' } }
} as const
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<ZodTypeProvider>(server)
server.post('/', {
schema: {
body: z.object({
name: z.string()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
})
// -----------------------------------------------------------------------
Fastify.withTypeProvider<YupTypeProvider>(server)
server.post('/', {
schema: {
body: yup.object({
name: yup.string().required()
})
}
}, async (request, reply) => {
const { name } = request.body // ok
return { hello: name }
}) |
Thanks! I will send a PR to include some of this in the README. |
Consider the following:
This does not work but it errors with:
However if we inline the route definition and
withTypeProvider
call it works:The text was updated successfully, but these errors were encountered: