The most tedious part of wiring up a new endpoint is often hand-writing TypeScript types from the JSON response. With many fields it is easy to miss one, use the wrong type, or forget that a field is optional.
This step can be fully automated: give the tool a real JSON sample and it infers the type of every field and emits a matching TypeScript interface. Below is how it works, plus the edge cases that trip people up.
A minimal example
Suppose the endpoint returns this JSON:
{
"id": 1001,
"name": "ToolGarden",
"isActive": true,
"tags": ["json", "pdf"],
"owner": {
"email": "hi@example.com",
"verified": false
},
"lastLogin": null
}From the values, matching TypeScript types can be inferred. Nested objects become their own interfaces and arrays infer their element type:
interface Owner {
email: string;
verified: boolean;
}
interface Root {
id: number;
name: string;
isActive: boolean;
tags: string[];
owner: Owner;
lastLogin: null;
}How are the types inferred?
- string → string, number → number, boolean → boolean.
- object → its own interface, named after the field (e.g. owner → Owner).
- array → element type with [], e.g. an array of strings is string[].
- null → null (a lone null value cannot reveal the real type; add it manually).
- A field that has different types across objects → a union type (e.g. string | number).
Edge cases to watch for
1. Optional fields
A single JSON sample cannot tell the tool which fields might be missing. If a field is sometimes returned and sometimes not, mark it optional by hand (add ? after the name). Using samples that cover more cases, or merging several samples, reduces these misses.
2. null vs the real type
A field like lastLogin: null can only be inferred as null. In reality it is usually string | null or number | null. Generate from a sample where the field has a value, or switch it to a union type by hand, to match the real API.
3. Empty and mixed arrays
An empty array [] gives no element type and usually degrades to unknown[] or any[]. A mixed array such as [1, "a"] becomes (number | string)[]. The richer the sample elements, the more accurate the result.
Practical tips when generating types
| Situation | Recommended approach |
|---|---|
| Field may be missing | Add ? to mark it optional, or merge multiple samples |
| Field value is null | Change to T | null (e.g. string | null) |
| Array is empty | Specify the element type by hand to avoid any[] |
| Enum-like strings | Use a literal union where useful (e.g. "draft" | "published") |
| Sample is invalid | Validate with a JSON formatter first, then generate |
Generated locally, your API data is never uploaded
API responses often carry tokens, emails and user IDs, so pasting them into a tool that uploads your data is risky. ToolGarden's JSON → TypeScript tool infers everything locally in your browser, so the sample never leaves your device — safe to run on a real response.
Summary
Generating a TypeScript interface from JSON saves a lot of hand-typing and cuts down on mistakes. What still needs your attention is what a sample cannot cover: optional fields, the real type behind null, and empty arrays. Fill those in and the generated types are ready to trust in your project.