Install
npm install @sinclair/typebox-codegen
Example
import * as Codegen from '@sinclair/typebox-codegen'
const Code = Codegen.TypeScriptToTypeBox.Generate(`
type T = { x: number, y: number, z: number }
`)
console.log(Code)
Overview
TypeBox-Codegen is a code generation tool that converts TypeScript types into TypeBox types as well as several other schema and library representations. It works by extracting structural type information from the TypeScript compiler then remaps it into a TypeBox representation (referred to as a Model). This Model can then be passed on to various code generators which generate through TypeBox schema introspection.
The library contains code transformations for libraries such as zod, io-ts, arktype and valibot, assert function generators for JavaScript and TypeScript (derived from TypeBox's TypeCompiler) as well as JSON Schema derived from TypeBox's raw schematics.
TypeBox Workbench Example
License MIT
Usage
The following is the general usage
import * as Codegen from '@sinclair/typebox-codegen'
const Code = `
export type T = {
x: number,
y: number,
z: number
}
`
console.log('TypeScript To TypeBox', Codegen.TypeScriptToTypeBox.Generate(Code))
const model = Codegen.TypeScriptToModel.Generate(Code)
console.log('TypeBoxModel', model)
console.log('Model To JsonSchema', Codegen.ModelToJsonSchema.Generate(model))
console.log('Model To JavaScript', Codegen.ModelToJavaScript.Generate(model))
console.log('Model To TypeScript', Codegen.ModelToTypeScript.Generate(model))
console.log('Model To Valibot', Codegen.ModelToValibot.Generate(model))
console.log('Model To Value', Codegen.ModelToValue.Generate(model))
console.log('Model To Yup', Codegen.ModelToYup.Generate(model))
console.log('Model To Zod', Codegen.ModelToZod.Generate(model))
console.log('Model To ArkType', Codegen.ModelToArkType.Generate(model))
Running Local
Clone the project and run the following commands.
$ npm install
$ npm format
$ npm clean
$ npm start
Formatting hook
Set up pre-commit formatting hook by running cp ./.git-hooks/pre-commit ./.git/hooks/