TypeDriver is a runtime type system that compiles TypeScript, JSON Schema and Standard Schema definitions into high performance JSON Schema validators. It also statically emulates the TypeScript type system inside the TypeScript type system which enables it to infer types from strings.
The compile function will accept any TypeScript, JSON Schema or Standard Schema definition and JIT compile it into a high-performance runtime validator. If the JavaScript environment does not support JIT (i.e. Cloudflare), the compiler will automatically fallback to dynamic validation.
Data constraints and annotations can be specified via a with keyword. TypeDriver natively understands JSON Schema vocabulary. The following constrains a Vector type.
TypeDriver has unified type inference for TypeScript, JSON Schema, and Standard Schema. Use the Static<T> type to derive a static TypeScript type from any runtime type definition.
TypeScript
import { type Static } from 'typedriver'type T = Static<`{ value: number }`> // type T = { // value: number // }
JSON Schema
import { type Static } from 'typedriver'type T = Static<{ // type T = { type: 'object', // value: number required: ['value'], // } properties: { value: { type: 'number' } }}>
Standard Schema
import { type Static } from 'typedriver'const T = z.object({ value: z.number() })type T = Static<typeof T> // type T = { // value: number // }
Reflect
TypeDriver uses JSON Schema to represent TypeScript at runtime. Compiled validators include a toJsonSchema() function that returns the internal JSON Schema representation.
import compile from 'typedriver'const validator = compile(`{ x: number }`)validator.isJsonSchema() // Returns true if the compiled type // supports JSON Schema transform. This // is true for TypeScript, JSON Schema, // and library implementations of // Standard JSON Schema.validator.toJsonSchema() // Returns JSON Schema or an empty // schema {} if not supported.
The original type used for compilation can also be retrieved via:
validator.toType() // Returns the original type or // schema used for compilation.
TypeDriver is designed for framework integration. The following is a starter script for a runtime and static type-safe function that is able to accept any TypeScript, JSON Schema or Standard Schema definition.
import compile, { type Static } from 'typedriver'// Callbackexport type TCallback< Input extends unknown, Output extends unknown> = (value: Input) => Output// Optionsexport type TOptions< Input extends unknown = unknown, Output extends unknown = unknown> = { input?: Input, output?: Output}// TypedFunctionexport function TypedFunction<const Options extends TOptions, Input extends unknown = Static<Options['input']>, Output extends unknown = Static<Options['output']>, Callback extends TCallback<Input, Output> = TCallback<Input, Output>>(options: Options, callback: Callback): Callback { const input = compile(options['input']) const output = compile(options['output']) return ( (value: unknown) => output.parse(callback(input.parse(value) as never)) ) as never}// Exampleconst add = TypedFunction({ input: `{ a: number, b: number }`, output: `number`}, ({ a, b }) => a + b)const sum = add({ a: 1, b: 2 }) // const value = 3
Benchmark
TypeDriver is built for performance and to be as lean as possible while hosting a TypeScript DSL engine and JSON Schema 2020-12 validation compiler. The following are high level metrics for the library.
Performance
The following table shows library throughput with and without TypeDriver acceleration.
TypeDriver is open to community contribution. Please ensure you submit an issue before submitting a pull request. This project prefers open community discussion before accepting new features.