@sinclair/typebox
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statically resolved to TypeScript types. TypeBox provides a TypeScr…
Installation
npm install @sinclair/typebox
yarn add @sinclair/typebox
pnpm add @sinclair/typebox
Import
import { Type, Static } from '@sinclair/typebox';Quick Example
import { Type, Static } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
const UserSchema = Type.Object({
name: Type.String({ minLength: 1 }),
email: Type.String({ format: 'email' }),
age: Type.Optional(Type.Integer({ minimum: 0 })),
});
type User = Static<typeof UserSchema>;
const C = TypeCompiler.Compile(UserSchema);
const isValid = C.Check({ name: 'Alice', email: '[email protected]' });About @sinclair/typebox
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statically resolved to TypeScript types. TypeBox provides a TypeScript-first API for constructing JSON Schema using familiar TypeScript-like syntax: Type.Object({ name: Type.String(), age: Type.Number() }) produces both a valid JSON Schema object and the corresponding TypeScript type through Static<typeof schema>. This dual-purpose approach means a single schema definition serves as both a runtime validator (through Ajv or TypeBox's own TypeCompiler) and a compile-time TypeScript type, eliminating schema/type duplication. TypeBox schemas are standard JSON Schema objects, making them usable with any JSON Schema-compatible tool, validator, or code generator. The TypeCompiler provides high-performance validation that compiles schemas into optimized checking functions, comparable to Ajv's compilation approach but without the dependency. TypeBox supports all JSON Schema types plus TypeScript-specific types like generics, conditional types, and template literal types. The library is used by Fastify for its type-safe schema system, Elysia for request/response validation, and various API frameworks that want JSON Schema compatibility with TypeScript type inference.
Quick Facts
| Package | @sinclair/typebox |
| Category | Validation |
| Weekly Downloads | 3M+ |
| License | MIT |
| Install | npm install @sinclair/typebox |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScri…
Superstruct is a library for validating data in JavaScript and TypeScript using composable, predicta…
Valibot is a modular schema validation library for TypeScript that achieves exceptionally small bund…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.