Typemap
Syntax Compiler and Translation System for Runtime Types
Install / Use
/learn @sinclairzx81/TypemapREADME
Install
$ npm install @sinclair/typemap --save
Usage
Parse and Compile Types from TypeScript Syntax (Example)
import { Compile } from '@sinclair/typemap'
// Compile Types With Syntax
const validator = Compile(`{ a: string, b: string }`) // const validator: Validator<TObject<{
// a: TString,
// b: TString
// }>>
// Validate with Standard Schema
const result = validator['~standard'].validate({ // const result: StandardSchemaV1.Result<{
a: 'hello', // a: string,
b: 'world' // b: string
}) // }>
Overview
TypeMap is a syntax frontend and compiler backend for the TypeBox, Valibot and Zod runtime type libraries. It offers a common TypeScript syntax for type construction, a runtime compiler for high-performance validation and type translation from one library to another.
TypeMap is written as an advanced type mapping system for the TypeBox project. It is designed to accelerate remote type libraries on TypeBox infrastructure as well enabling TypeBox to integrate with remote type library infrastructure via reverse type remapping. This project also offers high-performance validation for frameworks that orientate around the Standard Schema TypeScript interface.
License: MIT
Contents
- Install
- Usage
- Overview
- Example
- Mapping
- Syntax
- Static
- Json Schema
- Tree Shake
- Compile
- Benchmark
- Contribute
Example
Use TypeScript syntax to create types for TypeBox, Valibot and Zod (Example)
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
// Syntax Types
const S = `{
x: number,
y: number,
z: number
}`
// Runtime Types
const T = TypeBox(S) // const T: TObject<{ ... }>
const V = Valibot(S) // const V: ObjectSchema<{ ... }, ...>
const Z = Zod(S) // const Z: ZodObject<{ ... }, ...>
Translate TypeBox, Valibot and Zod types (Example)
import { TypeBox, Valibot, Zod } from '@sinclair/typemap'
// Syntax -> Zod -> Valibot -> TypeBox
const T = TypeBox(Valibot(Zod(`{
x: number,
y: number,
z: number
}`)))
Compile Valibot and Zod types on TypeBox infrastructure (Example)
import { Compile } from '@sinclair/typemap'
import z from 'zod'
// Zod Type
const Z = z.object({ // const Z: ZodObject<{
x: z.number(), // x: ZodNumber,
y: z.number(), // y: ZodNumber,
z: z.number(), // z: ZodNumber
}) // }>
// Remap and Compile
const C = Compile(Z) // const C: Validator<TObject<{
// x: TNumber,
// y: TNumber,
// z: TNumber
// }>>
// High Throughout Validation
const R = C.Check({ // Iterations: 10_000_000
x: 1, //
y: 2, // Zod : 4000ms (approx)
z: 3 // TypeMap : 40ms (approx)
})
<a name="Section-Mapping"></a>
Mapping
TypeMap is designed for runtime type translation. It provides one mapping function per library which can be used to translate remote types into types specific to that library. All mapping functions make a best effort attempt to translate the structures and semantics from each library. If no translation is possible, these functions return a never representation.
<a name="Mapping-Syntax"></a>
Syntax
Use the Syntax function to translate types into a Syntax string (Example)
import { Syntax } from '@sinclair/typemap'
const S = Syntax('string[]') // const S: 'string[]' (Syntax)
const T = Syntax(t.Number()) // const T: 'number' (TypeBox)
const V = Syntax(v.string()) // const V: 'string' (Valibot)
const Z = Syntax(z.boolean()) // const Z: 'boolean' (Zod)
<a name="Mapping-TypeBox"></a>
TypeBox
Use the TypeBox function to translate types and syntax into TypeBox types (Example)
import { TypeBox } from '@sinclair/typemap'
const S = TypeBox('string[]') // const S: TArray<TString> (Syntax)
const T = TypeBox(t.Number()) // const T: TNumber (TypeBox)
const V = TypeBox(v.string()) // const V: TString (Valibot)
const Z = TypeBox(z.boolean()) // const Z: TBoolean (Zod)
<a name="Mapping-Valibot"></a>
Valibot
Use the Valibot function to translate types and syntax into Valibot types (Example)
import { Valibot } from '@sinclair/typemap'
const S = Valibot('string[]') // const S: v.ArraySchema<...> (Syntax)
const T = Valibot(t.Number()) // const T: v.Number
