JSON Schema to TypeScript Converter
Generate TypeScript types from an explicit JSON Schema rather than guessing from sample values. Try the address-reference example to see required keys, optional nullable fields and local $ref resolution. Schemas stay in your browser; remote references are rejected.
typetools
instant, client-side type converter • 0ms network latency
// Structural types only: oneOf is approximated as a union; exact keys, numeric bounds, patterns and other validation constraints require a JSON Schema validator.
export interface Address {
city: string;
}
export interface RootPayload {
id: string;
address: Address;
note?: string | null;
}handling api edge cases & compiler gotchas
common deserialization pitfalls and solutions
Required is independent of nullable
required controls whether a key must exist. A string/null type union controls its value. These schema rules apply regardless of the sample inference toggles.
note?: string | null;
// Missing, string or null are allowed.Local references only
Local JSON Pointers, including guarded recursive references, resolve in memory. Unresolvable pointers, external URLs and unguarded recursion fail clearly without remote fetching.
Structural types are not full validation
allOf becomes intersections; anyOf and oneOf become inclusive unions. TypeScript does not enforce exclusive oneOf matches, integer-only values, numeric bounds, regex patterns or exact object keys. Use a JSON Schema validator at runtime.
Closed objects and literal limits
additionalProperties: false omits the index signature; fresh literals receive excess-property checks, but wider existing values may still be assignable. Impossible required keys produce never. Incompatible typed additionalProperties and object/array const or enum members fail explicitly. Unsafe numeric literals and overflow also fail; fractional values retain JavaScript precision limits.
step-by-step integration guide
technical specification differences & end-to-end conversion workflow
1Paste the schema and its local definitions
The editor example declares id and address as required and note as optional. Address resolves from the same document.
"address": { "$ref": "#/$defs/Address" }2Inspect the emitted types
Address.city is required. RootPayload.note is optional and permits either a string or null.
export interface Address {
city: string;
}
export interface RootPayload {
id: string;
address: Address;
note?: string | null;
}3Check representative values
A missing note is valid. Missing address or a numeric city is invalid. additionalProperties: false catches extra keys on fresh literals within TypeScript’s structural model.
const valid: RootPayload = { id: "inv_1", address: { city: "Austin" } };
// Invalid: { id: "inv_1" }
// Invalid: { id: "inv_1", address: { city: 42 } }4Compile and retain runtime validation
TypeScript output is not a replacement for a JSON Schema validator. In particular, overlapping oneOf branches, numeric bounds and patterns need runtime enforcement.
npx tsc --noEmit --strict generated-types.tsfrequently asked questions
common developer questions answered
q:How are JSON Schema 'allOf', 'anyOf', and 'oneOf' keywords translated to TypeScript?
The compiler maps allOf to TypeScript intersection types (TypeA & TypeB), and maps anyOf and oneOf to standard inclusive TypeScript union types (TypeA | TypeB). Because TypeScript unions are not exclusive-OR, runtime-only oneOf exclusions across overlapping branches require a runtime validator.
q:How does the converter handle internal '$ref' pointers to '#/$defs/' or '#/definitions/'?
Referenced local schemas inside #/$defs/ or #/definitions/ (including recursive references) are resolved in-memory and emitted as named TypeScript interfaces or type aliases. External remote $ref URLs fail explicitly so no schema payloads leave your browser.
q:What happens when 'additionalProperties' is set to false vs. a schema object?
When additionalProperties is false, the index signature is omitted so TypeScript's excess-property checks catch extra properties on fresh object literals. When additionalProperties is omitted or true, [key: string]: unknown is emitted. When additionalProperties is a compatible schema object, a typed index signature ([key: string]: ValueType) is generated.