JSON to TypeScript
Turn an API response or config file into TypeScript interfaces: nested objects get their own named types, and keys missing from some array items become optional.
Last checked
JSON
export interface Root {
id: number;
email: string;
is_active: boolean;
roles: string[];
profile: Profile;
orders: Order[];
}
export interface Profile {
display_name: string;
avatar_url: null;
time_zone: string;
}
export interface Order {
id: string;
total: number;
items: number;
coupon?: string;
}
3 types. Keys missing from some array items are optional (?).
How to use it
- Paste JSON, for example a response copied from the Network tab.
- Set the root type name and pick interface or type.
- Copy the types into your project.
What it checks and why
Typing an API response by hand is slow and easy to get wrong. The tool reads the JSON and writes a TypeScript type for every object it finds, named after the key that holds it, so the result reads like types you would write yourself.
One sample can only say so much, which is why arrays matter: the tool reads every item of an array and merges them. A key present in some items and missing from others becomes optional (coupon?: string), and a key that is a string in one item and null in another becomes string | null. Paste a response with several items to get the most accurate types.
interface and type are interchangeable for object shapes. Interfaces can be extended and merged by other declarations; type aliases can also name unions and primitives. The TypeScript handbook suggests interfaces for objects until you need a type alias feature.
Specs and sources
Limits
- Types come from the sample. A field that is always null in the sample cannot tell its real type; it is typed null (or optional unknown with the option on).
- Strings stay string: dates, enums and string literals are not guessed.
- Plain JSON only; comments and trailing commas (JSONC, JSON5) are a syntax error.
- No runtime validation: for that, generate a JSON Schema from the same data.
Questions
- How do I convert JSON to a TypeScript interface?
- Paste the JSON above and copy the result. By hand, write one interface per object with a field for each key and its type, and a separate interface for each nested object.
- Should I use interface or type?
- For object shapes either works. Interfaces can be extended and merged; type aliases are needed for unions and mapped types. Pick one style and keep it.
- How do I make a property optional in TypeScript?
- Add a question mark after its name: coupon?: string. The tool does this for keys that are missing from some items of an array.
- Does TypeScript check JSON at runtime?
- No. Types are removed when the code is compiled, so data from an API is not checked. Validate it with a schema library such as Zod or with JSON Schema if the shape matters.
- How do I type a JSON file I import?
- With resolveJsonModule on in tsconfig.json, TypeScript infers the type of an imported .json file. For data you fetch, use generated interfaces like these.
- Is my JSON sent anywhere?
- No. Parsing and type generation run in your browser, so you can paste private API responses.