- 1.1.26Latest
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.0
- 1.0.81
- 1.0.81
- 1.0.80
- 1.0.79
- 1.0.78
- 1.0.77
- 1.0.76
- 1.0.75
- 1.0.74
- 1.0.74
- 1.0.73
- 1.0.72
- 1.0.72
- 1.0.71
- 1.0.70
- 1.0.69
- 1.0.68
- 1.0.67
- 1.0.66
- 1.0.65
- 1.0.64
- 1.0.63
- 1.0.62
- 1.0.61
- 1.0.60
- 1.0.59
- 1.0.59
- 1.0.58
- 1.0.57
- 1.0.56
- 1.0.55
- 1.0.54
- 1.0.53
- 1.0.52
- 1.0.51
- 1.0.50
- 1.0.49
- 1.0.49
- 1.0.48
- 1.0.47
- 1.0.46
- 1.0.45
- 1.0.44
- 1.0.43
- 1.0.42
- 1.0.41
- 1.0.40
- 1.0.39
- 1.0.38
- 1.0.37
- 1.0.36
- 1.0.35
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-rc
- 0.34.41
- 0.34.40
- 0.34.39
- 0.34.38
- 0.34.36
- 0.34.35
- 0.34.34
- 0.34.33
- 0.34.32
- 0.34.31
- 0.34.30
- 0.34.29
- 0.34.28
- 0.34.27
- 0.34.26
- 0.34.25
- 0.34.24
- 0.34.23
- 0.34.22
- 0.34.21
- 0.34.20
- 0.34.19
- 0.34.18
- 0.34.17
- 0.34.16
- 0.34.15
- 0.34.14
- 0.34.13
- 0.34.12
- 0.34.11
- 0.34.10
- 0.34.9
- 0.34.8
- 0.34.7
- 0.34.6
- 0.34.5
- 0.34.3
- 0.34.1
- 0.34.1
- 0.34.0
- 0.33.22
- 0.33.22
- 0.33.21
- 0.33.20
- 0.33.20
- 0.33.19
- 0.33.18
- 0.33.18
- 0.33.17
- 0.33.16
- 0.33.15
- 0.33.14
- 0.33.12
- 0.33.11
- 0.33.10
- 0.33.9
- 0.33.8
- 0.33.7
- 0.33.7
- 0.33.6
- 0.33.6
- 0.33.5
- 0.33.4
- 0.33.3
- 0.33.2
- 0.33.2
- 0.33.1
- 0.33.0
- 0.32.35
- 0.32.34
- 0.32.33
- 0.32.32
- 0.32.30
- 0.32.29
- 0.32.28
- 0.32.27
- 0.32.27
- 0.32.25
- 0.32.24
- 0.32.23
- 0.32.22
- 0.32.21
- 0.32.20
- 0.32.19
- 0.32.18
- 0.32.18
- 0.32.17
- 0.32.16
- 0.32.15
- 0.32.14
- 0.32.13
- 0.32.12
- 0.32.11
- 0.32.10
- 0.32.9
- 0.32.8
- 0.32.7
- 0.32.6
- 0.32.5
- 0.32.4
- 0.32.3
- 0.32.2
- 0.32.1
- 0.32.0
- 0.31.28
- 0.31.27
- 0.31.26
- 0.31.25
- 0.31.24
- 0.31.23
- 0.31.23
- 0.31.22
- 0.31.21
- 0.31.20
- 0.31.19
- 0.31.18
- 0.31.17
- 0.31.16
- 0.31.15
- 0.31.14
- 0.31.13
- 0.31.11
- 0.31.10
- 0.31.9
- 0.31.8
- 0.31.8
- 0.31.7
- 0.31.6
- 0.31.5
- 0.31.4
- 0.31.3
- 0.31.2
- 0.31.1
- 0.31.1
- 0.31.0
- 0.30.4
- 0.30.3
- 0.30.2
- 0.30.1
- 0.30.0
- 0.29.6
- 0.29.5
- 0.29.4
- 0.29.3
- 0.29.2
- 0.29.2
- 0.29.1
- 0.29.1
- 0.29.0
- 0.28.20
- 0.28.19
- 0.28.18
- 0.28.17
- 0.28.16
- 0.28.16
- 0.28.15
- 0.28.14
- 0.28.14
- 0.28.13
- 0.28.12
- 0.28.11
- 0.28.10
- 0.28.9
- 0.28.8
- 0.28.7
- 0.28.6
- 0.28.5
- 0.28.4
- 0.28.3
- 0.28.2
- 0.28.1
- 0.28.1
- 0.28.0
- 0.27.8
- 0.27.7
- 0.27.6
- 0.27.5
- 0.27.4
- 0.27.3
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.8
- 0.26.7
- 0.26.6
- 0.26.5
- 0.26.4
- 0.26.3
- 0.26.2
- 0.26.1
- 0.26.0
- 0.26.0-dev.5
- 0.26.0-dev.4
- 0.26.0-dev.3
- 0.26.0-dev.2
- 0.26.0-dev.1
- 0.26.0-dev
- 0.25.24
- 0.25.23
- 0.25.22
- 0.25.21
- 0.25.20
- 0.25.19
- 0.25.18
- 0.25.17
- 0.25.16
- 0.25.15
- 0.25.14
- 0.25.13
- 0.25.12
- 0.25.11
- 0.25.10
- 0.25.9
- 0.25.8
- 0.25.7
- 0.25.6
- 0.25.5
- 0.25.4
- 0.25.3
- 0.25.2
- 0.25.1
- 0.25.0
- 0.24.51
- 0.24.50
- 0.24.49
- 0.24.48
- 0.24.47
- 0.24.46
- 0.24.45
- 0.24.44
- 0.24.43
- 0.24.42
- 0.24.41
- 0.24.40
- 0.24.39
- 0.24.38
- 0.24.37
- 0.24.36
- 0.24.35
- 0.24.34
- 0.24.33
- 0.24.32
- 0.24.31
- 0.24.30
- 0.24.29
- 0.24.28
- 0.24.27
- 0.24.26
- 0.24.25
- 0.24.24
- 0.24.23
- 0.24.22
- 0.24.21
- 0.24.20
- 0.24.19
- 0.24.18
- 0.24.17
- 0.24.16
- 0.24.15
- 0.24.14
- 0.24.13
- 0.24.12
- 0.24.11
- 0.24.10
- 0.24.9
- 0.24.8
- 0.24.7
- 0.24.6
- 0.24.5
- 0.24.4
- 0.24.3
- 0.24.2
- 0.24.1
- 0.24.0
- 0.23.4
- 0.23.3
- 0.23.1
- 0.23.0
- 0.22.1
- 0.22.0
- 0.21.2
- 0.21.2
- 0.21.1
- 0.21.0
- 0.20.6
- 0.20.5
- 0.20.4
- 0.20.3
- 0.20.2
- 0.20.1
- 0.20.0
- 0.19.2
- 0.19.1
- 0.19.0
- 0.18.1
- 0.18.0
- 0.17.8
- 0.17.7
Install
$ npm install typeboxUsage
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
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
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 has a high-performance JSON Schema compiler that supports drafts 3 to 2020-12. The compiler is designed to be an industry-grade alternative to Ajv and offers improved compilation and validation performance. It 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: 'user@domain.com' // email: string
}) // } = ...
Specification
TypeBox tests against the official JSON Schema test suite. The following table shows specification coverage across drafts 3 to the v1 candidate.
| 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 is compilation throughput for various JSON Schema structures using AJV8 as the baseline.
ββββββββββββββββββββββββ¬ββββββββββββββ¬ββββββββββββββ
β 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 is the validation throughput for various JSON Schema structures using AJV8 as the baseline.
ββββββββββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ
β 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.