How to use the json schema to typescript
- Paste your JSON Schema into the text area. It can be a full schema or just a fragment.
- Set the root interface name to control what the top-level type is called.
- Toggle Add export keyword if you need the output ready for a module.
- The TypeScript output updates live as you type, including any referenced definitions.
How the json schema to typescript conversion works
The tool walks the JSON Schema tree and maps each node to a TypeScript type. Primitive schemas like {"type": "string"} become string. Objects become interfaces where every key in properties is mapped to its schema type, and keys absent from the required array are marked optional with a question mark.
For example, given a schema with type: "object", properties id (integer) and name (string), and required: ["id"], the converter produces an interface named after your root setting. The id field becomes id: number; while name becomes name?: string; because it is not listed as required.
Arrays use the items keyword to determine the element type. If items is missing, the element type defaults to unknown. Nested objects become inline object types, and fixed-length arrays written with prefixItems become tuples. A description in the schema becomes a JSDoc comment above the field.
Supported keywords reference
This json schema to ts converter handles the keywords below, which cover most schemas written for Draft-07 and later:
| Keyword | TypeScript result |
|---|---|
type: "string" | string |
type: "number" / "integer" | number |
type: "boolean" | boolean |
type: "null" | null |
type: "array" | T[] |
type: "object" | interface |
enum | Union of literal values |
const | Single literal value |
oneOf / anyOf | Union (|) |
allOf | Intersection (&) |
$ref | Reference to named definition |
Keywords like pattern, minimum and format do not have direct TypeScript equivalents, so they are ignored during generation but remain valid in the source schema.
Resolving local $ref pointers
A major benefit of using a dedicated ts json schema generator instead of manual typing is automatic reference resolution. When the schema contains a definitions or $defs block, each entry is emitted as its own type alias or interface, named from the key in PascalCase.
Local references such as {"$ref": "#/definitions/Address"} are replaced by the name of the matching type. A reference to # points back to the root type, and a reference such as #/properties/id is expanded in place. Circular references work because TypeScript interfaces can refer to themselves.
External references (URLs or file paths) are not resolved because the tool works only on the text you paste and has no network access. They become unknown.
When to use this instead of other approaches
If you already have a JSON response rather than a schema, a json to typescript inference tool may be more convenient. However, inference guesses types from data, while a schema explicitly declares them. Generating types from a schema guarantees that optionality, unions and constraints match the contract exactly.
Once you have the TypeScript interfaces, you might need to ship plain JavaScript. In that case, pair this tool with a typescript to javascript transpiler step in your build pipeline. You can also validate incoming payloads against the original schema using a json schema validator before passing them to typed functions.
For quick formatting of raw schema files, run them through a json formatter first so the input is easier to read and debug.
Common edge cases
Several patterns trip up naive converters. This json schema to typescript tool accounts for them:
- Missing type keyword: A schema with only
propertiesis treated as an object. - Multiple types:
{"type": ["string", "null"]}becomesstring | null. - Empty schema:
{}accepts anything, so it maps tounknown. - Definitions: Each definition is emitted before the root type, in the order they appear in the schema.
- Additional properties:
additionalProperties: trueor a schema adds an index signature to the object. When the keyword is missing orfalse, no index signature is added, which gives the strict shape most APIs document.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple object | JSON Schema: {"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name"]}; Root interface name: Person; Add export keyword: yes | export interface Person { / name: string; / age?: number; / } |
| Array of strings | JSON Schema: {"type":"array","items":{"type":"string"}}; Root interface name: Tags; Add export keyword: no | type Tags = string[]; |
| Enum values | JSON Schema: {"enum":["red","green","blue"]}; Root interface name: Color; Add export keyword: yes | export type Color = "red" | "green" | "blue"; |
| Local $ref definitions | JSON Schema: {"type":"object","properties":{"user":{"$ref":"#/definitions/User"}},"definitions":{"User":{"type":"object","properties":{"id":{"type":"integer"},"email":{"type":"string"}},"required":["id","email"]}}}; Root interface name: Response; Add export keyword: yes | export interface User { / id: number; / email: string; / } / export interface Response { / user?: User; / } |
FAQ
Does this tool upload my schema to a server?
No. The json schema to typescript conversion runs entirely in your browser using JavaScript. Nothing leaves your machine.
Which JSON Schema drafts are supported?
The converter reads the keywords used by Draft-04 through Draft 2020-12 that describe shape: type, properties, required, items, prefixItems, enum, const, oneOf, anyOf, allOf, definitions, $defs and local $ref. Validation-only keywords such as pattern or minimum are ignored.
Can I use this as a ts json schema generator in CI pipelines?
This page is designed for interactive use. For automated builds, use a command-line tool such as the json-schema-to-typescript npm package, which covers more of the specification and runs in a script.
Why does integer map to number instead of a distinct type?
TypeScript does not have a separate integer type. All numeric values use the number type, so both integer and number schemas produce number.
How are optional properties determined?
A property is optional unless its name appears in the required array of the parent object schema. Optional properties get a question mark suffix in the output.
What happens with external $ref URLs?
External references cannot be fetched because the tool runs offline in the browser. Only local references starting with #/ are resolved.