Socket
Socket
Sign inDemoInstall

@sinclair/typebox

Package Overview
Dependencies
0
Maintainers
1
Versions
296
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinclair/typebox

JSONSchema Type Builder with Static Type Resolution for TypeScript


Version published
Maintainers
1
Weekly downloads
29,674,687
decreased by-2.04%
Bundle size
1.9 kB
Minified + gzipped

Weekly downloads

Package description

What is @sinclair/typebox?

The @sinclair/typebox package is a TypeScript utility designed to create type-safe schemas with a consistent syntax. It is primarily used for defining data structures with TypeScript types and validating data at runtime using a separate validation library like Ajv.

What are @sinclair/typebox's main functionalities?

Type Creation

Allows the creation of TypeScript types for various data structures such as strings, numbers, objects, arrays, etc. The created types can be used for compile-time type checking and runtime validation.

{"const T = Type.String()"}

Type Composition

Enables the composition of complex types by combining simpler types. This is useful for defining the shape of objects, with optional and required fields.

{"const UserType = Type.Object({ name: Type.String(), age: Type.Optional(Type.Number()) })"}

Type Validation

Provides a way to validate data at runtime against the defined types using a validation library like Ajv. This ensures that the data conforms to the specified schema.

{"const T = Type.String(); const validate = ajv.compile(T); const isValid = validate('hello');"}

Other packages similar to @sinclair/typebox

Readme

Source

TypeBox

JSON Schema Type Builder with Static Type Resolution for TypeScript



npm version Downloads GitHub CI

Install

npm

$ npm install @sinclair/typebox --save

deno

import { Static, Type } from 'npm:@sinclair/typebox'

esm

import { Static, Type } from 'https://esm.sh/@sinclair/typebox'

Usage

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

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

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

Overview

TypeBox is a type builder library 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.

TypeBox is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used either as a simple tool to build up complex schemas or integrated into REST and RPC services to help validate data received over the wire.

License MIT

Contents

Example

The following demonstrates TypeBox's general usage.


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

//--------------------------------------------------------------------------------------------
//
// Let's say you have the following type ...
//
//--------------------------------------------------------------------------------------------

type T = {
  id: string,
  name: string,
  timestamp: number
}

//--------------------------------------------------------------------------------------------
//
// ... you can express this type in the following way.
//
//--------------------------------------------------------------------------------------------

const T = Type.Object({                              // const T = {
  id: Type.String(),                                 //   type: 'object',
  name: Type.String(),                               //   properties: { 
  timestamp: Type.Integer()                          //     id: { 
})                                                   //       type: 'string' 
                                                     //     },
                                                     //     name: { 
                                                     //       type: 'string' 
                                                     //     },
                                                     //     timestamp: { 
                                                     //       type: 'integer' 
                                                     //     }
                                                     //   }, 
                                                     //   required: [
                                                     //     'id',
                                                     //     'name',
                                                     //     'timestamp'
                                                     //   ]
                                                     // } 

//--------------------------------------------------------------------------------------------
//
// ... then infer back to the original static type this way.
//
//--------------------------------------------------------------------------------------------

type T = Static<typeof T>                            // type T = {
                                                     //   id: string,
                                                     //   name: string,
                                                     //   timestamp: number
                                                     // }

//--------------------------------------------------------------------------------------------
//
// ... then use the type both as JSON schema and as a TypeScript type.
//
//--------------------------------------------------------------------------------------------

function receive(value: T) {                         // ... as a Type

  if(JSON.validate(T, value)) {                      // ... as a Schema
  
    // ok...
  }
}

Types

TypeBox provides a set of functions that allow you to compose JSON Schema similar to how you would compose static types with TypeScript. Each function creates a JSON schema fragment which can compose into more complex types. The schemas produced by TypeBox can be passed directly to any JSON Schema compliant validator, or used to reflect runtime metadata for a type.

Standard Types

The following table lists the Standard TypeBox types.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚ 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.RegEx(/foo/)    ā”‚ type T = string             ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚    type: 'string',             ā”‚
ā”‚                                ā”‚                             ā”‚    pattern: 'foo'              ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ 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                 ā”‚   properties: {                ā”‚
ā”‚ })                             ā”‚ }                           ā”‚      x: {                      ā”‚
ā”‚                                ā”‚                             ā”‚        type: 'number'          ā”‚
ā”‚                                ā”‚                             ā”‚      },                        ā”‚
ā”‚                                ā”‚                             ā”‚      y: {                      ā”‚
ā”‚                                ā”‚                             ā”‚        type: 'number'          ā”‚
ā”‚                                ā”‚                             ā”‚      }                         ā”‚
ā”‚                                ā”‚                             ā”‚   },                           ā”‚
ā”‚                                ā”‚                             ā”‚   required: ['x', 'y']         ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ 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.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                 ā”‚   type: 'object',              ā”‚
ā”‚     x: Type.Number()           ā”‚ } & {                       ā”‚   properties: {                ā”‚
ā”‚   }),                          ā”‚   y: number                 ā”‚     x: {                       ā”‚
ā”‚   Type.Object({                ā”‚ }                           ā”‚       type: 'number'           ā”‚
ā”‚     y: Type.Number()           ā”‚                             ā”‚     },                         ā”‚
ā”‚   })                           ā”‚                             ā”‚     y: {                       ā”‚
ā”‚ ])                             ā”‚                             ā”‚       type: 'number'           ā”‚
ā”‚                                ā”‚                             ā”‚     }                          ā”‚
ā”‚                                ā”‚                             ā”‚   },                           ā”‚
ā”‚                                ā”‚                             ā”‚   required: ['x', 'y']         ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Never()         ā”‚ type T = never              ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚   allOf: [{                    ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'boolean',           ā”‚
ā”‚                                ā”‚                             ā”‚     const: false               ā”‚
ā”‚                                ā”‚                             ā”‚   }, {                         ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'boolean',           ā”‚
ā”‚                                ā”‚                             ā”‚     const: true                ā”‚
ā”‚                                ā”‚                             ā”‚   }]                           ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ 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                ā”‚   properties: {                ā”‚
ā”‚       Type.Number()            | }>                          ā”‚     x: {                       ā”‚
ā”‚     ),                         ā”‚                             ā”‚       type: 'number'           ā”‚
ā”‚     y: Type.Optional(          ā”‚                             ā”‚     },                         ā”‚
ā”‚       Type.Number()            ā”‚                             ā”‚     y: {                       ā”‚
ā”‚     )                          ā”‚                             ā”‚       type: 'number'           ā”‚
ā”‚   })                           ā”‚                             ā”‚     }                          ā”‚
ā”‚ )                              ā”‚                             ā”‚   },                           ā”‚
ā”‚                                ā”‚                             ā”‚   required: ['x', 'y']         ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Pick(           ā”‚ type T = Pick<{             ā”‚ const T = {                    ā”‚
ā”‚   Type.Object({                ā”‚   x: number,                ā”‚   type: 'object',              ā”‚
ā”‚     x: Type.Number(),          ā”‚   y: number                 ā”‚   properties: {                ā”‚
ā”‚     y: Type.Number()           | }, 'x'>                     ā”‚     x: {                       ā”‚
ā”‚   }), ['x']                    ā”‚                             ā”‚       type: 'number'           ā”‚
ā”‚ )                              ā”‚                             ā”‚     }                          ā”‚
ā”‚                                ā”‚                             ā”‚   },                           ā”‚
ā”‚                                ā”‚                             ā”‚   required: ['x']              ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Omit(           ā”‚ type T = Omit<{             ā”‚ const T = {                    ā”‚
ā”‚   Type.Object({                ā”‚   x: number,                ā”‚   type: 'object',              ā”‚
ā”‚     x: Type.Number(),          ā”‚   y: number                 ā”‚   properties: {                ā”‚
ā”‚     y: Type.Number()           | }, 'x'>                     ā”‚     y: {                       ā”‚
ā”‚   }), ['x']                    ā”‚                             ā”‚       type: 'number'           ā”‚
ā”‚ )                              ā”‚                             ā”‚     }                          ā”‚
ā”‚                                ā”‚                             ā”‚   },                           ā”‚
ā”‚                                ā”‚                             ā”‚   required: ['y']              ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Extended Types

TypeBox provides a set of extended types that can be used to express schematics for core JavaScript constructs and primitives. Extended types are not valid JSON Schema and will not validate using typical validation. These types however can be used to frame JSON schema and describe callable RPC interfaces that may receive JSON validated data.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚ TypeBox                        ā”‚ TypeScript                  ā”‚ Extended Schema                ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Constructor([   ā”‚ type T = new (              ā”‚ const T = {                    ā”‚
ā”‚   Type.String(),               ā”‚  arg0: string,              ā”‚   type: 'object',              ā”‚
ā”‚   Type.Number()                ā”‚  arg1: number               ā”‚   instanceOf: 'Constructor',   ā”‚
ā”‚ ], Type.Boolean())             ā”‚ ) => boolean                ā”‚   parameters: [{               ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'string'             ā”‚
ā”‚                                ā”‚                             ā”‚   }, {                         ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'number'             ā”‚
ā”‚                                ā”‚                             ā”‚   }],                          ā”‚
ā”‚                                ā”‚                             ā”‚   return: {                    ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'boolean'            ā”‚
ā”‚                                ā”‚                             ā”‚   }                            ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Function([      ā”‚ type T = (                  ā”‚ const T = {                    ā”‚
|   Type.String(),               ā”‚  arg0: string,              ā”‚   type : 'object',             ā”‚
ā”‚   Type.Number()                ā”‚  arg1: number               ā”‚   instanceOf: 'Function',      ā”‚
ā”‚ ], Type.Boolean())             ā”‚ ) => boolean                ā”‚   parameters: [{               ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'string'             ā”‚
ā”‚                                ā”‚                             ā”‚   }, {                         ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'number'             ā”‚
ā”‚                                ā”‚                             ā”‚   }],                          ā”‚
ā”‚                                ā”‚                             ā”‚   return: {                    ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'boolean'            ā”‚
ā”‚                                ā”‚                             ā”‚   }                            ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Promise(        ā”‚ type T = Promise<string>    ā”‚ const T = {                    ā”‚
ā”‚   Type.String()                ā”‚                             ā”‚   type: 'object',              ā”‚
ā”‚ )                              ā”‚                             ā”‚   instanceOf: 'Promise',       ā”‚
ā”‚                                ā”‚                             ā”‚   item: {                      ā”‚
ā”‚                                ā”‚                             ā”‚     type: 'string'             ā”‚
ā”‚                                ā”‚                             ā”‚   }                            ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Uint8Array()    ā”‚ type T = Uint8Array         ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚   type: 'object',              ā”‚
ā”‚                                ā”‚                             ā”‚   instanceOf: 'Uint8Array'     ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Date()          ā”‚ type T = Date               ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚   type: 'object',              ā”‚
ā”‚                                ā”‚                             ā”‚   instanceOf: 'Date'           ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Undefined()     ā”‚ type T = undefined          ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚   type: 'null',                ā”‚
ā”‚                                ā”‚                             ā”‚   typeOf: 'Undefined'          ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Void()          ā”‚ type T = void               ā”‚ const T = {                    ā”‚
ā”‚                                ā”‚                             ā”‚   type: 'null'                 ā”‚
ā”‚                                ā”‚                             ā”‚   typeOf: 'Void'               ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Modifiers

TypeBox provides modifiers that can be applied to an objects properties. This allows for optional and readonly to be applied to that property. The following table illustates how they map between TypeScript and JSON Schema.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚ TypeBox                        ā”‚ TypeScript                  ā”‚ JSON Schema                    ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Type.Object({        ā”‚ type T = {                  ā”‚ const T = {                    ā”‚
ā”‚   name: Type.Optional(         ā”‚   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.ReadonlyOptional( ā”‚   readonly name?: string    ā”‚   type: 'object',              ā”‚
ā”‚     Type.String()              ā”‚ }                           ā”‚   properties: {                ā”‚
ā”‚   )                            ā”‚                             ā”‚     name: {                    ā”‚
ā”‚ })  	                         ā”‚                             ā”‚       type: 'string'           ā”‚
ā”‚                                ā”‚                             ā”‚     }                          ā”‚
ā”‚                                ā”‚                             ā”‚   }                            ā”‚
ā”‚                                ā”‚                             ā”‚ }                              ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Options

You can pass additional JSON schema options on the last argument of any given type. The following are some examples.

// string must be an email
const T = Type.String({ format: 'email' })

// number must be a multiple of 2
const T = Type.Number({ multipleOf: 2 })

// array must have at least 5 integer values
const T = Type.Array(Type.Integer(), { minItems: 5 })

Reference

Use Type.Ref(...) to create referenced types. The target type must specify an $id.

const T = Type.String({ $id: 'T' })                  // const T = {
                                                     //    $id: 'T',
                                                     //    type: 'string'
                                                     // }
                                             
const R = Type.Ref(T)                                // const R = {
                                                     //    $ref: 'T'
                                                     // }

Recursive

Use Type.Recursive(...) to create recursive types.

const Node = Type.Recursive(Node => Type.Object({    // const Node = {
  id: Type.String(),                                 //   $id: 'Node',
  nodes: Type.Array(Node)                            //   type: 'object',
}), { $id: 'Node' })                                 //   properties: {
                                                     //     id: {
                                                     //       type: 'string'
                                                     //     },
                                                     //     nodes: {
                                                     //       type: 'array',
                                                     //       items: {
                                                     //         $ref: 'Node'
                                                     //       }
                                                     //     }
                                                     //   },
                                                     //   required: [
                                                     //     'id',
                                                     //     'nodes'
                                                     //   ]
                                                     // }

type Node = Static<typeof Node>                      // type Node = {
                                                     //   id: string
                                                     //   nodes: Node[]
                                                     // }

function test(node: Node) {
  const id = node.nodes[0].nodes[0]                  // id is string
                 .nodes[0].nodes[0]
                 .id
}

Generic

Use functions to create generic types. The following creates a generic Nullable<T> type.

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

const Nullable = <T extends TSchema>(type: T) => Type.Union([type, Type.Null()])

const T = Nullable(Type.String())                    // const T = {
                                                     //   anyOf: [{
                                                     //     type: 'string'
                                                     //   }, {
                                                     //     type: 'null'
                                                     //   }]
                                                     // }

type T = Static<typeof T>                            // type T = string | null

const U = Nullable(Type.Number())                    // const U = {
                                                     //   anyOf: [{
                                                     //     type: 'number'
                                                     //   }, {
                                                     //     type: 'null'
                                                     //   }]
                                                     // }

type U = Static<typeof U>                            // type U = number | null

Conditional

Use the conditional module to create Conditional Types. This module implements TypeScript's structural equivalence checks to enable TypeBox types to be conditionally inferred at runtime. This module also provides the Extract and Exclude utility types which are expressed as conditional types in TypeScript.

The conditional module is provided as an optional import.

import { Conditional } from '@sinclair/typebox/conditional'

The following table shows the TypeBox mappings between TypeScript and JSON schema.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚ TypeBox                        ā”‚ TypeScript                  ā”‚ JSON Schema                    ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Conditional.Extends( ā”‚ type T =                    ā”‚ const T = {                    ā”‚
ā”‚   Type.String(),               ā”‚  string extends number      ā”‚   const: false,                ā”‚
ā”‚   Type.Number(),               ā”‚  true : false               ā”‚   type: 'boolean'              ā”‚
ā”‚   Type.Literal(true),          ā”‚                             ā”‚ }                              ā”‚
ā”‚   Type.Literal(false)          ā”‚                             ā”‚                                ā”‚
ā”‚ )                              ā”‚                             ā”‚                                ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Conditional.Extract( ā”‚ type T = Extract<           ā”‚ const T = {                    ā”‚
ā”‚   Type.Union([                 ā”‚   'a' | 'b' | 'c',          ā”‚   anyOf: [{                    ā”‚
ā”‚     Type.Literal('a'),         ā”‚   'a' | 'f'                 ā”‚     const: 'a'                 ā”‚
ā”‚     Type.Literal('b'),         ā”‚ >                           ā”‚     type: 'string'             ā”‚
ā”‚     Type.Literal('c')          ā”‚                             ā”‚   }]                           ā”‚
ā”‚   ]),                          ā”‚                             ā”‚ }                              ā”‚
ā”‚   Type.Union([                 ā”‚                             ā”‚                                ā”‚
ā”‚     Type.Literal('a'),         ā”‚                             ā”‚                                ā”‚
ā”‚     Type.Literal('f')          ā”‚                             ā”‚                                ā”‚
ā”‚   ])                           ā”‚                             ā”‚                                ā”‚
ā”‚ )                              ā”‚                             ā”‚                                ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ const T = Conditional.Exclude( ā”‚ type T = Exclude<           ā”‚ const T = {                    ā”‚
ā”‚   Type.Union([                 ā”‚   'a' | 'b' | 'c',          ā”‚   anyOf: [{                    ā”‚
ā”‚     Type.Literal('a'),         ā”‚   'a'                       ā”‚     const: 'b',                ā”‚
ā”‚     Type.Literal('b'),         ā”‚ >                           ā”‚     type: 'string'             ā”‚
ā”‚     Type.Literal('c')          ā”‚                             ā”‚   }, {                         ā”‚
ā”‚   ]),                          ā”‚                             ā”‚     const: 'c',                ā”‚
ā”‚   Type.Union([                 ā”‚                             ā”‚     type: 'string'             ā”‚
ā”‚     Type.Literal('a')          ā”‚                             ā”‚   }]                           ā”‚
ā”‚   ])                           ā”‚                             ā”‚ }                              ā”‚
ā”‚ )                              ā”‚                             ā”‚                                ā”‚
ā”‚                                ā”‚                             ā”‚                                ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Unsafe

Use Type.Unsafe(...) to create custom schemas with user defined inference rules.

const T = Type.Unsafe<string>({ type: 'number' })    // const T = {
                                                     //   type: 'number'
                                                     // }

type T = Static<typeof T>                            // type T = string

This function can be used to create custom schemas for validators that require specific schema representations. An example of this might be OpenAPI's nullable and enum schemas which are not provided by TypeBox. The following demonstrates using Type.Unsafe(...) to create these types.

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

//--------------------------------------------------------------------------------------------
//
// Nullable<T>
//
//--------------------------------------------------------------------------------------------

function Nullable<T extends TSchema>(schema: T) {
  return Type.Unsafe<Static<T> | null>({ ...schema, nullable: true })
}

const T = Nullable(Type.String())                    // const T = {
                                                     //   type: 'string',
                                                     //   nullable: true
                                                     // }

type T = Static<typeof T>                            // type T = string | null


//--------------------------------------------------------------------------------------------
//
// StringEnum<string[]>
//
//--------------------------------------------------------------------------------------------

function StringEnum<T extends string[]>(values: [...T]) {
  return Type.Unsafe<T[number]>({ type: 'string', enum: values })
}

const T = StringEnum(['A', 'B', 'C'])                // const T = {
                                                     //   enum: ['A', 'B', 'C']
                                                     // }

type T = Static<typeof T>                            // type T = 'A' | 'B' | 'C'

Guards

Use the guard module to test if values are TypeBox types.

import { TypeGuard } from '@sinclair/typebox/guard'

const T = Type.String()

if(TypeGuard.TString(T)) {
    
  // T is TString
}

Strict

TypeBox schemas contain the Kind and Modifier symbol properties. These properties are provided to enable runtime type reflection on schemas, as well as helping TypeBox internally compose types. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a Type.Strict() function that will omit these properties if necessary.

const T = Type.Object({                              // const T = {
  name: Type.Optional(Type.String())                 //   [Kind]: 'Object',
})                                                   //   type: 'object',
                                                     //   properties: {
                                                     //     name: {
                                                     //       [Kind]: 'String',
                                                     //       type: 'string',
                                                     //       [Modifier]: 'Optional'
                                                     //     }
                                                     //   }
                                                     // }

const U = Type.Strict(T)                             // const U = {
                                                     //   type: 'object', 
                                                     //   properties: { 
                                                     //     name: { 
                                                     //       type: 'string' 
                                                     //     } 
                                                     //   } 
                                                     // }

Values

TypeBox includes an optional values module that can be used to perform common operations on JavaScript values. This module enables one to create, check and cast values from types. It also provides functionality to check equality, clone and diff and patch JavaScript values. The value module is provided as an optional import.

import { Value } from '@sinclair/typebox/value'

Create

Use the Create function to create a value from a TypeBox 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)                            // const A = { x: 0, y: 42 }

Clone

Use the Clone function to deeply clone a value

const A = Value.Clone({ x: 1, y: 2, z: 3 })          // const A = { 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 })                   // const R = true

Cast

Use the Cast function to cast a value into 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 X = { x: 0, y: 0 }

const Y = Value.Cast(T, { x: 1 })                    // const Y = { x: 1, y: 0 }

const Z = Value.Cast(T, { x: 1, y: 2, z: 3 })        // const Z = { x: 1, y: 2 }

Equal

Use the Equal function to deeply check for value equality.

const R = Value.Equal(                               // const R = true
  { x: 1, y: 2, z: 3 },
  { x: 1, y: 2, z: 3 }
)

Diff

Use the Diff function to produce a sequence of edits to transform one value into another.

const E = Value.Diff(                               // const E = [
  { x: 1, y: 2, z: 3 },                             //   { type: 'update', path: '/y', value: 4 },
  { y: 4, z: 5, w: 6 }                              //   { type: 'update', path: '/z', value: 5 },
)                                                   //   { type: 'insert', path: '/w', value: 6 },
                                                    //   { type: 'delete', path: '/x' }
                                                    // ]

Patch

Use the Patch function to apply edits

const A = { x: 1, y: 2 }

const B = { x: 3 }

const E = Value.Diff(A, B)                           // const E = [
                                                     //   { type: 'update', path: '/x', value: 3 },
                                                     //   { type: 'delete', path: '/y' }
                                                     // ]

const C = Value.Patch<typeof B>(A, E)                // const C = { x: 3 }

Errors

Use the Errors function enumerate validation errors.

const T = Type.Object({ x: Type.Number(), y: Type.Number() })

const R = [...Value.Errors(T, { x: '42' })]          // const R = [{
                                                     //   schema: { type: 'number' },
                                                     //   path: '/x',
                                                     //   value: '42',
                                                     //   message: 'Expected number'
                                                     // }, {
                                                     //   schema: { type: 'number' },
                                                     //   path: '/y',
                                                     //   value: undefined,
                                                     //   message: 'Expected number'
                                                     // }]

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)                         // const A = { x: 1, y: 0, z: 0 }
ValuePointer.Set(A, '/y', 1)                         // const A = { x: 1, y: 1, z: 0 }
ValuePointer.Set(A, '/z', 1)                         // const A = { x: 1, y: 1, z: 1 }

TypeCheck

TypeBox targets JSON Schema Draft 6 and is built and tested against the Ajv JSON Schema validator for standards compliance. TypeBox also includes an optional built-in TypeCompiler that can provide improved compilation and validation performance specifically for TypeBox types only.

The following sections detail using these validators.

Ajv

The following are the recommended configurations to support both the Standard and Extended type sets provided by TypeBox. For schema portability and publishing to remote systems, it is recommended to use the Standard type set only.

$ npm install ajv ajv-formats --save
Standard Ajv Configuration

Expand for Standard Type Set Configuration

import { Type }   from '@sinclair/typebox'
import addFormats from 'ajv-formats'
import Ajv        from 'ajv'

export function createAjv() {
  return addFormats(new Ajv({}), [
    'date-time', 
    'time', 
    'date', 
    'email',  
    'hostname', 
    'ipv4', 
    'ipv6', 
    'uri', 
    'uri-reference', 
    'uuid',
    'uri-template', 
    'json-pointer', 
    'relative-json-pointer', 
    'regex'
  ])
}

const ajv = createAjv()

const R = ajv.validate(Type.Object({                 // const R = true
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number()
}), { x: 1, y: 2, z: 3 })     
Extended Ajv Configuration

Expand for Extended Type Set Configuration

import { TypeGuard } from '@sinclair/typebox/guard'
import { Value }     from '@sinclair/typebox/value'
import { Type }      from '@sinclair/typebox'
import addFormats    from 'ajv-formats'
import Ajv           from 'ajv'

function schemaOf(schemaOf: string, value: unknown, schema: unknown) {
  switch (schemaOf) {
    case 'Constructor':
      return TypeGuard.TConstructor(schema) && Value.Check(schema, value) // not supported
    case 'Function':
      return TypeGuard.TFunction(schema) && Value.Check(schema, value) // not supported
    case 'Date':
      return TypeGuard.TDate(schema) && Value.Check(schema, value)
    case 'Promise':
      return TypeGuard.TPromise(schema) && Value.Check(schema, value) // not supported
    case 'Uint8Array':
      return TypeGuard.TUint8Array(schema) && Value.Check(schema, value)
    case 'Undefined':
      return TypeGuard.TUndefined(schema) && Value.Check(schema, value) // not supported
    case 'Void':
      return TypeGuard.TVoid(schema) && Value.Check(schema, value)
    default:
      return false
  }
}

export function createAjv() {
  return addFormats(new Ajv({}), [
    'date-time', 
    'time', 
    'date', 
    'email',  
    'hostname', 
    'ipv4', 
    'ipv6', 
    'uri', 
    'uri-reference', 
    'uuid',
    'uri-template', 
    'json-pointer', 
    'relative-json-pointer', 
    'regex'
  ])
  .addKeyword({ type: 'object', keyword: 'instanceOf', validate: schemaOf })
  .addKeyword({ type: 'null', keyword: 'typeOf', validate: schemaOf })
  .addKeyword('exclusiveMinimumTimestamp')
  .addKeyword('exclusiveMaximumTimestamp')
  .addKeyword('minimumTimestamp')
  .addKeyword('maximumTimestamp')
  .addKeyword('minByteLength')
  .addKeyword('maxByteLength')
}

const ajv = createAjv()

const R = ajv.validate(Type.Object({                 // const R = true
  buffer: Type.Uint8Array(),
  date: Type.Date(),
  void: Type.Void()
}), {
  buffer: new Uint8Array(),
  date: new Date(),
  void: null
})

TypeCompiler

TypeBox provides an optional high performance just-in-time (JIT) compiler and type checker that can be used in applications that require extremely fast validation. Note that this compiler is optimized for TypeBox types only where the schematics are known in advance. If defining custom types with Type.Unsafe<T> please consider Ajv.

The compiler module is provided as an optional import.

import { TypeCompiler } from '@sinclair/typebox/compiler'

Use the Compile(...) function to compile a type.

const C = TypeCompiler.Compile(Type.Object({         // const C: TypeCheck<TObject<{
  x: Type.Number(),                                  //     x: TNumber;
  y: Type.Number(),                                  //     y: TNumber;
  z: Type.Number()                                   //     z: TNumber;
}))                                                  // }>>

const R = C.Check({ x: 1, y: 2, z: 3 })              // const R = true

Validation errors can be read with the Errors(...) function.

const C = TypeCompiler.Compile(Type.Object({         // const C: TypeCheck<TObject<{
  x: Type.Number(),                                  //     x: TNumber;
  y: Type.Number(),                                  //     y: TNumber;
  z: Type.Number()                                   //     z: TNumber;
}))                                                  // }>>

const value = { }

const errors = [...C.Errors(value)]                  // const errors = [{
                                                     //   schema: { type: 'number' },
                                                     //   path: '/x',
                                                     //   value: undefined,
                                                     //   message: 'Expected number'
                                                     // }, {
                                                     //   schema: { type: 'number' },
                                                     //   path: '/y',
                                                     //   value: undefined,
                                                     //   message: 'Expected number'
                                                     // }, {
                                                     //   schema: { type: 'number' },
                                                     //   path: '/z',
                                                     //   value: undefined,
                                                     //   message: 'Expected number'
                                                     // }]

Compiled routines can be inspected with the .Code() function.

const C = TypeCompiler.Compile(Type.String())        // const C: TypeCheck<TString>

console.log(C.Code())                                // return function check(value) {
                                                     //   return (
                                                     //     (typeof value === 'string')
                                                     //   )
                                                     // }

Custom Types

Use the custom module to create user defined types. User defined types require a [Kind] symbol property which is used to match the schema against a registered type. Custom types are specific to TypeBox and can only be used with the TypeCompiler and Value modules.

The custom module is an optional import.

import { Custom } from '@sinclair/typebox/custom'

The following creates a BigInt type.

import { Type, Kind } from '@sinclair/typebox'

Custom.Set('BigInt', (schema, value) => typeof value === 'bigint')
//            ā”‚
//            ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”         The [Kind] is used to match registered type
//                                         ā”‚
const T = Type.Unsafe<bigint>({ [Kind]: 'BigInt' })  // const T = { [Kind]: 'BigInt' }

const A = TypeCompiler.Compile(T).Check(65536)       // const A = false

const B = Value.Check(T, 65536n)                     // const B = true

Custom Formats

Use the format module to create user defined string formats. If using Ajv, please refer to the official Ajv format documentation located here. The format module is specific to TypeBox can only be used with the TypeCompiler and Value modules.

The format module is an optional import.

import { Format } from '@sinclair/typebox/format'

The following creates a palindrome string format.

Format.Set('palindrome', value => value === value.split('').reverse().join(''))

Once set, this format can then be used by the TypeCompiler and Value modules.

const T = Type.String({ format: 'palindrome' })

const A = TypeCompiler.Compile(T).Check('engine')    // const A = false

const B = Value.Check(T, 'kayak')                    // const B = true

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.11.2.

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  ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚           Number ā”‚    2000    ā”‚ '    414 ms' ā”‚ '     14 ms' ā”‚ '   29.57 x' ā”‚
ā”‚           String ā”‚    2000    ā”‚ '    326 ms' ā”‚ '     12 ms' ā”‚ '   27.17 x' ā”‚
ā”‚          Boolean ā”‚    2000    ā”‚ '    301 ms' ā”‚ '     11 ms' ā”‚ '   27.36 x' ā”‚
ā”‚             Null ā”‚    2000    ā”‚ '    253 ms' ā”‚ '      8 ms' ā”‚ '   31.63 x' ā”‚
ā”‚            RegEx ā”‚    2000    ā”‚ '    480 ms' ā”‚ '     18 ms' ā”‚ '   26.67 x' ā”‚
ā”‚          ObjectA ā”‚    2000    ā”‚ '   2704 ms' ā”‚ '     53 ms' ā”‚ '   51.02 x' ā”‚
ā”‚          ObjectB ā”‚    2000    ā”‚ '   2846 ms' ā”‚ '     35 ms' ā”‚ '   81.31 x' ā”‚
ā”‚            Tuple ā”‚    2000    ā”‚ '   1244 ms' ā”‚ '     23 ms' ā”‚ '   54.09 x' ā”‚
ā”‚            Union ā”‚    2000    ā”‚ '   1222 ms' ā”‚ '     26 ms' ā”‚ '   47.00 x' ā”‚
ā”‚          Vector4 ā”‚    2000    ā”‚ '   1522 ms' ā”‚ '     22 ms' ā”‚ '   69.18 x' ā”‚
ā”‚          Matrix4 ā”‚    2000    ā”‚ '    830 ms' ā”‚ '     10 ms' ā”‚ '   83.00 x' ā”‚
ā”‚   Literal_String ā”‚    2000    ā”‚ '    348 ms' ā”‚ '      9 ms' ā”‚ '   38.67 x' ā”‚
ā”‚   Literal_Number ā”‚    2000    ā”‚ '    356 ms' ā”‚ '      9 ms' ā”‚ '   39.56 x' ā”‚
ā”‚  Literal_Boolean ā”‚    2000    ā”‚ '    353 ms' ā”‚ '      6 ms' ā”‚ '   58.83 x' ā”‚
ā”‚     Array_Number ā”‚    2000    ā”‚ '    686 ms' ā”‚ '     11 ms' ā”‚ '   62.36 x' ā”‚
ā”‚     Array_String ā”‚    2000    ā”‚ '    722 ms' ā”‚ '     11 ms' ā”‚ '   65.64 x' ā”‚
ā”‚    Array_Boolean ā”‚    2000    ā”‚ '    724 ms' ā”‚ '      9 ms' ā”‚ '   80.44 x' ā”‚
ā”‚    Array_ObjectA ā”‚    2000    ā”‚ '   3706 ms' ā”‚ '     36 ms' ā”‚ '  102.94 x' ā”‚
ā”‚    Array_ObjectB ā”‚    2000    ā”‚ '   3678 ms' ā”‚ '     38 ms' ā”‚ '   96.79 x' ā”‚
ā”‚      Array_Tuple ā”‚    2000    ā”‚ '   2217 ms' ā”‚ '     23 ms' ā”‚ '   96.39 x' ā”‚
ā”‚      Array_Union ā”‚    2000    ā”‚ '   1654 ms' ā”‚ '     24 ms' ā”‚ '   68.92 x' ā”‚
ā”‚    Array_Vector4 ā”‚    2000    ā”‚ '   2283 ms' ā”‚ '     20 ms' ā”‚ '  114.15 x' ā”‚
ā”‚    Array_Matrix4 ā”‚    2000    ā”‚ '   1567 ms' ā”‚ '     19 ms' ā”‚ '   82.47 x' ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Validate

This benchmark measures validation performance for varying types. You can review this benchmark here.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚     (index)      ā”‚ Iterations ā”‚  ValueCheck  ā”‚     Ajv      ā”‚ TypeCompiler ā”‚ Performance  ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚           Number ā”‚  1000000   ā”‚ '     33 ms' ā”‚ '      6 ms' ā”‚ '      5 ms' ā”‚ '    1.20 x' ā”‚
ā”‚           String ā”‚  1000000   ā”‚ '     26 ms' ā”‚ '     24 ms' ā”‚ '     12 ms' ā”‚ '    2.00 x' ā”‚
ā”‚          Boolean ā”‚  1000000   ā”‚ '     24 ms' ā”‚ '     22 ms' ā”‚ '     11 ms' ā”‚ '    2.00 x' ā”‚
ā”‚             Null ā”‚  1000000   ā”‚ '     28 ms' ā”‚ '     24 ms' ā”‚ '     11 ms' ā”‚ '    2.18 x' ā”‚
ā”‚            RegEx ā”‚  1000000   ā”‚ '    171 ms' ā”‚ '     56 ms' ā”‚ '     41 ms' ā”‚ '    1.37 x' ā”‚
ā”‚          ObjectA ā”‚  1000000   ā”‚ '    614 ms' ā”‚ '     42 ms' ā”‚ '     24 ms' ā”‚ '    1.75 x' ā”‚
ā”‚          ObjectB ā”‚  1000000   ā”‚ '   1109 ms' ā”‚ '     59 ms' ā”‚ '     45 ms' ā”‚ '    1.31 x' ā”‚
ā”‚            Tuple ā”‚  1000000   ā”‚ '    122 ms' ā”‚ '     25 ms' ā”‚ '     13 ms' ā”‚ '    1.92 x' ā”‚
ā”‚            Union ā”‚  1000000   ā”‚ '    316 ms' ā”‚ '     26 ms' ā”‚ '     16 ms' ā”‚ '    1.63 x' ā”‚
ā”‚        Recursive ā”‚  1000000   ā”‚ '   3219 ms' ā”‚ '    453 ms' ā”‚ '    128 ms' ā”‚ '    3.54 x' ā”‚
ā”‚          Vector4 ā”‚  1000000   ā”‚ '    145 ms' ā”‚ '     26 ms' ā”‚ '     13 ms' ā”‚ '    2.00 x' ā”‚
ā”‚          Matrix4 ā”‚  1000000   ā”‚ '    537 ms' ā”‚ '     42 ms' ā”‚ '     28 ms' ā”‚ '    1.50 x' ā”‚
ā”‚   Literal_String ā”‚  1000000   ā”‚ '     51 ms' ā”‚ '     21 ms' ā”‚ '     11 ms' ā”‚ '    1.91 x' ā”‚
ā”‚   Literal_Number ā”‚  1000000   ā”‚ '     48 ms' ā”‚ '     20 ms' ā”‚ '     11 ms' ā”‚ '    1.82 x' ā”‚
ā”‚  Literal_Boolean ā”‚  1000000   ā”‚ '     49 ms' ā”‚ '     20 ms' ā”‚ '     11 ms' ā”‚ '    1.82 x' ā”‚
ā”‚     Array_Number ā”‚  1000000   ā”‚ '    438 ms' ā”‚ '     35 ms' ā”‚ '     19 ms' ā”‚ '    1.84 x' ā”‚
ā”‚     Array_String ā”‚  1000000   ā”‚ '    468 ms' ā”‚ '     33 ms' ā”‚ '     23 ms' ā”‚ '    1.43 x' ā”‚
ā”‚    Array_Boolean ā”‚  1000000   ā”‚ '    448 ms' ā”‚ '     36 ms' ā”‚ '     29 ms' ā”‚ '    1.24 x' ā”‚
ā”‚    Array_ObjectA ā”‚  1000000   ā”‚ '  13904 ms' ā”‚ '   2536 ms' ā”‚ '   1530 ms' ā”‚ '    1.66 x' ā”‚
ā”‚    Array_ObjectB ā”‚  1000000   ā”‚ '  16188 ms' ā”‚ '   2724 ms' ā”‚ '   1834 ms' ā”‚ '    1.49 x' ā”‚
ā”‚      Array_Tuple ā”‚  1000000   ā”‚ '   1725 ms' ā”‚ '     98 ms' ā”‚ '     64 ms' ā”‚ '    1.53 x' ā”‚
ā”‚      Array_Union ā”‚  1000000   ā”‚ '   4762 ms' ā”‚ '    237 ms' ā”‚ '     87 ms' ā”‚ '    2.72 x' ā”‚
ā”‚  Array_Recursive ā”‚  1000000   ā”‚ '  54754 ms' ā”‚ '   7707 ms' ā”‚ '   1152 ms' ā”‚ '    6.69 x' ā”‚
ā”‚    Array_Vector4 ā”‚  1000000   ā”‚ '   2485 ms' ā”‚ '    100 ms' ā”‚ '     48 ms' ā”‚ '    2.08 x' ā”‚
ā”‚    Array_Matrix4 ā”‚  1000000   ā”‚ '  13116 ms' ā”‚ '    387 ms' ā”‚ '    232 ms' ā”‚ '    1.67 x' ā”‚
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Compression

The following table lists esbuild compiled and minified sizes for each TypeBox module.

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
ā”‚       (index)        ā”‚  Compiled  ā”‚  Minified  ā”‚ Compression ā”‚
ā”œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¤
ā”‚ typebox/compiler     ā”‚ '   56 kb' ā”‚ '   28 kb' ā”‚  '2.01 x'   ā”‚
ā”‚ typebox/conditional  ā”‚ '   45 kb' ā”‚ '   18 kb' ā”‚  '2.44 x'   ā”‚
ā”‚ typebox/custom       ā”‚ '    0 kb' ā”‚ '    0 kb' ā”‚  '2.61 x'   ā”‚
ā”‚ typebox/format       ā”‚ '    0 kb' ā”‚ '    0 kb' ā”‚  '2.66 x'   ā”‚
ā”‚ typebox/guard        ā”‚ '   23 kb' ā”‚ '   11 kb' ā”‚  '2.07 x'   ā”‚
ā”‚ typebox/value        ā”‚ '   80 kb' ā”‚ '   37 kb' ā”‚  '2.16 x'   ā”‚
ā”‚ typebox              ā”‚ '   12 kb' ā”‚ '    6 kb' ā”‚  '1.89 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.

Keywords

FAQs

Last updated on 25 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with āš”ļø by Socket Inc