About this tool
Paste an API response and get TypeScript types for it. When the JSON is an array, every item is merged into one type, so a field missing from some items becomes optional and a field that's sometimes null becomes nullable. Nested objects get their own named interfaces.
How to use it
- Paste a JSON sample. More samples give more accurate types.
- Set the root name and choose interface or type, export and readonly.
- Copy the TypeScript into your project.
Example
[{"id":1,"name":"Alex","manager":null},{"id":2,"name":"Riya","email":"riya@example.com","manager":{"id":1}}]export type Root = RootItem[];
export interface RootItem {
id: number;
name: string;
manager: Manager | null;
email?: string;
}
export interface Manager {
id: number;
}
email is optional because only one item has it, and manager is nullable because it's null in one item.
Questions
- How are optional fields detected?
- A field that's missing from some objects in the same array is marked optional with ?. The more samples you paste, the more accurate this gets.
- What about empty arrays?
- An array that's empty in every sample is typed unknown[], since there's nothing to infer from. Add a sample with items to get a precise type.
- Can it handle keys with spaces or dashes?
- Yes. Keys that aren't valid identifiers are quoted, as in "first name": string.