JSON Schema to TypeScript converter
Convert JSON Schema to TypeScript: types for every definition ($defs and $ref, enums, tuples, unions, nullable types), plus validate.ts: dependency-free isX(value) and parseX(value) functions that check every rule the types cannot express, such as minLength, pattern, minimum, formats, oneOf, not and if/then/else.
What you get
- Types and validators together: types.ts for your editor, and validate.ts with isX(value) and parseX(value) for TypeScript runtime validation, with no dependencies.
- Every rule enforced at run time: minLength, pattern, minimum, formats (email, date-time, uuid…), enums, tuples, oneOf, not and if/then/else.
- Proved, not guessed: validate.ts gives the same answer as Ajv on every test value, and types.ts and validate.ts compile in strict mode.
- $defs, $ref, recursive types, unions and nullable types become clean, named TypeScript.
How each result is checked
- Your schema is compiled with Ajv, the reference JSON Schema validator.
- types.ts and validate.ts are compiled together with the real TypeScript compiler in strict mode.
- validate.ts is run: on every test value, isX(value) must give the same answer as Ajv, and parseX must list the problems for values it rejects.
- Every test value that isX accepts is type-checked against the generated type, so `value is X` is true to the types.
- When your schema has examples, each example is type-checked against the generated root type.
What can't convert exactly
- TypeScript types alone cannot enforce rules like minLength, pattern or minimum (they stay as JSDoc comments); validate.ts checks all of them, with no library. Turn validate.ts off for types only.
- Formats (email, date-time, uuid, uri, ipv4…) are checked with the same rules as Ajv and ajv-formats, so both agree on every value.
- A schema with object rules but no "type" is typed as an object by default, and validate.ts assumes the same. JSON Schema also accepts other values there; choose "Exact" to match it precisely.
- unevaluatedProperties, unevaluatedItems and $dynamicRef are not checked yet; the notes name them when your schema uses them. External $refs (other files or URLs) are treated as unknown.
Questions
Can TypeScript check minLength, pattern or minimum?
Not in the type system: a type cannot say "a string of at least 3 characters". That is why this page also writes validate.ts: isOrder(value) and parseOrder(value) check every rule at run time and narrow the value to the type, with no dependencies.
How do I validate JSON against the generated types?
Use the generated functions: if (isOrder(data)) { … } narrows data to Order, and parseOrder(data) returns an Order or throws a ValidationError with a JSON pointer and a message for every problem.
Do I need Zod or Ajv at run time?
No. validate.ts is plain TypeScript with no imports except your types.
interface or type?
Objects are interfaces by default. Switch to type aliases in the options.
How is this tested?
For every test schema, generated valid data and hundreds of broken variants of it are checked by Ajv and by validate.ts, which must agree on each one; validate.ts is also compiled against TypeScript's real standard library, and every accepted value must type-check against the generated type.
How do I convert JSON Schema to TS with runtime validation?
Paste your JSON Schema here. You get TypeScript types from JSON Schema in types.ts, an interface or type for every definition, and validate.ts with isX(value) and parseX(value) functions that check every rule of the schema. Use parseX on data you receive (API responses, files, form input) to validate JSON against a TypeScript type at run time.
Do I need to install a package to convert JSON Schema to TypeScript?
Nothing to install: no package, CLI or account. It runs in your browser, so you can try a schema before adding a generator to your build.
What is the difference between types.ts and validate.ts?
types.ts holds the interfaces and types, which TypeScript erases at build time. validate.ts holds isX(value) and parseX(value), small functions that check data at run time against every rule of the schema (lengths, patterns, formats, oneOf…). Use both: the types for your editor, the validators for data you receive.
How do I convert JSON Schema to TypeScript online?
Paste your JSON Schema or open a file on the left (or click Example). The TypeScript appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.
Can the TypeScript output be wrong?
Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.