Scenario
A new endpoint is stable enough to integrate against, and the frontend needs TypeScript types for its response. Writing interfaces by hand from a JSON example invites the classic drift bugs: a typoed field name that compiles fine and fails at runtime, or a field typed string that is sometimes null. Generating the interface from a real response gets the mechanical part right so review time goes to the judgment calls.
Broken input
{"id":"user_123","email":"ada@example.test","roles":["admin"],"profile":{"timezone":"UTC"}}
Goal
Use JSON to TypeScript Interface Generator to derive interfaces from the response sample, then review them as a draft — the generator captures what this response looks like, and you decide what every response is allowed to look like.
Tool sequence
- Open JSON to TypeScript Interface Generator.
- Paste a real captured response, not a hand-typed reconstruction — the whole point is to type what the API actually returns.
- Generate and read the result: primitives typed from values,
rolesinferred asstring[], and the nestedprofileobject extracted into its own interface. - Rename the generated interfaces to domain names (
User,UserProfile) and mark fields optional or nullable where the API contract says so — one sample cannot tell you that. - If several teams consume the endpoint, also generate a schema with JSON Schema Validator / Generator so the contract is enforceable outside TypeScript.
Checkpoint output
interface User {
id: string;
email: string;
roles: string[];
profile: Profile;
}
interface Profile {
timezone: string;
}
The generated types carry the string fields, the roles: string[] array, and a nested profile interface — a correct skeleton awaiting optionality decisions.
Common mistakes
- Generating types from invalid or truncated JSON and typing a shape the API never returns.
- Treating one example as proof that every field is always present — samples cannot show optionality.
- Skipping the rename pass, so
RootObjectandProfile2leak into the codebase. - Regenerating types after an API change without diffing, silently overwriting hand-added
| nullannotations.
Related tools and lessons
- JSON Validator - Validate & pretty-print JSON.
- JSON Formatter - Format, minify, and filter JSON by keys.
- JSON Schema Validator / Generator - Validate JSON with Draft 2020-12 or generate starter schemas.
- Related lesson: Find the JSON Syntax Error Breaking a Request
- Related lesson: Make a Nested JSON Response Readable