Generate JSON Schema from TypeScript Types: A Practical Review of ts-json-schema-generator
How ts-json-schema-generator turns TypeScript types into JSON Schema, what the conversion preserves or loses, and when a hand-written or schema-first approach fits better.
Converting TypeScript types into JSON Schema promises one source of truth: you describe a data shape once in TypeScript and get a machine-readable schema for validating JSON at runtime. ts-json-schema-generator is one of the best-known tools for this. This review explains what the conversion changes, where it is lossy, and how usable the output is once you share it with validators, API tooling or other people.
A note on evidence: no benchmarks, version-specific test results or measured compatibility data were available for this review. The points below rest on how TypeScript and JSON Schema work in general and on the tool's broadly documented purpose. Command flags, defaults and supported annotations change between releases, so confirm them against the current project documentation or the CLI's help output before relying on them.
Is generating JSON Schema from TypeScript right for your use?
The conversion suits teams whose TypeScript types are already the authoritative description of their data and who need a schema for runtime validation, configuration files, editor autocompletion or interchange with other services. The main benefit is avoiding two hand-maintained definitions that drift apart.
The decisive tradeoff is that the TypeScript type system and JSON Schema overlap only partly. TypeScript describes shapes but cannot express many runtime constraints, such as a minimum value, a string pattern or a maximum length. JSON Schema cannot represent functions, class behavior or many advanced type-level computations. The generated schema is therefore only as precise as your types plus any annotations you add, and it should be reviewed rather than trusted blindly.
A hand-written schema, or a schema-first library from which types are derived, is usually preferable when the schema is a public contract versioned independently of your code, when most of its value lies in constraints TypeScript cannot express, or when your project already validates input with a runtime library that infers TypeScript types.
- Usually a good fit:
- Internal services where TypeScript interfaces already define request and response payloads
- Configuration files that need editor validation based on existing types
- Projects that want a generated schema checked into the repository and refreshed automatically
- Usually a poor fit:
- Schemas dominated by value constraints, formats and conditional rules
- Types built heavily from complex conditional or mapped types that may not resolve to clean, concrete shapes
- Public API contracts that need careful, human-curated wording and stable structure
What the conversion actually changes
TypeScript types are erased at compile time; nothing of an interface survives in the emitted JavaScript. A generator such as ts-json-schema-generator works around this by analyzing your source code with the TypeScript compiler, resolving the named type you ask for, and writing a separate JSON document that describes the same shape. Your source files are not modified. The result is a new artifact that a JSON Schema validator can read at runtime.
Two common points of confusion are worth separating. First, generating a schema does not validate anything by itself; you still need a validator library that supports the schema's draft version. Second, the direction matters: this tool goes from TypeScript to JSON Schema. Tools that generate TypeScript declarations from an existing schema solve the opposite problem and imply a different source of truth.
How typical TypeScript constructs map
| TypeScript construct | Typical JSON Schema result | Caveat |
| string, number, boolean | A matching type keyword | number does not distinguish integers; an annotation is needed if integers are required |
| Optional property (name?: T) | Property left out of the required list | Optional and undefined-capable properties are not the same in every configuration; check the output |
| Union of string literals | An enum or a set of constant alternatives | Exact keyword choice varies by tool and version |
| Named interface or type alias | A reusable definition referenced by $ref | Readers must follow references to see the full shape |
| Index signature or Record | additionalProperties describing the value type | Interacts with how the tool handles extra properties by default |
| Date | No native JSON type; JSON carries dates as strings | Generator behavior differs; confirm whether you get a string with a date format or something else |
| Functions, methods, symbols | No JSON equivalent | Expect them to be omitted or to cause an error |
| Generics, conditional and mapped types | Resolved to concrete shapes where possible | Complex types may produce verbose output or fail to generate |
Structural typing deserves particular attention. TypeScript generally tolerates extra properties on objects that are not fresh literals, while a JSON Schema with additionalProperties set to false rejects them. Whether your generated schema allows or forbids extra properties depends on the tool's default and your options, and that choice changes validation behavior significantly. Verify it explicitly.
Illustrative example
The following pair is hand-written to show the mapping described above. It is not captured output from a specific version of ts-json-schema-generator; real output will typically add a $schema declaration, may wrap the type in a definitions section referenced from the top level, and may order keys differently.
export interface User {
id: number;
name: string;
email?: string;
role: "admin" | "member";
}
{
"type": "object",
"properties": {
"id": { "type": "number" },
"name": { "type": "string" },
"email": { "type": "string" },
"role": { "type": "string", "enum": ["admin", "member"] }
},
"required": ["id", "name", "role"],
"additionalProperties": false
}
Checking the example against the mechanism: the optional email property is absent from required, the literal union becomes an enum of strings, and id stays a generic number because the TypeScript type says nothing about integers or ranges. Note also what is missing: nothing says email must look like an email address. Adding that kind of constraint requires annotations in the source. The tool's documentation describes JSDoc-style tags for constraints such as minimums, patterns and formats; confirm the supported tag list for the version you install.
Will the schema stay usable after copying or sharing?
The output is plain JSON text, so copying, pasting, committing and diffing it is straightforward. There is no hidden styling or special character substitution to lose along the way. Usability problems come instead from the consumers that read the schema and the people who need to understand it.
- Draft compatibility: JSON Schema has several drafts with different keywords. A validator or tool that expects a different draft may reject or misread parts of the schema. OpenAPI 3.0 uses its own modified subset of JSON Schema, while OpenAPI 3.1 aligns with a newer draft, so a generated schema may need adjustment before it fits an API description.
- Reference structure: generated schemas often rely on $ref and a definitions section. Some consumers resolve references fully; others handle only a subset. Test with the exact tool you plan to use.
- Naming: definition names are derived from type names. Generic instantiations can produce long or encoded names that are hard to read and may change when types are refactored.
- Editing: manual edits to a generated file are overwritten the next time it is regenerated. Make changes in the TypeScript source or its annotations instead.
- Human readability: deeply nested JSON with references is hard to follow for any reader, including someone navigating with a screen reader. If the schema doubles as documentation for people, provide a prose description or generated reference documentation alongside it.
No scoped compatibility tests against specific validators, editors or API platforms were available for this review. One schema validating correctly in one tool is not evidence that it will behave identically everywhere.
A practical workflow
- 1. Export the type you want to describe and keep it free of functions or runtime-only members.
- 2. Run the generator against that type using the same tsconfig your project builds with, so type resolution matches.
- 3. Inspect the output for extra-property handling, date handling, integer versus number, and any types that resolved unexpectedly.
- 4. Validate a set of real sample payloads, including deliberately invalid ones, with a validator that supports the schema's draft.
- 5. Commit the schema and regenerate it in continuous integration, failing the build if the committed file differs from fresh output.
A typical invocation looks like the following. The flag names reflect the tool's commonly documented interface, but options change between releases, so confirm them with the CLI's help output.
npx ts-json-schema-generator --path 'src/types.ts' --type 'User' --tsconfig tsconfig.json --out user.schema.json
How it differs from related approaches
| Approach | Source of truth | Main strength | Main limitation |
| ts-json-schema-generator | TypeScript types | Reuses types you already maintain | Lossy where TypeScript and JSON Schema do not overlap |
| Other TypeScript-to-schema generators, such as typescript-json-schema | TypeScript types | Same goal with different annotation conventions and type support | Differences only show up when tested on your own types |
| Schema-first runtime libraries | Schema defined in code, types inferred | Runtime validation and types cannot drift apart | Types are written in the library's own style, and exporting standard JSON Schema may need an extra step |
| Hand-written JSON Schema | The schema document | Full control over constraints, wording and structure | Must be kept in sync with code by hand |
No head-to-head comparison data between these tools was available. If you are choosing between TypeScript-to-schema generators, run each against a representative set of your own types, including your most complex generics, and compare the output directly.
Gaps and open questions
- No version-specific tests: defaults such as extra-property handling and supported annotations were not verified for a particular release.
- No performance data: generation time on large codebases was not measured.
- No consumer compatibility tests: behavior with specific validators, editors and API platforms was not checked.
- Data handling: ts-json-schema-generator is installed as a package and run in your own environment. This review does not assess any online TypeScript-to-JSON-Schema converter; check a web tool's stated data practices before pasting proprietary types into it.
Bottom line: generating JSON Schema from TypeScript types works well when your types already define the data and you are prepared to review the output, add annotations for constraints, and test with the validators you actually use. When the schema itself is the product, or when constraints matter more than shape, write the schema directly or adopt a schema-first approach.