Gathering detailed insights and metrics for @sinclair/typebox-codegen
Gathering detailed insights and metrics for @sinclair/typebox-codegen
Gathering detailed insights and metrics for @sinclair/typebox-codegen
Gathering detailed insights and metrics for @sinclair/typebox-codegen
npm install @sinclair/typebox-codegen
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
140 Stars
80 Commits
12 Forks
4 Watching
8 Branches
6 Contributors
Updated on 27 Nov 2024
TypeScript (98.08%)
JavaScript (1.57%)
Shell (0.35%)
Cumulative downloads
Total Downloads
Last day
-6.2%
1,373
Compared to previous day
Last week
8.7%
7,616
Compared to previous week
Last month
7.4%
33,732
Compared to previous month
Last year
3,253.2%
415,465
Compared to previous year
3
3
1npm install @sinclair/typebox-codegen
1import * as Codegen from '@sinclair/typebox-codegen' 2 3const Code = Codegen.TypeScriptToTypeBox.Generate(` 4 type T = { x: number, y: number, z: number } 5`) 6 7console.log(Code) 8 9// Output: 10// 11// import { Type, Static } from '@sinclair/typebox' 12// 13// type T = Static<typeof T> 14// const T = Type.Object({ 15// x: Type.Number(), 16// y: Type.Number(), 17// z: Type.Number() 18// })
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.
License MIT
The following is the general usage
1import * as Codegen from '@sinclair/typebox-codegen' 2 3const Code = ` 4export type T = { 5 x: number, 6 y: number, 7 z: number 8} 9` 10// ---------------------------------------------------------------------------- 11// 12// TypeScriptToTypeBox 13// 14// Generates an immediate TypeScript to TypeBox type code transformation 15// 16// ---------------------------------------------------------------------------- 17 18console.log('TypeScript To TypeBox', Codegen.TypeScriptToTypeBox.Generate(Code)) 19 20// ---------------------------------------------------------------------------- 21// 22// TypeScriptToModel 23// 24// Generates an in-memory TypeBox Model 25// 26// ---------------------------------------------------------------------------- 27 28const model = Codegen.TypeScriptToModel.Generate(Code) 29 30// ---------------------------------------------------------------------------- 31// 32// ModelToX 33// 34// The TypeBox Model can be passed to several generators which map the 35// Model into varying type representations. 36// 37// ---------------------------------------------------------------------------- 38 39console.log('TypeBoxModel', model) 40console.log('Model To JsonSchema', Codegen.ModelToJsonSchema.Generate(model)) 41console.log('Model To JavaScript', Codegen.ModelToJavaScript.Generate(model)) 42console.log('Model To TypeScript', Codegen.ModelToTypeScript.Generate(model)) 43console.log('Model To Valibot', Codegen.ModelToValibot.Generate(model)) 44console.log('Model To Value', Codegen.ModelToValue.Generate(model)) 45console.log('Model To Yup', Codegen.ModelToYup.Generate(model)) 46console.log('Model To Zod', Codegen.ModelToZod.Generate(model)) 47console.log('Model To ArkType', Codegen.ModelToArkType.Generate(model)) 48console.log('Model To Effect', Codegen.ModelToEffect.Generate(model))
Clone the project and run the following commands.
1$ npm install # install dependencies 2 3$ npm format # prettier pass for `src` and `example` 4 5$ npm clean # remove the `target` directory. 6 7$ npm start # run the `example` script in node
Set up pre-commit formatting hook by running cp ./.git-hooks/pre-commit ./.git/hooks/
No vulnerabilities found.
No security vulnerabilities found.