How to Convert JSON to TypeScript Interfaces Automatically
You have a JSON response from an API. You need TypeScript types for it. Writing them by hand takes time and invites mistakes. Here's how to generate accurate interfaces in seconds.
Why Type Your API Responses
TypeScript's value comes from knowing the shape of your data at compile time. Without types for API responses, you're back to the same runtime errors that TypeScript was designed to prevent.
Typed responses give you autocomplete in your editor, catch property name typos at build time, and make refactoring safe — if an API field changes, TypeScript tells you everywhere it's used.
Interface vs. Type Alias
TypeScript offers two ways to define object shapes: interface and type. For API response typing, the practical difference is small:
- Interfaces can be extended and merged. Use them when other code might need to augment the type.
- Type aliases are more flexible — they support unions, intersections, and mapped types. Use them for complex transformations.
- For plain API responses, both work equally well. Pick one convention and stick with it across your project.
What Auto-Generation Handles
A good JSON-to-TypeScript converter infers types from the actual values in your JSON sample:
- Primitive types: Strings, numbers, booleans, and null are mapped directly
- Nested objects: Each nested object becomes its own interface
- Arrays: Element types are inferred from the array contents — if all elements are the same type, the array is typed as
Type[] - Mixed arrays: Arrays with different element types become union arrays
(string | number)[] - Nullable fields: Fields with null values become
Type | null - Optional vs. required: If you provide multiple JSON samples, fields missing in some are marked optional
Limitations of Auto-Generation
Generated types are a starting point, not a finished product. They can't infer:
- String literal types: A status field with value "active" generates
string, not"active" | "inactive" | "suspended" - Date strings: ISO date strings look like regular strings to a JSON converter
- Branded types: A user ID and a post ID are both numbers, but you might want them to be distinct types
- Optional vs. nullable: From a single sample, the converter can't tell if a missing field is optional or just absent in this response
Always review generated types and refine them: tighten strings to unions, add readonly where mutation isn't expected, and mark fields optional based on your knowledge of the API.
Best Practices for API Types
- Name types after the resource, not the endpoint:
User, notGetUserResponse - Keep API types separate from UI types: Your API shape and your component props are different concerns
- Use Zod or io-ts for runtime validation: TypeScript types disappear at runtime — if you need to validate data, pair types with a runtime schema
- Version your types: When an API changes, update types and let the compiler find every affected call site
Generate TypeScript types from JSON
Paste a JSON response, get clean TypeScript interfaces — with nested types and arrays handled automatically.
Open JSON to TypeScript Converter