fastify

Fastify

Used in 193 typescript projects (avg ★242)

TypeScript
framework
Used by 193 projects

Details

Language / Topic
typescriptTypeScript
Category
framework
Compatible Frameworks
fastify

Rules

balanced
- Use `Type` providers with `@fastify/type-provider-typebox` or `@fastify/type-provider-zod` for automatic request/response type inference from schemas.
- Type route handlers with schema generics: `fastify.get<{ Params: { id: string }; Reply: User }>('/users/:id', handler)`.
- Type plugins as `FastifyPluginAsync<PluginOptions>` with explicit options interfaces for modular, typed route registration.
- Register typed hooks with generics: `fastify.addHook<{ Body: CreateUserBody }>('preHandler', async (request) => { ... })` — request body is automatically typed from the generic.
- Use `fastify.withTypeProvider<TypeBoxTypeProvider>()` to enable schema-driven type inference across all routes in a plugin scope.
- Extend Fastify's types via declaration merging: `declare module 'fastify' { interface FastifyRequest { user: User } }` for typed decorator properties.
- Define route schemas with `Type.Object()` (TypeBox) or `z.object()` (Zod) and let the type provider infer `request.body`, `request.params`, and `reply` types.
- Type hooks with `onRequest: async (request: FastifyRequest<{ Headers: AuthHeaders }>, reply) => { ... }` for typed lifecycle middleware.
- Use `FastifyInstance` type for dependency injection in route plugin factory functions.