JSON Schema to TypeScript
Convert a JSON Schema into TypeScript types. Objects become interfaces with required and optional properties, $defs and definitions become named exports that $ref points to, enum and const become literal types, anyOf and oneOf become unions and allOf becomes an intersection. Descriptions are kept as doc comments.
Frequently asked questions
Which JSON Schema drafts are supported?
The converter reads the keywords shared by draft-07, 2019-09 and 2020-12: type, properties, required, additionalProperties, items, prefixItems, enum, const, anyOf, oneOf, allOf and $ref to $defs or definitions. Validation keywords such as minLength or pattern have no TypeScript equivalent and are left out.
How are $ref references resolved?
References to #, #/$defs/Name and #/definitions/Name become the named type of that definition. Any other reference, such as one to another file or a URL, is typed as unknown and listed in a comment at the top of the output.
How is additionalProperties handled?
An object with no properties and an additionalProperties schema becomes Record<string, T>. An object with properties and an additionalProperties schema becomes the interface joined with Record<string, T>. additionalProperties false or a missing keyword adds nothing, because TypeScript object types already describe the known keys.
What does nullable do?
The OpenAPI 3.0 nullable: true keyword and the Swagger x-nullable extension add | null to the type. In JSON Schema itself, write type: ["string", "null"], which becomes string | null.
Are recursive schemas supported?
Yes. A definition that refers to itself or to another definition through $ref becomes a named type that refers to the other name, which TypeScript resolves without problems.
Does it check that my schema is valid?
It only reads the schema. Unknown keywords are ignored and a schema without a type becomes unknown. Use the JSON Schema Validator to check data against a schema.