← JSON to Types Generator

How to type an API response safely

Generated types are a description of the samples you gave the generator, not a promise about the API. They are still the fastest way to a correct first draft, as long as you know where they lie.

Optional is not nullable

CaseIn the JSONTypeScript
OptionalThe key is sometimes absentname?: string
NullableThe key is present with nullname: string | null
BothAbsent in some, null in othersname?: string | null

A single sample can show neither. That is why the tool asks for several samples and warns when you give it one: every field of a one-sample type is marked required.

Pick samples that disagree

Types vanish at runtime: validate at the edge

TypeScript types are erased when the code runs, so a response that no longer matches fails somewhere far from the cause. The Zod output gives you a schema to parse the response with, and an inferred type from the same definition, so the check and the type cannot drift apart.

What the tool guesses, and flags

Using the generator

Paste the samples into the tool or run node cli.mjs a.json b.json --style zod, read the notes it prints, and commit the result. Re-run it when the API changes.