How to use the JSON to TypeScript
- Paste a representative JSON sample, such as an API response.
- Set the root type name and choose between interface and type declarations.
- Decide whether keys missing from some array items should be optional, whether properties should be readonly, and whether to export.
- Copy the generated code or download it as types.ts.
Features
- Named nested interfaces derived from key names (users → User, address → Address)
- Arrays of objects merged into a single element type; inconsistent keys become optional
- Union types for mixed values, including null
- interface or type alias output, readonly and export options
- Non-identifier keys are quoted; reserved names are suffixed automatically
How types are inferred
Each JSON value maps to a TypeScript type: strings, numbers and booleans to their primitives, null to null, and objects to a named interface. For arrays, every element's shape is inspected. Objects are merged so the element type has the union of all keys, and a key that isn't present in every element is marked optional (when that option is on). Mixed primitives become a union such as string | number.
The generator infers from the sample you give it, so a field that happens to be null in the sample will be typed as null. Provide a sample where fields have realistic values, or edit the output; the tool is a head start, not a schema.
Naming
Nested type names come from the key that holds them, converted to PascalCase and singularised for arrays: order_items becomes OrderItem. When two different keys produce the same name, a number is appended. Names that clash with built-ins such as Date or Map get a Type suffix.