TsWorks Tools
All tools (39)

JSON to TypeScript

Generate TypeScript interfaces from a JSON sample.

Declare as

Off emits a separate type for every nested object.

Paste a JSON response to generate interfaces from it.

TypeScript interfaces from a JSON sample

Paste an API response and get interfaces for it. Every nested object becomes its own named type, and the names are derived from the keys they came from.

Two things this gets right that most generators do not

  • Arrays are merged across every element, not read from the first one. If item one has lastLogin and item two does not, the field is marked optional instead of being declared required and breaking at runtime.
  • Identical shapes are declared once and reused. A list of 200 users produces one User interface, not 200 inline object types.

Fields with different types across elements become a union. A field that is null in the sample is typed null - widen it by hand if the real type is string | null.

This is a starting point, not a contract

The JSON you paste is one observation of an API's shape. A field that happens to be present in every sampled record is not necessarily required, and a number that happens to be an integer is not necessarily always one. Check the generated types against the API's documentation before relying on them. If the API publishes an OpenAPI or JSON Schema document, generate from that instead.

Related tools