@mdxld/typescript
Generate TypeScript types, Zod schemas, and JSON5 from objects
@mdxld/typescript
Generate TypeScript interfaces, Zod validation schemas, JSON5 configuration, and JSDoc type definitions from object schemas. All outputs are valid TypeScript/JavaScript.
Installation
Quick Start
API Reference
toTypeScript(schema, options?)
Generate TypeScript interface definitions.
Example:
Output:
toZod(schema, options?)
Generate Zod validation schema.
Example:
Output:
toJSON5(object, options?)
Convert object to JSON5 (valid JavaScript/TypeScript).
Example:
Output:
toJSDoc(schema, options?)
Generate JSDoc type definitions for plain JavaScript.
Example:
Output:
fromTypeScript(source)
Parse TypeScript interface back to schema object.
Example:
Type Mappings
Primitive Types
| Schema Type | TypeScript | Zod |
|---|---|---|
string | string | z.string() |
number | number | z.number() |
integer | number | z.number().int() |
boolean | boolean | z.boolean() |
null | null | z.null() |
any | unknown | z.unknown() |
Complex Types
| Schema Type | TypeScript | Zod |
|---|---|---|
array | T[] | z.array(T) |
object | Record<string, T> | z.record(T) |
enum | 'a' | 'b' | z.enum(['a', 'b']) |
union | A | B | z.union([A, B]) |
optional | T? | T.optional() |
Format Hints
| Format | TypeScript | Zod |
|---|---|---|
email | string | z.string().email() |
url | string | z.string().url() |
uuid | string | z.string().uuid() |
date | Date | z.coerce.date() |
JSON5 Features
JSON5 extends JSON with JavaScript-friendly features:
Since JSON5 is valid JavaScript, you can use it directly:
Use Cases
Generate Types from MDXLD
Config File Generation
API Type Generation
Runtime Validation
Related Packages
| Package | Description |
|---|---|
@mdxld/json | JSON Schema generation |
@mdxld/yaml | YAML output |
mdxld | Core MDXLD with typegen CLI |