How to generate TypeScript interfaces from JSON
Typing an API response by hand is tedious and error prone, and most of it is mechanical: an object becomes an interface, a nested object becomes another interface, an array of objects becomes an array of one. The parts that are not mechanical are exactly the parts a sample cannot tell you, such as which fields are nullable.
Paste a real response and get the interfaces immediately. Then widen the types the sample could not see. Everything runs in your browser, so a production payload with customer data stays on your machine.
- Paste your JSON. An array of several records gives much better results than a single object.
- Name the root interface.
- Read the generated interfaces, including the nested ones inferred from nested objects.
- Check for question marks (optional fields, detected across array elements) and any unknown[] (an array your sample left empty).
- Copy the TypeScript, then widen any field that your sample happened not to show as null.