Install
$ npm install @sinclair/typemap --save
Example
Use TypeScript syntax to construct types for TypeBox, Valibot and Zod ...
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
// const T: TObject<{ ... }>
const T = TypeBox(`{
x: number,
y: number,
z: number
}`)
// const V: ObjectSchema<{ ... }>
const V = Valibot(`{
x: number,
y: number,
z: number
}`)
// const Z: ZodObject<{ ... }>
const Z = Zod(`{
x: number,
y: number,
z: number
}`)
... or structurally remap types from one library to another
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
// Syntax > Zod > Valibot > TypeBox
const T = TypeBox(Valibot(Zod(`{
x: number,
y: number,
z: number
}`)))
... or compile types for high performance runtime type checking
import { Compile } from '@sinclair/typemap'
import z from 'zod'
const T = z.object({ // const T: z.ZodObject<{
x: z.number(), // x: z.ZodNumber,
y: z.number(), // y: z.ZodNumber,
z: z.number(), // z: z.ZodNumber,
}) // }>
const C = Compile(T) // const C: Validator<TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>>
const R = C.Check({ // const R: boolean - High Performance Checking!
x: 1,
y: 2,
z: 3
})
Overview
TypeMap is a type mapping library developed for the TypeBox, Valibot and Zod runtime type libraries. It is designed to make these libraries cross compatible by structurally remapping types from one library to another. TypeMap also provides a uniform frontend syntax for creating types, as well as uniform type compilation for high-performance validation via the TypeBox compiler infrastructure.
TypeMap is built to be a simple tool to let Valibot and Zod to integrate with TypeBox and Json Schema infrastructure. It is also written to enable TypeBox to integrate with systems using Valibot and Zod for validation.
License: MIT
Contents
Libraries
TypeMap exports mapping functions named after the library they map for. Each function can accept a type from any other library, where the function will attempt to map the type or return a never representation if a mapping is not possible.
TypeBox
Use the TypeBox function to map the parameter into a TypeBox type
import { TypeBox } from '@sinclair/typemap'
const A = TypeBox(t.Number()) // const A: TNumber (TypeBox)
const B = TypeBox(v.string()) // const B: TString (Valibot)
const C = TypeBox(z.boolean()) // const C: TBoolean (Zod)
const D = TypeBox('string[]') // const D: TArray<TString> (Syntax)
Valibot
Use the Valibot function to map the parameter into a Valibot type
import { Valibot } from '@sinclair/typemap'
const A = Valibot(t.Number()) // const A: v.NumberSchema (TypeBox)
const B = Valibot(v.string()) // const B: v.StringSchema (Valibot)
const C = Valibot(z.boolean()) // const C: v.BooleanSchema (Zod)
const D = Valibot('string[]') // const D: v.ArraySchema<...> (Syntax)
Zod
Use the Zod function to map the parameter into a Zod type
import { Zod } from '@sinclair/typemap'
const A = Zod(t.Number()) // const A: z.ZodNumber (TypeBox)
const B = Zod(v.string()) // const B: z.ZodString (Valibot)
const C = Zod(z.boolean()) // const C: z.ZodBoolean (Zod)
const D = Zod('string[]') // const D: z.ZodArray<...> (Syntax)
Static
TypeMap can statically infer for TypeBox, Valibot, Zod and Syntax with the Static type.
import { type Static } from '@sinclair/typemap'
const T = t.Number() // TypeBox
const V = v.string() // Valibot
const Z = z.boolean() // Zod
const S = 'string[]' // Syntax
type T = Static<typeof T> // number
type V = Static<typeof V> // string
type Z = Static<typeof Z> // boolean
type S = Static<typeof S> // string[]
Compile
TypeMap offers JIT compilation of TypeBox, Valibot, Zod and Syntax using the Compile function. This function will internally use the TypeBox TypeCompiler for high performance checking. This function will also gracefully degrade to dynamic checking if the runtime does not support JavaScript evaluation.
The Compile function returns a validator object that implements the standard-schema interface.
import { Compile } from '@sinclair/typemap'
// Pass TypeBox, Valibot, Zod or Syntax to JIT Compile the type.
const V = Compile(`{
x: number
y: number,
z: number
}`)
// TypeMap Interface
const R1 = V.Check({ x: 1, y: 2, z: 3 })
// Standard Schema Interface
const R2 = V['~standard'].validate({ x: 1, y: 2, z: 3 })
Benchmark
This project manages a benchmark that evaluates type-check performance using Zod, Valibot, and TypeBox validators. The benchmark is set up to run 10 million check operations per library-validator pairing and reports the elapsed time taken to complete.
Type
Benchmarks are run for the following type.
type T = { x: number, y: string, z: boolean }
Results
Results show validate performance for the type.
┌─────────┬────────────────┬────────────────────┬────────────┬────────────┐
│ (index) │ library │ using │ iterations │ elapsed │
├─────────┼────────────────┼────────────────────┼────────────┼────────────┤
│ 0 │ 'valibot ' │ 'valibot ' │ 10000000 │ '1534 ms ' │
│ 1 │ 'valibot ' │ 'typebox:value ' │ 10000000 │ '1377 ms ' │
│ 2 │ 'valibot ' │ 'typebox:compile ' │ 10000000 │ '46 ms ' │
└─────────┴────────────────┴────────────────────┴────────────┴────────────┘
┌─────────┬────────────────┬────────────────────┬────────────┬────────────┐
│ (index) │ library │ using │ iterations │ elapsed │
├─────────┼────────────────┼────────────────────┼────────────┼────────────┤
│ 0 │ 'zod ' │ 'zod ' │ 10000000 │ '4669 ms ' │
│ 1 │ 'zod ' │ 'typebox:value ' │ 10000000 │ '1359 ms ' │
│ 2 │ 'zod ' │ 'typebox:compile ' │ 10000000 │ '47 ms ' │
└─────────┴────────────────┴────────────────────┴────────────┴────────────┘
For community benchmarks, refer to the runtime-type-benchmarks project.
Contribute
This project is open to community contributions. Please ensure you submit an open issue before creating a pull request. TypeMap encourages open community discussion before accepting new features.
