npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

typebox

v1.1.37

Published

Json Schema Type Builder with Static Type Resolution for TypeScript

Readme

npm version Downloads Build License

Install

$ npm install typebox

Usage

import Type from 'typebox'

const T = Type.Object({                     // const T = {
  x: Type.Number(),                         //   type: 'object',
  y: Type.Number(),                         //   properties: {
  z: Type.Number()                          //     x: { type: 'number' },
})                                          //     y: { type: 'number' },
                                            //     z: { type: 'number' }
                                            //   },
                                            //   required: ['x', 'y', 'z']
                                            // }

type T = Type.Static<typeof T>              // type T = {
                                            //   x: number,
                                            //   y: number,
                                            //   z: number
                                            // }

Overview

Documentation

TypeBox is a runtime type system that creates in-memory JSON Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard JSON Schema.

This library is designed to allow JSON Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST, RPC and MCP services to help validate data received over the wire.

License: MIT

Contents

Type

Documentation | Example

TypeBox types are JSON Schema fragments that compose into more complex types. The library offers a set of types used to construct JSON Schema compliant schematics as well as a set of extended types used to model constructs native to the JavaScript language. The schematics produced by TypeBox can be passed directly to any JSON Schema compliant validator.

Example

The following creates a User type and infers with Static.

import Type from 'typebox'

// Type

const User = Type.Object({                       // const User = {
  id: Type.String(),                             //   type: 'object',
  name: Type.String(),                           //   properties: {
  email: Type.String({ format: 'email' })        //     id: { type: 'string' },
})                                               //     name: { type: 'string' },
                                                 //     email: { 
                                                 //       type: 'string', 
                                                 //       format: 'email' 
                                                 //     }
                                                 //   }
                                                 //   required: [
                                                 //     'id', 
                                                 //     'name', 
                                                 //     'email'
                                                 //   ]
                                                 // }

// Static

type User = Type.Static<typeof User>              // type User = {
                                                  //   id: string,
                                                  //   name: string,
                                                  //   email: string
                                                  // }

Script

Documentation | Example 1 | Example 2

The TypeBox Script function is a micro DSL for constructing JSON Schema from TypeScript syntax. It offers a full syntactic frontend to Type.* with broad support for type-level expressions including Conditional, Mapped, Infer, Generic, Distributive types and more. This feature is implemented symmetrically at both runtime and in the type system.

Example

The following uses the Script function to parse TypeScript interfaces into JSON Schema.

import Type from 'typebox'

// Script

const { User, UserUpdate } = Type.Script(`

  interface Entity {
    id: string
  }

  interface User extends Entity { 
    name: string, 
    email: string 
  }

  type UserUpdate = Evaluate<
    Pick<User, keyof Entity> & 
    Partial<Omit<User, keyof Entity>>
  >

`)

// Reflect

console.log(User)                                // {
                                                 //   type: 'object',
                                                 //   properties: {
                                                 //     id: { type: 'string' },
                                                 //     name: { type: 'string' },
                                                 //     email: { type: 'string' }
                                                 //   },
                                                 //   required: [
                                                 //     'id', 
                                                 //     'name', 
                                                 //     'email'
                                                 //   ]
                                                 // }

console.log(UserUpdate)                          // {
                                                 //   type: 'object',
                                                 //   properties: {
                                                 //     id: { type: 'string' },
                                                 //     name: { type: 'string' },
                                                 //     email: { type: 'string' }
                                                 //   },
                                                 //   required: ['id']
                                                 // }

// Static

type User = Type.Static<typeof User>              // type User = {
                                                  //   id: string,
                                                  //   name: string,
                                                  //   email: string
                                                  // }

type UserUpdate = Type.Static<typeof UserUpdate>  // type UserUpdate = {
                                                  //   id: string,
                                                  //   name?: string,
                                                  //   email?: string
                                                  // }

Schema

Documentation | Example 1 | Example 2 | Specification

TypeBox includes a high-performance JSON Schema compiler with support for drafts 3 through to 2020-12. The compiler is designed to be a lightweight industry-grade alternative to Ajv and offers improved compilation and validation performance. The compiler also offers automatic fallback to dynamic checking in JIT-restricted environments such as Cloudflare Workers.

Example

The following uses the Schema submodule to compile a TypeBox type.

import Schema from 'typebox/schema'

// Compile

const User = Schema.Compile(Type.Object({        // const User = Validator<Type.TObject<{
  id: Type.String(),                             //   id: Type.TString;
  name: Type.String(),                           //   name: Type.TString;
  email: Type.String({ format: 'email' })        //   email: Type.TString;
}))                                              // }>, { ... }>

// Parse

const user = User.Parse({                        // const user: {
  id: '65f1a2b3c4d5e6f7a8b9c0d1',                //   id: string,
  name: 'user',                                  //   name: string,
  email: '[email protected]'                       //   email: string
})                                               // } = ...

Coverage

The following table shows specification coverage implemented by TypeBox.

Ref: JSON Schema Test Suite

| Spec | 3 | 4 | 6 | 7 | 2019-09 | 2020-12 | v1 | |:-----|:--|:--|:--|:--|:--|:--|:--| | additionalItems | ✅ | ✅ | ✅ | ✅ | ✅ | - | - | | additionalProperties | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | allOf | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | anchor | - | - | - | - | ✅ | ✅ | ✅ | | anyOf | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | boolean_schema | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | const | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | contains | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | content | - | - | - | - | ✅ | ✅ | ✅ | | default | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | dependencies | 17/18 | ✅ | ✅ | ✅ | - | - | - | | dependentRequired | - | - | - | - | ✅ | ✅ | ✅ | | dependentSchemas | - | - | - | - | ✅ | ✅ | ✅ | | dynamicRef | - | - | - | - | - | 38/44 | 19/27 | | enum | 14/16 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | exclusiveMaximum | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | exclusiveMinimum | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | if-then-else | - | - | - | ✅ | ✅ | ✅ | ✅ | | infinite-loop-detection | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | items | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | maxContains | - | - | - | - | ✅ | ✅ | ✅ | | maximum | 13/14 | 13/14 | ✅ | ✅ | ✅ | ✅ | ✅ | | maxItems | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | maxLength | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | maxProperties | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | minContains | - | - | - | - | ✅ | ✅ | ✅ | | minimum | 12/13 | 16/17 | ✅ | ✅ | ✅ | ✅ | ✅ | | minItems | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | minLength | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | minProperties | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | multipleOf | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | not | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | oneOf | - | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | pattern | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | patternProperties | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | prefixItems | - | - | - | - | - | ✅ | ✅ | | properties | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | propertyNames | - | - | ✅ | ✅ | ✅ | ✅ | ✅ | | recursiveRef | - | - | - | - | ✅ | - | - | | ref | 23/27 | 37/45 | 67/70 | 75/78 | 79/81 | 77/79 | 77/79 | | required | 3/4 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | type | 73/80 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | | unevaluatedItems | - | - | - | - | ✅ | 65/71 | 64/71 | | unevaluatedProperties | - | - | - | - | ✅ | ✅ | 124/125 | | uniqueItems | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |

Performance

The following table shows compile performance for various JSON Schema structures using AJV8 as a basis for comparison.

┌──────────────────────┬─────────────┬─────────────┐
│ Compile              │ TB1X        │ AJV8        │
├──────────────────────┼─────────────┼─────────────┤
│ Boolean              │ 28.4K ops/s │    7K ops/s │
│ Number               │ 21.8K ops/s │  7.7K ops/s │
│ String               │ 47.8K ops/s │  7.3K ops/s │
│ Null                 │ 35.6K ops/s │  7.8K ops/s │
│ Literal_String       │ 28.6K ops/s │  6.3K ops/s │
│ Literal_Number       │ 46.6K ops/s │  6.2K ops/s │
│ Literal_Boolean      │ 40.8K ops/s │  6.6K ops/s │
│ Pattern              │ 29.7K ops/s │  4.9K ops/s │
│ Object_Open          │  6.8K ops/s │  1.1K ops/s │
│ Object_Close         │  7.4K ops/s │   833 ops/s │
│ Object_Vector3       │ 19.4K ops/s │  2.1K ops/s │
│ Object_Basis3        │    6K ops/s │   895 ops/s │
│ Intersect_And        │   12K ops/s │  3.5K ops/s │
│ Intersect_Structural │  8.4K ops/s │  1.1K ops/s │
│ Union_Or             │ 18.2K ops/s │  2.5K ops/s │
│ Union_Structural     │ 10.9K ops/s │  1.3K ops/s │
│ Tuple_Values         │  7.3K ops/s │  1.6K ops/s │
│ Tuple_Objects        │  1.9K ops/s │   339 ops/s │
│ Array_Numbers_4      │ 29.9K ops/s │  3.4K ops/s │
│ Array_Numbers_8      │ 20.3K ops/s │  3.4K ops/s │
│ Array_Numbers_16     │ 29.4K ops/s │  3.3K ops/s │
│ Array_Objects_Open   │  6.3K ops/s │   684 ops/s │
│ Array_Objects_Close  │  7.3K ops/s │   762 ops/s │
└──────────────────────┴─────────────┴─────────────┘

The following tables shows validation performance for various JSON Schema structures using AJV8 as a basis for comparison.

┌──────────────────────┬──────────────┬──────────────┐
│ Validate             │ TB1X         │ AJV8         │
├──────────────────────┼──────────────┼──────────────┤
│ Boolean              │ 164.1M ops/s │ 181.5M ops/s │
│ Number               │   107M ops/s │  50.2M ops/s │
│ String               │ 102.2M ops/s │  61.9M ops/s │
│ Null                 │ 112.1M ops/s │  48.2M ops/s │
│ Literal_String       │ 102.8M ops/s │  61.5M ops/s │
│ Literal_Number       │ 109.1M ops/s │  46.4M ops/s │
│ Literal_Boolean      │ 109.6M ops/s │  63.3M ops/s │
│ Pattern              │  24.7M ops/s │  20.3M ops/s │
│ Object_Open          │  75.4M ops/s │  37.3M ops/s │
│ Object_Close         │  35.9M ops/s │  21.9M ops/s │
│ Object_Vector3       │  77.6M ops/s │  47.4M ops/s │
│ Object_Basis3        │    37M ops/s │  24.3M ops/s │
│ Intersect_And        │  93.3M ops/s │  61.1M ops/s │
│ Intersect_Structural │    83M ops/s │  36.4M ops/s │
│ Union_Or             │  99.7M ops/s │   8.6M ops/s │
│ Union_Structural     │  81.3M ops/s │  43.5M ops/s │
│ Tuple_Values         │  72.4M ops/s │  41.7M ops/s │
│ Tuple_Objects        │  32.6M ops/s │  22.4M ops/s │
│ Array_Numbers_4      │  94.1M ops/s │  42.8M ops/s │
│ Array_Numbers_8      │  90.6M ops/s │  42.3M ops/s │
│ Array_Numbers_16     │  77.5M ops/s │  40.2M ops/s │
│ Array_Objects_Open   │  26.3M ops/s │  19.6M ops/s │
│ Array_Objects_Close  │   9.1M ops/s │    10M ops/s │
└──────────────────────┴──────────────┴──────────────┘

Versions

TypeBox ships two distinct versions that span two generations of the TypeScript compiler.

| TypeBox | TypeScript | Description | | :--- | :--- | :--- | | 1.x | 6.0 - 7.0+ | Latest. Developed against the TypeScript 7 native compiler. Provides advanced type inference and native JSON Schema 2020-12 support. Includes backwards compatibility with 0.x types. ESM only. | | 0.x | 5.0 - 6.0 | LTS. Developed against older TypeScript versions and actively maintained under Long Term Support. Compatible with both ESM and CJS. Issues should be submitted to the Sinclair TypeBox repository. |

Contribute

TypeBox is open to community contribution. Please ensure you submit an issue before submitting a pull request. The TypeBox project prefers open community discussion before accepting new features.