How to generate TypeScript types from JSON API responses
Published by InstantToolDeck · Updated November 2026
Writing types for an API response by hand is slow and error-prone. Generating them from a real JSON sample is faster, as long as you know what the generator can and cannot infer.
Steps
- Copy a realistic response from your API or browser network tab.
- Paste it into the JSON to TypeScript converter.
- Set the root name and choose interface or type alias.
- Copy the output into your project and review it.
How types are inferred
- Strings, numbers and booleans become
string,numberandboolean. - Objects become interfaces, named from the property that holds them (
profilebecomesProfile). - Arrays become
T[]. Arrays of objects are merged into one shared interface. - A key missing in some objects becomes optional:
age?: number. - Different value types for one key become a union:
string | null.
Limits of a single sample
A type generator only knows what is in the sample. If the sample never contains null for a field that can be null, the type will not include it. Use several responses, or a sample with an array of varied items, to make types accurate. An empty array becomes unknown[].
Fields that hold only a few possible strings, such as a status, are typed as string. Replace them with a union such as "active" | "closed" by hand.
Compile-time types are not validation
TypeScript types disappear at runtime. They do not stop an unexpected response from reaching your code. For untrusted data, add a runtime validator such as Zod or Valibot in addition to the types.
Dates and numbers
JSON has no date type, so dates arrive as strings. Large integers beyond 2^53 lose precision when parsed as JavaScript numbers.
Frequently asked questions
Can I generate TypeScript types from YAML?
Yes. Convert the YAML to JSON with the YAML converter, then paste the JSON into the TypeScript generator.
Why is my field typed as unknown[]?
The sample had an empty array, so nothing could be inferred. Use a sample with at least one item.
Should I use interface or type?
Interfaces are the usual choice for object shapes and can be extended. Type aliases are more flexible for unions.
Are generated types safe to trust?
Treat them as a first draft. They reflect your sample, not the API's full specification.