DevTools Logo

JSON Schema to TypeScript

JSON Schema to TypeScript

Compile JSON Schema into TypeScript interfaces. $ref, allOf, anyOf, oneOf, enum and tuples are handled; unsupported keywords are listed.

Runs in your browser
Try an example

JSON Schema

TypeScript

Paste a JSON Schema to get TypeScript

Results update as you type. Nothing leaves your browser.

Examples

Required vs optional with a union

Input
{ "type": "object", "required": ["id"], "properties": { "id": { "type": "integer" }, "value": { "anyOf": [{ "type": "string" }, { "type": "number" }] } } }
Output
export interface Root {
  id: number;
  value?: string | number;
}

Listed required keys lose the ?, unlisted keys gain it, and anyOf branches join with |.

Cyclic $ref plus a string map

Input
{ "type": "object", "properties": { "node": { "$ref": "#/$defs/Node" } }, "additionalProperties": { "type": "string" }, "$defs": { "Node": { "type": "object", "required": ["next"], "properties": { "next": { "$ref": "#/$defs/Node" } } } } }
Output
export interface Node {
  next: Node;
}

[key: string]: string;

Node emits exactly once with next: Node, and additionalProperties becomes an index signature.

About this tool

JSON Schema to TypeScript compiles a JSON Schema object into TypeScript interfaces with compileJsonSchemaToTypescript, defaulting the root to export interface Root. Object properties are emitted in sorted key order with required entries as name: T and everything else as name?: T, descriptions become /** */ doc comments, and integer maps to number. Unions come from anyOf/oneOf (joined with |), string|number style type arrays, enum string literals, and const values, while arrays render as T[] or Array<A | B> when the item type is itself a union.

References resolve through $defs and definitions: $ref values shaped like #/$defs/Name or #/definitions/Name (with ~0/~1 unescaping) emit one interface per target name, and # points back at the root. Cyclic graphs terminate because emit tracks an active set — a self-referencing Node with required next: $ref Node renders once as next: Node rather than expanding forever. Plain objects without an explicit type still emit interfaces when they carry properties or $defs, while additionalProperties: true yields [key: string]: unknown and an object schema value yields the resolved value type.

Validation runs before emission and fails loudly: non-object input throws Schema must be a JSON object, compileJsonSchemaJson throws Schema is not valid JSON on unparseable text, unknown type strings throw Invalid schema: unsupported type at <path>, and malformed properties, required, anyOf/oneOf, or $defs entries report their JSON-pointer path. Identifier sanitizing strips non-identifier characters and prefixes digit-leading names, so output is deterministic regardless of property insertion order.

How to use this tool

  1. Paste a schema

    Supply a JSON object schema with type, properties, required, and optionally $defs. Click compile or call compileJsonSchemaToTypescript(schema) to get the Root interface plus one interface per definition.

  2. Model unions and maps

    Use anyOf/oneOf for unions, enum for literal unions, type arrays for nullable shapes, and additionalProperties for string-keyed maps that render as [key: string]: T.

  3. Wire up $refs

    Point $ref at #/$defs/Name, #/definitions/Name, or # for the root. Cyclic refs are safe: the target renders once and referrers use its alias name.

  4. Read the errors

    Unsupported types, non-object properties, and bad required/anyOf shapes throw with a path (e.g. $.address). Fix the reported node instead of guessing.

Use cases

Typing API responses

Turn an endpoint's JSON Schema into Root plus named definition interfaces for request/response payloads.

Modeling recursive data

Express trees and linked structures with self-referencing $defs entries that compile to alias references instead of infinite nesting.

Documenting config shapes

Convert hand-written schemas with descriptions, enums, and string maps into commented interfaces for config files.

Common mistakes

Mistake:Expecting optional fields to be required.

Fix:Only keys listed in required lose the ?. Add the key to required when the field must always be present.

Mistake:Passing a JSON string to compileJsonSchemaToTypescript.

Fix:That function takes an object; pass raw text to compileJsonSchemaJson instead, which parses then compiles and throws Schema is not valid JSON on bad input.

Mistake:Using an unsupported type name.

Fix:Stick to array, boolean, integer, null, number, object, string. Anything else throws Invalid schema: unsupported type at the offending path.

Frequently asked questions

What does this tool do?

It compiles a JSON Schema object into TypeScript interfaces with compileJsonSchemaToTypescript (raw text via compileJsonSchemaJson): sorted properties, required keys without ? and optional keys with ?, anyOf/oneOf/enum/const unions, arrays, $ref aliases from $defs/definitions, and additionalProperties index signatures.

How do cyclic $refs, unions, and maps render?

A self-referencing $defs/Node entry emits once with next: Node instead of recursing; anyOf string|number renders value?: string | number; additionalProperties string renders [key: string]: string; and keys absent from required always gain ? (e.g. nickname?: string alongside id: number).

Is my schema uploaded or stored?

No. Compilation, $ref resolution, and validation are pure client-side computation — schemas never leave your browser.

Why did compilation throw?

Non-object input throws Schema must be a JSON object, bad JSON throws Schema is not valid JSON, and unknown types or malformed properties/required/anyOf nodes throw Invalid schema errors naming the $ path — fix the reported node.

References & standards