Validation3M+/wkMIT

@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
npm install @sinclair/typebox
yarn
yarn add @sinclair/typebox
pnpm
pnpm add @sinclair/typebox

Import

ESM
import { Type, Static } from '@sinclair/typebox';

Quick Example

usage
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
CategoryValidation
Weekly Downloads3M+
LicenseMIT
Installnpm install @sinclair/typebox

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.