@typespec-dev/emitter-typebox
v0.3.0
Published
A TypeSpec emitter and library for creating TypeBox schemas from TypeSpec types.
Maintainers
Readme
@typespec-dev/emitter-typebox
A TypeSpec emitter that generates TypeBox schemas from TypeSpec type definitions.
Install
pnpm add @typespec-dev/emitter-typeboxPeer dependencies
@typespec/[email protected]@typespec/[email protected]
Configuration
Add the emitter to your tspconfig.yaml:
emit:
- "@typespec-dev/emitter-typebox"Example
Given a TypeSpec definition:
model User {
name: string;
age?: int32;
tags: string[];
}The emitter produces:
import { type Static, Type } from "typebox";
export const user = Type.Object({
name: Type.String(),
age: Type.Optional(Type.Integer()),
tags: Type.Array(Type.String()),
});
export type User = Static<typeof user>;Supported Types
- Scalars (string, boolean, integers, floats, decimals, bigint, url, bytes, dates, durations)
- Literal types (string, number, boolean)
- Intrinsic types (null, never, unknown, void)
- Models (
Type.Objectwith optional properties, nested references) - Enums (union of literals)
- Unions, Arrays, Records, Tuples
- Nullable, Optional wrappers
- Constraints (minLength, maxLength, minimum, maximum, pattern, format, minItems, maxItems)
- Descriptions (
@docannotation) - Default values (
@defaultannotation)
Development
pnpm build # build the emitter
pnpm test # run tests
pnpm lint # lint
pnpm type # type-check