0.10.2 • Published 11 days ago

@sinclair/typebox-codegen v0.10.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

npm version GitHub CI License

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)

// 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.

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
}
`
// ----------------------------------------------------------------------------
//
// 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))

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/

0.10.2

11 days ago

0.9.12

14 days ago

0.9.13

14 days ago

0.9.14

14 days ago

0.9.15

14 days ago

0.10.1

14 days ago

0.10.0

14 days ago

0.9.11

20 days ago

0.9.10

29 days ago

0.9.9

1 month ago

0.9.8

2 months ago

0.9.7

4 months ago

0.9.6

4 months ago

0.9.5

4 months ago

0.9.4

4 months ago

0.9.3

4 months ago

0.9.2

4 months ago

0.9.1

4 months ago

0.9.0

5 months ago

0.8.13

6 months ago

0.8.12

6 months ago

0.8.11

8 months ago

0.8.10

8 months ago

0.8.9

8 months ago

0.8.8

8 months ago

0.8.7

8 months ago

0.8.6

9 months ago

0.8.5

9 months ago

0.8.4

9 months ago

0.8.3

9 months ago

0.8.2

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago