Install
Npm
$ npm install @sinclair/typebox --save
Example
import { Type, type Static } from '@sinclair/typebox'
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
})
type T = Static<typeof T>
Overview
TypeBox is a runtime type builder that creates in-memory Json Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard Json Schema validation.
This library enables Json Schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
License MIT
Contents
Usage
The following shows general usage.
import { Type, type Static } from '@sinclair/typebox'
type T = {
id: string,
name: string,
timestamp: number
}
const T = Type.Object({
id: Type.String(),
name: Type.String(),
timestamp: Type.Integer()
})
type T = Static<typeof T>
import { Value } from '@sinclair/typebox/value'
function receive(value: T) {
if(Value.Check(T, value)) {
}
}
Types
TypeBox types are Json Schema fragments that compose into more complex types. Each fragment is structured such that any Json Schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox offers a set of Json Types which are used to create Json Schema compliant schematics as well as a JavaScript type set used to create schematics for constructs native to JavaScript.
Json Types
The following table lists the supported Json types. These types are fully compatible with the Json Schema Draft 7 specification.
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
│ TypeBox │ TypeScript │ Json Schema │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Any() │ type T = any │ const T = { } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Unknown() │ type T = unknown │ const T = { } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.String() │ type T = string │ const T = { │
│ │ │ type: 'string' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Number() │ type T = number │ const T = { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Integer() │ type T = number │ const T = { │
│ │ │ type: 'integer' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Boolean() │ type T = boolean │ const T = { │
│ │ │ type: 'boolean' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Null() │ type T = null │ const T = { │
│ │ │ type: 'null' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Literal(42) │ type T = 42 │ const T = { │
│ │ │ const: 42, │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Array( │ type T = number[] │ const T = { │
│ Type.Number() │ │ type: 'array', │
│ ) │ │ items: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Object({ │ type T = { │ const T = { │
│ x: Type.Number(), │ x: number, │ type: 'object', │
│ y: Type.Number() │ y: number │ required: ['x', 'y'], │
│ }) │ } │ properties: { │
│ │ │ x: { │
│ │ │ type: 'number' │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Tuple([ │ type T = [number, number] │ const T = { │
│ Type.Number(), │ │ type: 'array', │
│ Type.Number() │ │ items: [{ │
│ ]) │ │ type: 'number' │
│ │ │ }, { │
│ │ │ type: 'number' │
│ │ │ }], │
│ │ │ additionalItems: false, │
│ │ │ minItems: 2, │
│ │ │ maxItems: 2 │
│ │ │ } │
│ │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ enum Foo { │ enum Foo { │ const T = { │
│ A, │ A, │ anyOf: [{ │
│ B │ B │ type: 'number', │
│ } │ } │ const: 0 │
│ │ │ }, { │
│ const T = Type.Enum(Foo) │ type T = Foo │ type: 'number', │
│ │ │ const: 1 │
│ │ │ }] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Const({ │ type T = { │ const T = { │
│ x: 1, │ readonly x: 1, │ type: 'object', │
│ y: 2, │ readonly y: 2 │ required: ['x', 'y'], │
│ } as const) │ } │ properties: { │
│ │ │ x: { │
│ │ │ type: 'number', │
│ │ │ const: 1 │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number', │
│ │ │ const: 2 │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │
│ Type.Object({ │ x: number, │ anyOf: [{ │
│ x: Type.Number(), │ y: number │ type: 'string', │
│ y: Type.Number() │ } │ const: 'x' │
│ }) │ │ }, { │
│ ) │ │ type: 'string', │
│ │ │ const: 'y' │
│ │ │ }] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Union([ │ type T = string | number │ const T = { │
│ Type.String(), │ │ anyOf: [{ │
│ Type.Number() │ │ type: 'string' │
│ ]) │ │ }, { │
│ │ │ type: 'number' │
│ │ │ }] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Intersect([ │ type T = { │ const T = { │
│ Type.Object({ │ x: number │ allOf: [{ │
│ x: Type.Number() │ } & { │ type: 'object', │
│ }), │ y: number │ required: ['x'], │
│ Type.Object({ │ } │ properties: { │
│ y: Type.Number() │ │ x: { │
│ ]) │ │ type: 'number' │
│ ]) │ │ } │
│ │ │ } │
│ │ │ }, { │
│ │ │ type: 'object', |
│ │ │ required: ['y'], │
│ │ │ properties: { │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ }] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Composite([ │ type T = { │ const T = { │
│ Type.Object({ │ x: number, │ type: 'object', │
│ x: Type.Number() │ y: number │ required: ['x', 'y'], │
│ }), │ } │ properties: { │
│ Type.Object({ │ │ x: { │
│ y: Type.Number() │ │ type: 'number' │
│ }) │ │ }, │
│ ]) │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Never() │ type T = never │ const T = { │
│ │ │ not: {} │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Not( | type T = unknown │ const T = { │
│ Type.String() │ │ not: { │
│ ) │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Extends( │ type T = │ const T = { │
│ Type.String(), │ string extends number │ const: false, │
│ Type.Number(), │ ? true │ type: 'boolean' │
│ Type.Literal(true), │ : false │ } │
│ Type.Literal(false) │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Extract( │ type T = Extract< │ const T = { │
│ Type.Union([ │ string | number, │ type: 'string' │
│ Type.String(), │ string │ } │
│ Type.Number(), │ > │ │
│ ]), │ │ │
│ Type.String() │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Exclude( │ type T = Exclude< │ const T = { │
│ Type.Union([ │ string | number, │ type: 'number' │
│ Type.String(), │ string │ } │
│ Type.Number(), │ > │ │
│ ]), │ │ │
│ Type.String() │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Mapped( │ type T = { │ const T = { │
│ Type.Union([ │ [_ in 'x' | 'y'] : number │ type: 'object', │
│ Type.Literal('x'), │ } │ required: ['x', 'y'], │
│ Type.Literal('y') │ │ properties: { │
│ ]), │ │ x: { │
│ () => Type.Number() │ │ type: 'number' │
│ ) │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const U = Type.Union([ │ type U = 'open' | 'close' │ const T = { │
│ Type.Literal('open'), │ │ type: 'string', │
│ Type.Literal('close') │ type T = `on${U}` │ pattern: '^on(open|close)$' │
│ ]) │ │ } │
│ │ │ │
│ const T = Type │ │ │
│ .TemplateLiteral([ │ │ │
│ Type.Literal('on'), │ │ │
│ U │ │ │
│ ]) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Record( │ type T = Record< │ const T = { │
│ Type.String(), │ string, │ type: 'object', │
│ Type.Number() │ number │ patternProperties: { │
│ ) │ > │ '^.*$': { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Partial( │ type T = Partial<{ │ const T = { │
│ Type.Object({ │ x: number, │ type: 'object', │
│ x: Type.Number(), │ y: number │ properties: { │
│ y: Type.Number() | }> │ x: { │
│ }) │ │ type: 'number' │
│ ) │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Required( │ type T = Required<{ │ const T = { │
│ Type.Object({ │ x?: number, │ type: 'object', │
│ x: Type.Optional( │ y?: number │ required: ['x', 'y'], │
│ Type.Number() | }> │ properties: { │
│ ), │ │ x: { │
│ y: Type.Optional( │ │ type: 'number' │
│ Type.Number() │ │ }, │
│ ) │ │ y: { │
│ }) │ │ type: 'number' │
│ ) │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Pick( │ type T = Pick<{ │ const T = { │
│ Type.Object({ │ x: number, │ type: 'object', │
│ x: Type.Number(), │ y: number │ required: ['x'], │
│ y: Type.Number() │ }, 'x'> │ properties: { │
│ }), ['x'] | │ x: { │
│ ) │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Omit( │ type T = Omit<{ │ const T = { │
│ Type.Object({ │ x: number, │ type: 'object', │
│ x: Type.Number(), │ y: number │ required: ['y'], │
│ y: Type.Number() │ }, 'x'> │ properties: { │
│ }), ['x'] | │ y: { │
│ ) │ │ type: 'number' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Index( │ type T = { │ const T = { │
│ Type.Object({ │ x: number, │ type: 'number' │
│ x: Type.Number(), │ y: string │ } │
│ y: Type.String() │ }['x'] │ │
│ }), ['x'] │ │ │
│ ) │ │ │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const A = Type.Tuple([ │ type A = [0, 1] │ const T = { │
│ Type.Literal(0), │ type B = [2, 3] │ type: 'array', │
│ Type.Literal(1) │ type T = [ │ items: [ │
│ ]) │ ...A, │ { const: 0 }, │
│ const B = Type.Tuple([ │ ...B │ { const: 1 }, │
| Type.Literal(2), │ ] │ { const: 2 }, │
| Type.Literal(3) │ │ { const: 3 } │
│ ]) │ │ ], │
│ const T = Type.Tuple([ │ │ additionalItems: false, │
| ...Type.Rest(A), │ │ minItems: 4, │
| ...Type.Rest(B) │ │ maxItems: 4 │
│ ]) │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Uncapitalize( │ type T = Uncapitalize< │ const T = { │
│ Type.Literal('Hello') │ 'Hello' │ type: 'string', │
│ ) │ > │ const: 'hello' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Capitalize( │ type T = Capitalize< │ const T = { │
│ Type.Literal('hello') │ 'hello' │ type: 'string', │
│ ) │ > │ const: 'Hello' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Uppercase( │ type T = Uppercase< │ const T = { │
│ Type.Literal('hello') │ 'hello' │ type: 'string', │
│ ) │ > │ const: 'HELLO' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Lowercase( │ type T = Lowercase< │ const T = { │
│ Type.Literal('HELLO') │ 'HELLO' │ type: 'string', │
│ ) │ > │ const: 'hello' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Object({ │ type T = { │ const R = { │
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
│ y: Type.Number() │ y: number │ } │
│ }, { $id: 'T' }) | } │ │
│ │ │ │
│ const R = Type.Ref(T) │ type R = T │ │
│ │ │ │
│ │ │ │
│ │ │ │
│ │ │ │
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
JavaScript Types
TypeBox provides an extended type set that can be used to create schematics for common JavaScript constructs. These types can not be used with any standard Json Schema validator; but can be used to frame schematics for interfaces that may receive Json validated data. JavaScript types are prefixed with the [JavaScript]
jsdoc comment for convenience. The following table lists the supported types.
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
│ TypeBox │ TypeScript │ Extended Schema │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Constructor([ │ type T = new ( │ const T = { │
│ Type.String(), │ arg0: string, │ type: 'Constructor', │
│ Type.Number() │ arg0: number │ parameters: [{ │
│ ], Type.Boolean()) │ ) => boolean │ type: 'string' │
│ │ │ }, { │
│ │ │ type: 'number' │
│ │ │ }], │
│ │ │ returns: { │
│ │ │ type: 'boolean' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Function([ │ type T = ( │ const T = { │
| Type.String(), │ arg0: string, │ type: 'Function', │
│ Type.Number() │ arg1: number │ parameters: [{ │
│ ], Type.Boolean()) │ ) => boolean │ type: 'string' │
│ │ │ }, { │
│ │ │ type: 'number' │
│ │ │ }], │
│ │ │ returns: { │
│ │ │ type: 'boolean' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Promise( │ type T = Promise<string> │ const T = { │
│ Type.String() │ │ type: 'Promise', │
│ ) │ │ item: { │
│ │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = │ type T = │ const T = { │
│ Type.AsyncIterator( │ AsyncIterableIterator< │ type: 'AsyncIterator', │
│ Type.String() │ string │ items: { │
│ ) │ > │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Iterator( │ type T = │ const T = { │
│ Type.String() │ IterableIterator<string> │ type: 'Iterator', │
│ ) │ │ items: { │
│ │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.RegExp(/abc/) │ type T = string │ const T = { │
│ │ │ type: 'string' │
│ │ │ pattern: 'abc' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Uint8Array() │ type T = Uint8Array │ const T = { │
│ │ │ type: 'Uint8Array' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Date() │ type T = Date │ const T = { │
│ │ │ type: 'Date' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Undefined() │ type T = undefined │ const T = { │
│ │ │ type: 'undefined' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Symbol() │ type T = symbol │ const T = { │
│ │ │ type: 'symbol' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.BigInt() │ type T = bigint │ const T = { │
│ │ │ type: 'bigint' │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Void() │ type T = void │ const T = { │
│ │ │ type: 'void' │
│ │ │ } │
│ │ │ │
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
Options
You can pass Json Schema options on the last argument of any type. Option hints specific to each type are provided for convenience.
const T = Type.String({
format: 'email'
})
const T = Type.Number({
multipleOf: 2
})
const T = Type.Array(Type.Integer(), {
minItems: 5
})
Properties
Object properties can be modified with Readonly and Optional. The following table shows how these modifiers map between TypeScript and Json Schema.
┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
│ TypeBox │ TypeScript │ Json Schema │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Object({ │ type T = { │ const T = { │
│ name: Type.ReadonlyOptional( │ readonly name?: string │ type: 'object', │
│ Type.String() │ } │ properties: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Object({ │ type T = { │ const T = { │
│ name: Type.Readonly( │ readonly name: string │ type: 'object', │
│ Type.String() │ } │ properties: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['name'] │
│ │ │ } │
│ │ │ │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
│ const T = Type.Object({ │ type T = { │ const T = { │
│ name: Type.Optional( │ name?: string │ type: 'object', │
│ Type.String() │ } │ properties: { │
│ ) │ │ name: { │
│ }) │ │ type: 'string' │
│ │ │ } │
│ │ │ } │
│ │ │ } │
│ │ │ │
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
Modular Types
TypeBox implements a modular type system. The following imports the full type system (recommended)
import { Type } from '@sinclair/typebox'
The following imports types individually which enables modern bundlers to optimize bundle sizes via tree shaking.
import { Object, Number } from '@sinclair/typebox'
Generic Types
Generic types can be created with generic functions. All types extend the base type TSchema. It is common to constrain generic function arguments to this type. The following creates a generic Vector type.
import { Type, type Static, type TSchema } from '@sinclair/typebox'
const Vector = <T extends TSchema>(t: T) => Type.Object({ x: t, y: t, z: t })
const NumberVector = Vector(Type.Number())
type NumberVector = Static<typeof NumberVector>
Generic types are often used to create aliases for more complex types. The following creates a Nullable generic type.
const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
const T = Nullable(Type.String())
type T = Static<typeof T>
Reference Types
Reference types can be created with Type.Ref. These types infer the same as the target type.
const Vector = Type.Object({
x: Type.Number(),
y: Type.Number(),
}, { $id: 'Vector' })
const VectorRef = Type.Ref(Vector)
type VectorRef = Static<typeof VectorRef>
Use Type.Deref to dereference a type. This will replace any $ref
with the target type.
const Vertex = Type.Object({
position: VectorRef,
texcoord: VectorRef,
})
const VertexDeref = Type.Deref(Vertex, [Vector])
Note that reference types do not contain structural information about the type they're referencing. This means they cannot be used directly with some type mapping types (such as Partial) that require structural information to operate. For applications that need mapping on references, use Type.Deref to dereference the type first.
Recursive Types
TypeBox supports singular recursive data structures. Recursive type inference is also supported. The following creates a recursive Node data structure.
const Node = Type.Recursive(This => Type.Object({
id: Type.String(),
nodes: Type.Array(This)
}), { $id: 'Node' })
type Node = Static<typeof Node>
function test(node: Node) {
const id = node.nodes[0].nodes[0].id
}
Conditional Types
TypeBox supports runtime Conditional types with Type.Extends. This type runs a structural assignability check against the first and second arguments and will return either the third (true
) or fourth (false
) argument based on the result. The conditional types Exclude and Extract are also supported. The following shows both TypeScript and TypeBox examples of conditional types.
type T = string extends number ? true : false
const T = Type.Extends(
Type.String(),
Type.Number(),
Type.Literal(true),
Type.Literal(false)
)
type T = Extract<1 | 2 | 3, 1>
const T = Type.Extract(
Type.Union([
Type.Literal(1),
Type.Literal(2),
Type.Literal(3)
]),
Type.Literal(1)
)
type T = Exclude<1 | 2 | 3, 1>
const T = Type.Exclude(
Type.Union([
Type.Literal(1),
Type.Literal(2),
Type.Literal(3)
]),
Type.Literal(1)
)
Mapped Types
TypeBox supports Mapped types with Type.Mapped. This type accepts a union of property keys as the first argument, and a type mapping function which accepts each key (K
) as the second. This type can be used with Conditional and Indexed Access types to allow for generalized property remapping irrespective of property type. The following example remaps each property of T
to be T[K] | null
.
const T = Type.Object({
x: Type.Number(),
y: Type.String(),
z: Type.Boolean()
})
const M = Type.Mapped(Type.KeyOf(T), K => {
return Type.Union([
Type.Index(T, K),
Type.Null()
])
})
Template Literal Types
TypeBox supports Template Literal types with Type.TemplateLiteral. This type can be created using a syntax similar to the TypeScript Template Literal syntax. TypeBox encodes Template Literals as regular expression string patterns which enables the template to be checked by Json Schema validators. This type also supports regular expression parsing, enabling template patterns to be used for generative types. The following shows both TypeScript and TypeBox usage.
type K = `prop${'A'|'B'|'C'}`
type R = Record<K, string>
const K = Type.TemplateLiteral('prop${A|B|C}')
const R = Type.Record(K, Type.String())
Indexed Access Types
TypeBox supports Indexed Access types with Type.Index. This type enables uniform access to interior property and array element types without having to extract them from the underlying schema representation. This type is supported for Object, Array, Tuple, Union and Intersect types.
const T = Type.Object({
x: Type.Number(),
y: Type.String(),
z: Type.Boolean()
})
const A = Type.Index(T, ['x'])
const B = Type.Index(T, ['x', 'y'])
const C = Type.Index(T, Type.KeyOf(T))
Intrinsic Types
TypeBox supports the TypeScript Intrinsic String Manipulation types Uppercase, Lowercase, Capitalize and Uncapitalize. These types can be used to remap String Literal, Template Literal and Union of Literal types.
type A = Capitalize<'hello'>
type B = Capitalize<'hello' | 'world'>
type C = Capitalize<`hello${1|2|3}`>
const A = Type.Capitalize(Type.Literal('hello'))
const B = Type.Capitalize(Type.Union([
Type.Literal('hello'),
Type.Literal('world')
]))
const C = Type.Capitalize(
Type.TemplateLiteral('hello${1|2|3}')
)
Transform Types
TypeBox supports value decoding and encoding with Transform types. These types work in tandem with the Encode and Decode functions available on the Value and TypeCompiler modules. Transform types can be used to convert Json encoded values into constructs more natural to JavaScript. The following creates a Transform type to decode numbers into Dates using the Value module.
import { Value } from '@sinclair/typebox/value'
const T = Type.Transform(Type.Number())
.Decode(value => new Date(value))
.Encode(value => value.getTime())
const decoded = Value.Decode(T, 0)
const encoded = Value.Encode(T, decoded)
Use the StaticEncode or StaticDecode types to infer a Transform type.
import { Static, StaticDecode, StaticEncode } from '@sinclair/typebox'
const T = Type.Transform(Type.Array(Type.Number(), { uniqueItems: true }))
.Decode(value => new Set(value))
.Encode(value => [...value])
type D = StaticDecode<typeof T>
type E = StaticEncode<typeof T>
type T = Static<typeof T>
Rest Types
TypeBox provides the Rest type to uniformly extract variadic tuples from Intersect, Union and Tuple types. This type can be useful to remap variadic types into different forms. The following uses Rest to remap a Tuple into a Union.
const T = Type.Tuple([
Type.String(),
Type.Number()
])
const R = Type.Rest(T)
const U = Type.Union(R)
Unsafe Types
TypeBox supports user defined types with Unsafe. This type allows you to specify both schema representation and inference type. The following creates an Unsafe type with a number schema that infers as string.
const T = Type.Unsafe<string>({ type: 'number' })
type T = Static<typeof T>
The Unsafe type is often used to create schematics for extended specifications like OpenAPI
const Nullable = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T> | null>({
...schema, nullable: true
})
const T = Nullable(Type.String())
type T = Static<typeof T>
const StringEnum = <T extends string[]>(values: [...T]) => Type.Unsafe<T[number]>({
type: 'string', enum: values
})
const S = StringEnum(['A', 'B', 'C'])
type S = Static<typeof T>
TypeGuard
TypeBox can type check its own types with the TypeGuard module. This module is written for reflection and provides structural tests for every built-in TypeBox type. Functions of this module return is
guards which can be used with TypeScript control flow assertions to obtain schema inference. The following guards that the value A is TString.
import { Type, TypeGuard } from '@sinclair/typebox'
const A: unknown = { ... }
if(TypeGuard.TString(A)) {
A.type
}
Strict
TypeBox types contain various symbol properties that are used for reflection, composition and compilation. These properties are not strictly valid Json Schema; so in some cases it may be desirable to omit them. TypeBox provides a Strict
function that will omit these properties if necessary.
const T = Type.Object({
name: Type.Optional(Type.String())
})
const U = Type.Strict(T)
Values
TypeBox provides an optional utility module that can be used to perform structural operations on JavaScript values. This module includes functionality to create, check and cast values from types as well as check equality, clone, diff and patch JavaScript values. This module is provided via optional import.
import { Value } from '@sinclair/typebox/value'
Create
Use the Create function to create a value from a type. TypeBox will use default values if specified.
const T = Type.Object({ x: Type.Number(), y: Type.Number({ default: 42 }) })
const A = Value.Create(T)
Clone
Use the Clone function to deeply clone a value.
const A = Value.Clone({ x: 1, y: 2, z: 3 })
Check
Use the Check function to type check a value.
const T = Type.Object({ x: Type.Number() })
const R = Value.Check(T, { x: 1 })
Convert
Use the Convert function to convert a value into its target type if a reasonable conversion is possible. This function may return an invalid value and should be checked before use. Its return type is unknown
.
const T = Type.Object({ x: Type.Number() })
const R1 = Value.Convert(T, { x: '3.14' })
const R2 = Value.Convert(T, { x: 'not a number' })
Clean
Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first.
const T = Type.Object({
x: Type.Number(),
y: Type.Number()
})
const X = Value.Clean(T, null)
const Y = Value.Clean(T, { x: 1 })
const Z = Value.Clean(T, { x: 1, y: 2, z: 3 })
Default
Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first.
const T = Type.Object({
x: Type.Number({ default: 0 }),
y: Type.Number({ default: 0 })
})
const X = Value.Default(T, null)
const Y = Value.Default(T, { })
const Z = Value.Default(T, { x: 1 })
Cast
Use the Cast function to cast a value with a type. The cast function will retain as much information as possible from the original value.
const T = Type.Object({ x: Type.Number(), y: Type.Number() }, { additionalProperties: false })
const X = Value.Cast(T, null)
const Y = Value.Cast(T, { x: 1 })
const Z = Value.Cast(T, { x: 1, y: 2, z: 3 })
Decode
Use the Decode function to decode a value from a type, or throw if the value is invalid. The return value will infer as the decoded type. This function will run Transform codecs if available.
const A = Value.Decode(Type.String(), 'hello')
const B = Value.Decode(Type.String(), 42)
Encode
Use the Encode function to encode a value to a type, or throw if the value is invalid. The return value will infer as the encoded type. This function will run Transform codecs if available.
const A = Value.Encode(Type.String(), 'hello')
const B = Value.Encode(Type.String(), 42)
Equal
Use the Equal function to deeply check for value equality.
const R = Value.Equal(
{ x: 1, y: 2, z: 3 },
{ x: 1, y: 2, z: 3 }
)
Hash
Use the Hash function to create a FNV1A-64 non cryptographic hash of a value.
const A = Value.Hash({ x: 1, y: 2, z: 3 })
const B = Value.Hash({ x: 1, y: 4, z: 3 })
Diff
Use the Diff function to generate a sequence of edits that will transform one value into another.
const E = Value.Diff(
{ x: 1, y: 2, z: 3 },
{ y: 4, z: 5, w: 6 }
)
Patch
Use the Patch function to apply a sequence of edits.
const A = { x: 1, y: 2 }
const B = { x: 3 }
const E = Value.Diff(A, B)
const C = Value.Patch<typeof B>(A, E)
Errors
Use the Errors function to enumerate validation errors.
const T = Type.Object({ x: Type.Number(), y: Type.Number() })
const R = [...Value.Errors(T, { x: '42' })]
Mutate
Use the Mutate function to perform a deep mutable value assignment while retaining internal references.
const Y = { z: 1 }
const X = { y: Y }
const A = { x: X }
Value.Mutate(A, { x: { y: { z: 2 } } })
const R0 = A.x.y.z === 2
const R1 = A.x.y === Y
const R2 = A.x === X
Pointer
Use ValuePointer to perform mutable updates on existing values using RFC6901 Json Pointers.
import { ValuePointer } from '@sinclair/typebox/value'
const A = { x: 0, y: 0, z: 0 }
ValuePointer.Set(A, '/x', 1)
ValuePointer.Set(A, '/y', 1)
ValuePointer.Set(A, '/z', 1)
TypeRegistry
The TypeBox type system can be extended with additional types and formats using the TypeRegistry and FormatRegistry modules. These modules integrate deeply with TypeBox's internal type checking infrastructure and can be used to create application specific types, or register schematics for alternative specifications.
TypeRegistry
Use the TypeRegistry to register a new type. The Kind must match the registered type name.
import { TypeRegistry, Symbols } from '@sinclair/typebox'
TypeRegistry.Set('Foo', (schema, value) => value === 'foo')
const A = Value.Check({ [Kind]: 'Foo' }, 'foo')
const B = Value.Check({ [Kind]: 'Foo' }, 'bar')
FormatRegistry
Use the FormatRegistry to register a string format.
import { FormatRegistry } from '@sinclair/typebox'
FormatRegistry.Set('foo', (value) => value === 'foo')
const T = Type.String({ format: 'foo' })
const A = Value.Check(T, 'foo')
const B = Value.Check(T, 'bar')
TypeCheck
TypeBox types target Json Schema Draft 7 and are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for TypeBox types that offers high performance compilation and value checking.
The following sections detail using Ajv and the TypeBox compiler infrastructure.
Ajv
The following shows the recommended setup for Ajv.
$ npm install ajv ajv-formats --save
import { Type } from '@sinclair/typebox'
import addFormats from 'ajv-formats'
import Ajv from 'ajv'
const ajv = addFormats(new Ajv({}), [
'date-time',
'time',
'date',
'email',
'hostname',
'ipv4',
'ipv6',
'uri',
'uri-reference',
'uuid',
'uri-template',
'json-pointer',
'relative-json-pointer',
'regex'
])
const validate = ajv.compile(Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
}))
const R = validate({ x: 1, y: 2, z: 3 })
TypeCompiler
The TypeBox TypeCompiler is a high performance JIT validation compiler that transforms TypeBox types into optimized JavaScript validation routines. The compiler is tuned for fast compilation as well as fast value assertion. It is built to serve as a validation backend that can be integrated into larger applications. It can also be used for code generation.
The TypeCompiler is provided as an optional import.
import { TypeCompiler } from '@sinclair/typebox/compiler'
Use the Compile function to JIT compile a type. Note that compilation is generally an expensive operation and should only be performed once per type during application start up. TypeBox does not cache previously compiled types, and applications are expected to hold references to each compiled type for the lifetime of the application.
const C = TypeCompiler.Compile(Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
}))
const R = C.Check({ x: 1, y: 2, z: 3 })
Use the Errors function to generate diagnostic errors for a value. The Errors function will return an iterator that when enumerated; will perform an exhaustive check across the entire value yielding any error found. For performance, this function should only be called after a failed Check. Applications may also choose to yield only the first value to avoid exhaustive error generation.
const C = TypeCompiler.Compile(Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
}))
const value = { }
const first = C.Errors(value).First()
const all = [...C.Errors(value)]
Use the Code function to generate assertion functions as strings. This function can be used to create high performance assertions that can be written to disk as importable modules. The following generates code to check a string.
const C = TypeCompiler.Code(Type.String())
TypeSystem
The TypeBox TypeSystem module provides configurations to use either Json Schema or TypeScript type checking semantics. Configurations made to the TypeSystem module are observed by the TypeCompiler, Value and Error modules.
Policies
TypeBox validates using standard Json Schema assertion policies by default. The TypeSystemPolicy module can override some of these to have TypeBox check values inline with TypeScript static assertions. It also provides overrides for certain checking rules related to non-serializable values (such as void) which can be useful in Json based protocols such as JsonRpc-2.
The following overrides are available.
import { TypeSystemPolicy } from '@sinclair/typebox/system'
TypeSystemPolicy.ExactOptionalPropertyTypes = true
TypeSystemPolicy.AllowArrayObject = true
TypeSystemPolicy.AllowNaN = true
TypeSystemPolicy.AllowNullVoid = true
Error Function
Error messages in TypeBox can be customized by defining an ErrorFunction. This function allows for the localization of error messages as well as enabling custom error messages for custom types. By default, TypeBox will generate messages using the en-US
locale. To support additional locales, you can replicate the function found in src/errors/function.ts
and create a locale specific translation. The function can then be set via SetErrorFunction.
The following example shows an inline error function that intercepts errors for String, Number and Boolean only. The DefaultErrorFunction is used to return a default error message.
import { SetErrorFunction, DefaultErrorFunction, ValueErrorType } from '@sinclair/typebox/errors'
SetErrorFunction((schema, errorType) => {
switch(errorType) {
case ValueErrorType.String: return 'Expected string'
case ValueErrorType.Number: return 'Nombre attendu'
case ValueErrorType.Boolean: return '예상 부울'
default: return DefaultErrorFunction(schema, errorType)
}
})
const T = Type.Object({
x: Type.String(),
y: Type.Number(),
z: Type.Boolean()
})
const E = [...Value.Errors(T, {
x: null,
y: null,
z: null
})]
TypeBox Workbench
TypeBox offers a web based code generation tool that can convert TypeScript types into TypeBox types as well as several other ecosystem libraries.
TypeBox Workbench Link Here
TypeBox Codegen
TypeBox provides a code generation library that can be used to automate type translation between TypeScript and TypeBox. This library also includes functionality to transform TypeScript types to other ecosystem libraries.
TypeBox Codegen Link Here
Ecosystem
The following is a list of community packages that offer general tooling, extended functionality and framework integration support for TypeBox.
Benchmark
This project maintains a set of benchmarks that measure Ajv, Value and TypeCompiler compilation and validation performance. These benchmarks can be run locally by cloning this repository and running npm run benchmark
. The results below show for Ajv version 8.12.0 running on Node 20.10.0.
For additional comparative benchmarks, please refer to typescript-runtime-type-benchmarks.
Compile
This benchmark measures compilation performance for varying types. You can review this benchmark here.
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┐
│ (index) │ Iterations │ Ajv │ TypeCompiler │ Performance │
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┤
│ Literal_String │ 1000 │ ' 236 ms' │ ' 9 ms' │ ' 26.22 x' │
│ Literal_Number │ 1000 │ ' 205 ms' │ ' 13 ms' │ ' 15.77 x' │
│ Literal_Boolean │ 1000 │ ' 168 ms' │ ' 4 ms' │ ' 42.00 x' │
│ Primitive_Number │ 1000 │ ' 170 ms' │ ' 8 ms' │ ' 21.25 x' │
│ Primitive_String │ 1000 │ ' 162 ms' │ ' 7 ms' │ ' 23.14 x' │
│ Primitive_String_Pattern │ 1000 │ ' 211 ms' │ ' 10 ms' │ ' 21.10 x' │
│ Primitive_Boolean │ 1000 │ ' 139 ms' │ ' 4 ms' │ ' 34.75 x' │
│ Primitive_Null │ 1000 │ ' 146 ms' │ ' 5 ms' │ ' 29.20 x' │
│ Object_Unconstrained │ 1000 │ ' 1160 ms' │ ' 33 ms' │ ' 35.15 x' │
│ Object_Constrained │ 1000 │ ' 1257 ms' │ ' 26 ms' │ ' 48.35 x' │
│ Object_Vector3 │ 1000 │ ' 393 ms' │ ' 8 ms' │ ' 49.13 x' │
│ Object_Box3D │ 1000 │ ' 1795 ms' │ ' 30 ms' │ ' 59.83 x' │
│ Tuple_Primitive │ 1000 │ ' 542 ms' │ ' 16 ms' │ ' 33.88 x' │
│ Tuple_Object │ 1000 │ ' 1330 ms' │ ' 17 ms' │ ' 78.24 x' │
│ Composite_Intersect │ 1000 │ ' 624 ms' │ ' 17 ms' │ ' 36.71 x' │
│ Composite_Union │ 1000 │ ' 565 ms' │ ' 19 ms' │ ' 29.74 x' │
│ Math_Vector4 │ 1000 │ ' 853 ms' │ ' 10 ms' │ ' 85.30 x' │
│ Math_Matrix4 │ 1000 │ ' 406 ms' │ ' 12 ms' │ ' 33.83 x' │
│ Array_Primitive_Number │ 1000 │ ' 401 ms' │ ' 11 ms' │ ' 36.45 x' │
│ Array_Primitive_String │ 1000 │ ' 385 ms' │ ' 6 ms' │ ' 64.17 x' │
│ Array_Primitive_Boolean │ 1000 │ ' 347 ms' │ ' 8 ms' │ ' 43.38 x' │
│ Array_Object_Unconstrained │ 1000 │ ' 1904 ms' │ ' 25 ms' │ ' 76.16 x' │
│ Array_Object_Constrained │ 1000 │ ' 1582 ms' │ ' 20 ms' │ ' 79.10 x' │
│ Array_Tuple_Primitive │ 1000 │ ' 864 ms' │ ' 11 ms' │ ' 78.55 x' │
│ Array_Tuple_Object │ 1000 │ ' 1658 ms' │ ' 16 ms' │ ' 103.63 x' │
│ Array_Composite_Intersect │ 1000 │ ' 786 ms' │ ' 17 ms' │ ' 46.24 x' │
│ Array_Composite_Union │ 1000 │ ' 844 ms' │ ' 16 ms' │ ' 52.75 x' │
│ Array_Math_Vector4 │ 1000 │ ' 1192 ms' │ ' 10 ms' │ ' 119.20 x' │
│ Array_Math_Matrix4 │ 1000 │ ' 688 ms' │ ' 10 ms' │ ' 68.80 x' │
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┘
Validate
This benchmark measures validation performance for varying types. You can review this benchmark here.
┌────────────────────────────┬────────────┬──────────────┬──────────────┬──────────────┬──────────────┐
│ (index) │ Iterations │ ValueCheck │ Ajv │ TypeCompiler │ Performance │
├────────────────────────────┼────────────┼──────────────┼──────────────┼──────────────┼──────────────┤
│ Literal_String │ 1000000 │ ' 19 ms' │ ' 5 ms' │ ' 4 ms' │ ' 1.25 x' │
│ Literal_Number │ 1000000 │ ' 14 ms' │ ' 19 ms' │ ' 10 ms' │ ' 1.90 x' │
│ Literal_Boolean │ 1000000 │ ' 14 ms' │ ' 19 ms' │ ' 9 ms' │ ' 2.11 x' │
│ Primitive_Number │ 1000000 │ ' 21 ms' │ ' 18 ms' │ ' 10 ms' │ ' 1.80 x' │
│ Primitive_String │ 1000000 │ ' 22 ms' │ ' 17 ms' │ ' 9 ms' │ ' 1.89 x' │
│ Primitive_String_Pattern │ 1000000 │ ' 164 ms' │ ' 44 ms' │ ' 36 ms' │ ' 1.22 x' │
│ Primitive_Boolean │ 1000000 │ ' 18 ms' │ ' 18 ms' │ ' 9 ms' │ ' 2.00 x' │
│ Primitive_Null │ 1000000 │ ' 19 ms' │ ' 17 ms' │ ' 9 ms' │ ' 1.89 x' │
│ Object_Unconstrained │ 1000000 │ ' 966 ms' │ ' 33 ms' │ ' 24 ms' │ ' 1.38 x' │
│ Object_Constrained │ 1000000 │ ' 1275 ms' │ ' 52 ms' │ ' 42 ms' │ ' 1.24 x' │
│ Object_Vector3 │ 1000000 │ ' 427 ms' │ ' 23 ms' │ ' 14 ms' │ ' 1.64 x' │
│ Object_Box3D │ 1000000 │ ' 2024 ms' │ ' 56 ms' │ ' 50 ms' │ ' 1.12 x' │
│ Object_Recursive │ 1000000 │ ' 5263 ms' │ ' 358 ms' │ ' 164 ms' │ ' 2.18 x' │
│ Tuple_Primitive │ 1000000 │ ' 157 ms' │ ' 22 ms' │ ' 12 ms' │ ' 1.83 x' │
│ Tuple_Object │ 1000000 │ ' 767 ms' │ ' 29 ms' │ ' 18 ms' │ ' 1.61 x' │
│ Composite_Intersect │ 1000000 │ ' 769 ms' │ ' 26 ms' │ ' 15 ms' │ ' 1.73 x' │
│ Composite_Union │ 1000000 │ ' 501 ms' │ ' 24 ms' │ ' 13 ms' │ ' 1.85 x' │
│ Math_Vector4 │ 1000000 │ ' 250 ms' │ ' 23 ms' │ ' 11 ms' │ ' 2.09 x' │
│ Math_Matrix4 │ 1000000 │ ' 1073 ms' │ ' 43 ms' │ ' 28 ms' │ ' 1.54 x' │
│ Array_Primitive_Number │ 1000000 │ ' 264 ms' │ ' 21 ms' │ ' 12 ms' │ ' 1.75 x' │
│ Array_Primitive_String │ 1000000 │ ' 242 ms' │ ' 23 ms' │ ' 14 ms' │ ' 1.64 x' │
│ Array_Primitive_Boolean │ 1000000 │ ' 151 ms' │ ' 24 ms' │ ' 14 ms' │ ' 1.71 x' │
│ Array_Object_Unconstrained │ 1000000 │ ' 5864 ms' │ ' 66 ms' │ ' 58 ms' │ ' 1.14 x' │
│ Array_Object_Constrained │ 1000000 │ ' 5996 ms' │ ' 138 ms' │ ' 116 ms' │ ' 1.19 x' │
│ Array_Object_Recursive │ 1000000 │ ' 22515 ms' │ ' 1625 ms' │ ' 585 ms' │ ' 2.78 x' │
│ Array_Tuple_Primitive │ 1000000 │ ' 737 ms' │ ' 38 ms' │ ' 31 ms' │ ' 1.23 x' │
│ Array_Tuple_Object │ 1000000 │ ' 3257 ms' │ ' 77 ms' │ ' 56 ms' │ ' 1.38 x' │
│ Array_Composite_Intersect │ 1000000 │ ' 3119 ms' │ ' 49 ms' │ ' 38 ms' │ ' 1.29 x' │
│ Array_Composite_Union │ 1000000 │ ' 2108 ms' │ ' 72 ms' │ ' 33 ms' │ ' 2.18 x' │
│ Array_Math_Vector4 │ 1000000 │ ' 1078 ms' │ ' 38 ms' │ ' 26 ms' │ ' 1.46 x' │
│ Array_Math_Matrix4 │ 1000000 │ ' 4967 ms' │ ' 126 ms' │ ' 88 ms' │ ' 1.43 x' │
└────────────────────────────┴────────────┴──────────────┴──────────────┴──────────────┴──────────────┘
Compression
The following table lists esbuild compiled and minified sizes for each TypeBox module.
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ Compiled │ Minified │ Compression │
├──────────────────────┼────────────┼────────────┼─────────────┤
│ typebox/compiler │ '118.3 kb' │ ' 52.4 kb' │ '2.26 x' │
│ typebox/errors │ ' 55.1 kb' │ ' 25.2 kb' │ '2.19 x' │
│ typebox/system │ ' 4.6 kb' │ ' 2.0 kb' │ '2.31 x' │
│ typebox/value │ '144.0 kb' │ ' 61.5 kb' │ '2.34 x' │
│ typebox │ ' 87.0 kb' │ ' 36.5 kb' │ '2.39 x' │
└──────────────────────┴────────────┴────────────┴─────────────┘
Contribute
TypeBox is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The TypeBox project preferences open community discussion prior to accepting new features.