How to convert JSON to TypeScript
Infer a TypeScript type declaration from a sample JSON value. Use it as a starting point when adding types around data from an external feed or fixture.
- Paste a representative JSON value, set Root type name and convert.
- Review the inferred type and compile it with the rest of your project.
Example input
{"title":"Field Notes","inStock":true,"quantity":12,"tags":["paper","stationery"]}Example use
Take a sample array of inventory objects and generate a root type describing an array of those records. Add examples with optional fields and nulls so the declaration does not imply a narrower contract than the real data.
Output and compatibility
A type inferred from examples is a snapshot of the observed shape. It cannot infer all future fields, distinguish JSON text from datetime strings on its own, or validate data at runtime. Large numeric identifiers may be safer as strings in the input schema.
Review optional and nullable fields in your real data
Choose samples with explicit nulls, absent fields and both simple and special records. A declaration based on only one successful response can be too narrow for partial data. Confirm the result against the source contract and compile it with your existing code.
Troubleshooting
If the tool rejects the root type, use a valid TypeScript identifier rather than a name with a space or hyphen. If mismatched objects belong to distinct shapes, check whether your application needs a manually written union.
Use JSON Viewer to inspect sample data or TypeScript Formatter to format the declarations afterward.
Frequently asked questions
Does it generate an interface or an alias?
It emits an exported type alias using the name you choose.
Can I paste a JSON array?
Yes. The root type describes the observed array and its items.
Does it validate incoming JSON at runtime?
A TypeScript type is a compile-time declaration. Use JSON Schema Validator when you need to check actual values.
Will it infer fields that are missing from my sample?
The generator can only reason from the supplied values. Add representative samples or edit the result.
Can I choose the type’s name?
Use Root type name and verify it is a valid TypeScript identifier.