### Install Dependencies and Run Example Source: https://github.com/sinclairzx81/typebox-codegen/blob/main/readme.md Commands to install project dependencies, format code, clean the build directory, and run the example script locally. ```bash $ 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 ``` -------------------------------- ### Install TypeBox-Codegen Source: https://github.com/sinclairzx81/typebox-codegen/blob/main/readme.md Install the TypeBox-Codegen package using npm. ```typescript npm install @sinclair/typebox-codegen ``` -------------------------------- ### TypeBox-Codegen Usage Example Source: https://github.com/sinclairzx81/typebox-codegen/blob/main/readme.md Demonstrates various code generation capabilities of TypeBox-Codegen, including TypeScript to TypeBox, model generation, and conversion to multiple formats like JSON Schema, JavaScript, Valibot, Yup, Zod, ArkType, and Effect. ```typescript 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 // // ---------------------------------------------------------------------------- const model = Codegen.TypeScriptToModel.Generate(Code) // ---------------------------------------------------------------------------- // // ModelToX // // The TypeBox Model can be passed to several generators which map the // Model into varying type representations. // // ---------------------------------------------------------------------------- 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)) console.log('Model To Effect', Codegen.ModelToEffect.Generate(model)) ``` -------------------------------- ### Set up Pre-commit Formatting Hook Source: https://github.com/sinclairzx81/typebox-codegen/blob/main/readme.md Instructions to copy the pre-commit hook script to the local Git hooks directory for automatic code formatting. ```bash cp ./.git-hooks/pre-commit ./.git/hooks/ ``` -------------------------------- ### Configure TypeBox Code Generation with Options Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Customize the generated TypeBox code using options such as exporting all types, omitting the TypeBox import, or including $id identifiers. These options control the structure and content of the output. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` type Point = { x: number, y: number } type Line = { start: Point, end: Point } ` // Option: Export all types (useful for model generation) const withExports = Codegen.TypeScriptToTypeBox.Generate(Code, { useExportEverything: true }) // Option: Skip TypeBox import (for embedding in existing code) const withoutImport = Codegen.TypeScriptToTypeBox.Generate(Code, { useTypeBoxImport: false }) // Option: Include $id identifiers in schemas const withIdentifiers = Codegen.TypeScriptToTypeBox.Generate(Code, { useIdentifiers: true }) console.log(withIdentifiers) ``` -------------------------------- ### Full Code Generation Pipeline Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Use this pipeline to convert TypeScript types to TypeBox, then to an intermediate model, and finally to multiple target formats like Zod, Valibot, ArkType, Effect, JSON Schema, TypeScript with Guards, JavaScript Validators, and Default Values. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const TypeScriptCode = ` export type Vector3 = { x: number y: number z: number } export type Transform = { position: Vector3 rotation: Vector3 scale: Vector3 } export type Entity = { id: string name: string transform: Transform active: boolean tags: string[] } ` // Step 1: Direct TypeScript to TypeBox (one-step) const typeboxDirect = Codegen.TypeScriptToTypeBox.Generate(TypeScriptCode) console.log('=== TypeBox ===') console.log(typeboxDirect) // Step 2: Create intermediate model for multi-target generation const model = Codegen.TypeScriptToModel.Generate(TypeScriptCode) // Step 3: Generate for multiple targets console.log('=== Zod ===') console.log(Codegen.ModelToZod.Generate(model)) console.log('=== Valibot ===') console.log(Codegen.ModelToValibot.Generate(model)) console.log('=== ArkType ===') console.log(Codegen.ModelToArkType.Generate(model)) console.log('=== Effect ===') console.log(Codegen.ModelToEffect.Generate(model)) console.log('=== JSON Schema ===') console.log(Codegen.ModelToJsonSchema.Generate(model)) console.log('=== TypeScript with Guards ===') console.log(Codegen.ModelToTypeScript.Generate(model)) console.log('=== JavaScript Validators ===') console.log(Codegen.ModelToJavaScript.Generate(model)) console.log('=== Default Values ===') console.log(Codegen.ModelToValue.Generate(model)) ``` -------------------------------- ### Generate TypeBox from TypeScript Source: https://github.com/sinclairzx81/typebox-codegen/blob/main/readme.md Use TypeScriptToTypeBox.Generate to convert a TypeScript type definition string into a TypeBox compatible code string. ```typescript 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 // const T = Type.Object({ // x: Type.Number(), // y: Type.Number(), // z: Type.Number() // }) ``` -------------------------------- ### Generate ArkType Schema from TypeBox Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Converts a TypeBox model into ArkType schema definitions. Generates fully typed schemas with proper constraint handling using scopes and type expressions. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Coordinates = { latitude: number longitude: number } export type Location = { name: string coords: Coordinates elevation?: number } ` const model = Codegen.TypeScriptToModel.Generate(Code) const arktypeCode = Codegen.ModelToArkType.Generate(model) console.log(arktypeCode) ``` -------------------------------- ### Generate Zod Schema from TypeBox Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Transforms a TypeBox model into Zod schema definitions. Handles common TypeBox types and provides full type inference support. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Address = { street: string city: string zipCode: string country: string } export type Person = { name: string age: number email?: string addresses: Address[] } ` const model = Codegen.TypeScriptToModel.Generate(Code) const zodCode = Codegen.ModelToZod.Generate(model) console.log(zodCode) ``` -------------------------------- ### Generate Default Values from TypeBox Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Generates JavaScript objects with default values based on a TypeBox model. This uses TypeBox's Value.Create to produce instances matching the schema structure. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type FormData = { firstName: string lastName: string age: number active: boolean } ` const model = Codegen.TypeScriptToModel.Generate(Code) const valueCode = Codegen.ModelToValue.Generate(model) console.log(valueCode) ``` -------------------------------- ### Generate Valibot Schema from TypeBox Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Generates Valibot schema code from a TypeBox model. Supports configuration for exactOptionalPropertyTypes handling. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Config = { apiKey: string timeout?: number retries: number debug: boolean } export type Settings = { theme: 'light' | 'dark' language: string notifications?: boolean } ` const model = Codegen.TypeScriptToModel.Generate(Code) // Standard generation const valibotCode = Codegen.ModelToValibot.Generate(model) // With exactOptionalPropertyTypes enabled const exactCode = Codegen.ModelToValibot.Generate(model, { exactOptionalPropertyTypes: true }) console.log(valibotCode) ``` -------------------------------- ### Generate Effect Schema from TypeBox Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Generates Effect Schema definitions from a TypeBox model, creating schemas compatible with the @effect/schema library with proper type inference. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Task = { id: string title: string completed: boolean priority: number dueDate?: string } export type TaskList = { name: string tasks: Task[] } ` const model = Codegen.TypeScriptToModel.Generate(Code) const effectCode = Codegen.ModelToEffect.Generate(model) console.log(effectCode) ``` -------------------------------- ### Format Code with Prettier Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Formats generated code using Prettier with TypeScript parsing. This is an internal utility for ensuring clean and readable output. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const uglyCode = `const x={a:1,b:2,c:{d:3,e:4}}` const formatted = Codegen.Formatter.Format(uglyCode) console.log(formatted) ``` -------------------------------- ### Convert TypeBox Model to JSON Schema Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Use this to convert a TypeBox model into a standard JSON Schema object. The output is an exportable JavaScript constant. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Event = { id: string name: string timestamp: number metadata: { [key: string]: any } } ` const model = Codegen.TypeScriptToModel.Generate(Code) const jsonSchemaCode = Codegen.ModelToJsonSchema.Generate(model) console.log(jsonSchemaCode) ``` -------------------------------- ### Generate TypeBox Model from TypeScript Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Convert TypeScript types into an in-memory TypeBox model. This model serves as an intermediate representation that can be passed to various code generators for different target formats. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Product = { id: string name: string price: number inStock: boolean tags: string[] } export type Order = { orderId: string products: Product[] total: number status: 'pending' | 'shipped' | 'delivered' } ` // Generate the TypeBox model const model = Codegen.TypeScriptToModel.Generate(Code) // The model contains an array of TypeBox schemas console.log(model.types.length) // 2 console.log(model.types[0].$id) // 'Product' console.log(model.types[1].$id) // 'Order' // Model can now be passed to any ModelToX generator const zodCode = Codegen.ModelToZod.Generate(model) const valibotCode = Codegen.ModelToValibot.Generate(model) const jsonSchema = Codegen.ModelToJsonSchema.Generate(model) ``` -------------------------------- ### Generate TypeBox Code from TypeScript Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Use this function to directly generate TypeBox type definitions from TypeScript interface and type declarations. Ensure the necessary TypeBox compiler and codegen libraries are imported. ```typescript import * as Codegen from '@sinclair/typebox-codegen' // Define TypeScript types const Code = ` export type User = { id: number name: string email: string age?: number readonly createdAt: Date } export type ApiResponse = { data: T success: boolean error?: string } ` // Generate TypeBox code const typeboxCode = Codegen.TypeScriptToTypeBox.Generate(Code) console.log(typeboxCode) ``` -------------------------------- ### Generate TypeScript Types and Guards from Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Generates TypeScript type aliases and runtime type guard functions from a TypeBox model. This is useful for ensuring type safety in TypeScript applications. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type User = { id: string username: string role: 'admin' | 'user' | 'guest' } ` const model = Codegen.TypeScriptToModel.Generate(Code) const tsCode = Codegen.ModelToTypeScript.Generate(model) console.log(tsCode) ``` -------------------------------- ### Generate JavaScript Validation Functions from Model Source: https://context7.com/sinclairzx81/typebox-codegen/llms.txt Creates optimized JavaScript validation functions from a TypeBox model. These validators do not include TypeScript type annotations and are suitable for plain JavaScript environments. ```typescript import * as Codegen from '@sinclair/typebox-codegen' const Code = ` export type Payment = { amount: number currency: string processed: boolean } ` const model = Codegen.TypeScriptToModel.Generate(Code) const jsCode = Codegen.ModelToJavaScript.Generate(model) console.log(jsCode) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.