How the types are inferred
Paste a representative API response and the converter writes the matching TypeScript declarations. Each nested object becomes its own named interface, named after its key: an address object becomes Address, and the items of an orders array become Order. The root name is yours to choose.
Arrays are inspected element by element and the shapes are merged. If one object in an array has a field that another lacks, the field becomes optional (gift?: boolean). If an array mixes types you get a union such as (number | string)[]. Empty arrays become unknown[], because nothing can be inferred from them, and a null value is typed null.
Choose interface or type declarations, add or drop export, and mark every property readonly for immutable data. Keys that are not valid identifiers, such as full-name, are quoted automatically.
Worked example
Input:
{"id": 42, "email": null, "roles": ["admin"],
"orders": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1, "gift": true}]}
Output:
export interface Root {
id: number;
email: null;
roles: string[];
orders: Order[];
}
export interface Order {
sku: string;
qty: number;
gift?: boolean;
}
The second order has a gift field the first lacks, so it is optional in the merged type.
Getting better types
- A sample is only evidence. A field that is
nullin your sample is probablystring | nullin reality, so adjust it by hand. - Include several array items, including unusual ones, so optional fields and unions are discovered.
- Numbers are always
number; there is no integer type in TypeScript. - Generated types describe the shape only. Validate untrusted data at runtime, for example with the JSON Schema validator.
Frequently asked questions
Should I use interface or type?
Both describe object shapes. Interfaces can be extended and merged and give clearer error messages, so they are the usual choice for object data. Type aliases are useful when you want unions or mapped types later.
How are optional properties detected?
When the same array contains objects with different sets of keys, any key missing from at least one object is marked optional with a question mark.
Why is a field typed as null?
Your sample contained null, and nothing else is known about that field. Edit the type to something like string | null once you know the real shape.
Does it work for very large responses?
Yes. Repeated shapes in arrays are merged into one type, so even big payloads produce compact output. All processing happens locally.