Tech / JSON to TypeScript
JSON to TypeScript
Paste a JSON sample and get TypeScript interfaces for it, with nested objects, optional keys and union types worked out for you.
Objects inside an array are merged into one type. A key missing from some of them becomes optional (?), and mixed values become a union such as string | null.
Why turn JSON into TypeScript types?
When your code reads data from an API, TypeScript types tell your editor which fields exist and what kind of value each one holds, so mistakes show up while you type instead of when the app is running.
For example, this sample, where the second person has no email and an extra phone number:
[{"id":42,"name":"Anu","email":"anu@example.com"},{"id":43,"name":"Ravi","email":null,"phone":"0400 000 000"}]
becomes an interface where email is string | null and phone is optional (phone?: string), because it is missing from one of the items.
Interface or type?
Both describe the shape of an object. An interface can be extended and merged, while a type can also describe unions and other combinations. Either works well for API data - choose one style and use it across your project.
Do types check the real data?
No. TypeScript types are removed when the code is compiled, so they cannot catch a wrong value that arrives from an API while the app is running. For data from outside your app, also check it at run time, for example with a schema validation library such as Zod.
Tips for good results
- Paste a realistic sample with several items, including ones with empty or missing fields, so optional keys and nulls are found.
- Rename the root type to something meaningful, such as
CustomerorOrder.
Your JSON stays in your browser and is not uploaded.