JSON to TypeScript
Turn a sample payload into TypeScript interfaces — nested objects get their own type, mixed arrays become unions, and keys missing from some rows come out optional.
JSON
TypeScript
About this tool
Typing an API response by hand is tedious and easy to get subtly wrong. Paste one real payload and this generates the interfaces for it: every nested object becomes its own named type, arrays of objects are merged into a single shape so a key missing from some rows is marked optional, and mixed arrays become unions. Identical shapes are emitted once and shared. What it cannot do is guess beyond the sample — a field that is null in your example is typed null, because nothing in the data says otherwise.
How to use it
- 1Paste a representative JSON response — the more rows an array has, the better the optional detection.
- 2Name the root type and choose interface or type, to match your codebase.
- 3Copy the result into a .d.ts file, then widen any field the sample under-described.
Frequently asked questions
›How are optional fields decided?
By comparing the objects in an array. If a key is present in some elements and missing in others, it is marked optional; a key present everywhere is required. A single object gives no evidence either way, so every key comes out required.
›Why is a field typed null instead of string | null?
Because null is all the sample showed. Inference can only describe the data it was given — widen those fields by hand, or paste a sample that includes a populated value.
›Why do two different keys share one interface?
Identical shapes are emitted once and reused, which keeps the output short. Rename the interface if the two are conceptually different types that happen to look alike today.
›interface or type — which should I pick?
Interfaces can be extended and merged by declaration, which suits object shapes and public APIs. Type aliases cover unions and mapped types too. For plain generated payload shapes the two behave the same, so pick whichever your codebase already uses.
›Does this validate the data at runtime?
No. TypeScript types are erased at compile time, so nothing checks that an API response actually matches. If you need runtime guarantees, generate a schema with Zod or Valibot instead and derive the type from it.