Type Providers in Fastify for Full Safety
You'll learn: How to use TypeBox or Zod as type providers in Fastify to automatically infer TypeScript types from your runtime schema definitions, eliminating duplication between validation and types.
The Problem: Two Sources of Truth
You've seen how Fastify uses generics for request/response typing, and you've used Zod schemas for validation. But here's the painful part: you define a schema for runtime validation, then manually write TypeScript types that mirror it. Change the schema? Update the types. Change the types? Update the schema. This duplication is a maintenance nightmare.
Type providers solve this by making your schema the single source of truth. Define it once, get both runtime validation and compile-time types automatically.
TypeBox: The Native Fastify Choice
TypeBox creates JSON Schema definitions that Fastify understands natively, while simultaneously generating TypeScript types:
import { Type, Static } from '@sinclair/typebox';
const UserSchema = Type.Object({
id: Type.Number(),
email: Type.String({ format: 'email' }),
role: Type.Union([
Type.Literal('admin'),
Type.Literal('user')
])
});
// Automatically infer the TypeScript type
type User = Static<typeof UserSchema>;
// Result: { id: number; email: string; role: 'admin' | 'user' }
Now use it as a type provider in your route:
fastify.post('/users', {
schema: {
body: UserSchema,
response: {
200: UserSchema
}
}
}, async (request, reply) => {
// request.body is FULLY TYPED from UserSchema
const { id, email, role } = request.body;
// reply.send() expects the UserSchema shape
return { id, email, role };
});
Fastify validates the incoming JSON against the schema and TypeScript knows the exact shape—zero duplication.
Zod as a Type Provider
Zod schemas also serve as both validators and type generators. Use fastify-type-provider-zod to bridge them:
import { z } from 'zod';
import {
serializerCompiler,
validatorCompiler
} from 'fastify-type-provider-zod';
// Configure Fastify to understand Zod
fastify.setValidatorCompiler(validatorCompiler);
fastify.setSerializerCompiler(serializerCompiler);
const UserSchema = z.object({
id: z.number(),
email: z.string().email(),
role: z.enum(['admin', 'user'])
});
fastify.post('/users', {
schema: {
body: UserSchema,
response: {
200: UserSchema
}
}
}, async (request, reply) => {
// request.body has the inferred Zod type automatically
request.body.email; // string (with email format validation!)
});
The Zod schema validates at runtime and provides the TypeScript type through z.infer<typeof UserSchema> under the hood.
Why This Matters
Without type providers, you write schemas, then write matching TypeScript interfaces, then hope they stay synchronized. With type providers, the schema IS the type definition. Refactor your schema? TypeScript automatically reflects the change everywhere that schema is used. This is the peak of end-to-end type safety: your runtime contracts and compile-time types are guaranteed identical because they come from the same source.
Key Takeaway: Type providers like TypeBox or Zod eliminate type/schema duplication by deriving TypeScript types directly from validation schemas, giving you true single-source-of-truth type safety in Fastify.