import * as Codegen from '@sinclair/typebox-codegen'const Code = Codegen.TypeScriptToTypeBox.Generate(` type T = { x: number, y: number, z: number }`)console.log(Code)// Output://// import { Type, Static } from '@sinclair/typebox'// // type T = Static<typeof T>// const T = Type.Object({// x: Type.Number(),// y: Type.Number(),// z: Type.Number()// })
Overview
TypeBox-Codegen is a code generation tool that transforms TypeScript types into TypeBox types as well as several other schema and library representations. It works by mapping structural type information from the TypeScript compiler into a TypeBox model. This model is then passed on to code generators which generate via TypeBox schema introspection.
The library contains code transformations for libraries such as zod, effect, arktype, io-ts and valibot, assertion generators for JavaScript and TypeScript as well as Json Schema derived from TypeBox's raw schematics.
import * as Codegen from '@sinclair/typebox-codegen'const Code = `export type T = { x: number, y: number, z: number}`// ----------------------------------------------------------------------------//// TypeScriptToTypeBox//// Generates an immediate TypeScript to TypeBox type code transformation//// ----------------------------------------------------------------------------console.log('TypeScript To TypeBox', Codegen.TypeScriptToTypeBox.Generate(Code))// ----------------------------------------------------------------------------//// TypeScriptToModel//// Generates an in-memory TypeBox Model//// ----------------------------------------------------------------------------
Running Local
Clone the project and run the following commands.
$ npm install # install dependencies$ npm format # prettier pass for `src` and `example`$ npm clean # remove the `target` directory.$ npm start # run the `example` script in node
Formatting hook
Set up pre-commit formatting hook by running cp ./.git-hooks/pre-commit ./.git/hooks/