Developer

Generating TypeScript types from JSON, and where they go wrong

Pasting an API response into a generator gives you types in a second. They describe that one response, not the API, and the difference shows up in production.

3 minute read

You call a new API, copy the JSON response, and want types for it so your editor can autocomplete and your compiler can catch mistakes. Writing the interfaces by hand is tedious and error-prone. A generator does it in a second. The catch is that the types describe the example you pasted, and an API's real behaviour is usually wider than one example.

What a generator actually does

The JSON to TypeScript tool walks the JSON and writes a type for each value it finds. Strings, numbers and booleans map directly. Each nested object becomes its own named interface, so user.address produces an Address type rather than one giant inline type. Arrays take the type of their elements. The same structure can be written as TypeScript interfaces, Zod schemas, Go structs or Python dataclasses.

Arrays of objects get special treatment. The generator merges every object in the array into one shape, and any field that appears in some objects but not all is marked optional: ? in TypeScript, .optional() in Zod, a pointer with omitempty in Go, and Optional[...] = None in Python. That is the single most useful thing it does, and it only works if the sample contains enough variety to reveal it.

Five places one sample misleads you

1. Fields that were not in the sample. If an error response has a message field and your sample was a success, the type will never mention it. Paste one example of each response shape you expect.

2. Fields that were null. A field that is null in the sample is typed as null, because there is nothing else to infer from. In reality it is probably "a string, or null when missing". Find a sample where it has a value, or widen the type by hand to string | null.

3. Empty arrays. "tags": [] gives the generator no element to look at, so it falls back to unknown[]. That is honest, and it is your cue to fill in the real element type.

4. Integers that are not always integers. In Go and Python the generator chooses int for 3 and float64 or float for 3.5. A price that happened to be a whole number in your sample becomes an integer type, and the first 19.99 from the real API breaks decoding. TypeScript has only number, so this one only bites in typed backends.

5. Dates and enums. JSON has no date type, so "2026-10-24T09:00:00Z" is typed as string. Likewise "status": "active" becomes string, not the union of statuses the API actually allows. Both are worth tightening by hand once you know the real values.

Types vanish at runtime; schemas do not

A TypeScript interface is erased when the code is compiled. If the API changes and starts sending price as a string, your interface still says number, the compiler is happy, and the bug surfaces somewhere far from the cause.

This is what the Zod output is for. A Zod schema is code that runs: you call schema.parse(response) at the point where data enters your program, and a response that does not match fails loudly, with a message saying which field was wrong. The generated schema also gives you the TypeScript type for free through z.infer. For data that crosses a trust boundary, such as a third-party API, a webhook or a file a user uploads, validating at the edge is worth the few lines.

A workflow that holds up

  1. Collect three or four real responses, including an empty list, an error and an object with every optional field filled. Tidy them in a JSON formatter first if they arrived minified.
  2. Wrap them in one array and generate from that, so the merge can find the optional fields.
  3. Fix the nulls, empty arrays, number types, dates and enums by hand.
  4. If the API publishes an OpenAPI or JSON Schema document, prefer types generated from that. It is the contract; your samples are only evidence.

For the opposite job, testing your code against data that does not exist yet, a mock JSON generator produces realistic fake records from a field list, and a JSONPath tester helps you check the paths your code will read before you write them.

Further reading

Do it hereJSON to TypeScriptGenerate TypeScript interfaces, Zod schemas, Go structs or Python dataclasses from JSONOpen

Written for NextTool. Every statement about a tool is checked against that tool's code, and corrections are dated above. How guides are written

jsontypescriptzodgopythonapi