Generate TypeScript interfaces from JSON
string β string, number β number, boolean β boolean, null β null.interface (or type). Nested objects produce additional named interfaces whose names are derived from their parent key in PascalCase.?) when "Infer optional fields" is enabled.(string | number)[]. An array of objects and primitives produces a union type.unknown[] since the element type cannot be inferred.interface is the idiomatic TypeScript construct for describing object shapes. It supports declaration merging (multiple interface declarations with the same name are merged by the compiler) and is generally preferred for public API contracts and class implementations. type is more flexible β it can represent unions, intersections, primitives, tuples, and mapped types β but does not support declaration merging. For plain object shapes generated from JSON, both are functionally equivalent. Choose interface as the default unless you need union types or are working in a codebase with a type-first convention.
A JSON field set to null is typed as null. In practice you usually want string | null or T | null. Enable "All fields optional" or manually edit the generated type to add the union. If the field is sometimes a string and sometimes null across different API responses, consider running both responses through the tool separately and merging by hand.
When "Infer optional fields" is enabled, the tool compares all objects in an array. If a key appears in at least one object but not all, it is marked optional (?). This mirrors real-world API responses where some fields are omitted rather than set to null when absent.
Copy the output and paste it into a .ts or .d.ts file. Then import and use: import type { Root } from './types';. For API responses, cast with const data = await res.json() as Root or use a validation library like Zod to parse and assert the shape at runtime.