JSON to TypeScript

Generate TypeScript interfaces/types from JSON.

The JSON to TypeScript generator creates interface or type declarations from a sample JSON document. Nested objects get their own named types, arrays get element types, and keys missing from some objects can be marked optional. It saves time when typing an API response by hand.

Typing JSON in TypeScript

TypeScript checks the shape of data at compile time, but data from an API arrives as untyped JSON. To use it safely you describe its shape with an interface or type alias, for example a User with id, name and tags. Writing these by hand for a large response is slow and error-prone, so a generator infers them from a sample. It maps JSON strings, numbers, booleans and null to string, number, boolean and null, arrays to element[] types, and nested objects to their own named declarations named after their keys. If an array contains several objects, their shapes are merged, and a key that is missing from some of them becomes optional. Types inferred from a sample cannot know about values the sample did not show, so review them before relying on them.

How to use JSON to TypeScript

  1. Paste a sample Paste a JSON document into the Sample JSON box.
  2. Set the names and style Enter a Root name and choose interface or type in Declaration.
  3. Choose optional handling Keep Optional when missing from some objects ticked to mark such keys with ?.
  4. Copy the TypeScript Copy the output or download it as types.ts.

Frequently asked questions

Does it generate interfaces or type aliases?

Either. Choose interface or type in the Declaration option; both are exported.

How are dates typed?

As string. JSON has no date type, so a timestamp in a sample is a string. Convert it to Date in your own code if needed.

Why is a property marked optional?

With Optional when missing from some objects ticked, a key that appears in some but not all of the objects merged into that declaration is written with a question mark.

Will the types match all future responses?

Not necessarily. The types describe only the sample you pasted. A field that was always present in your sample may be missing or have another type elsewhere.

Is my JSON sent to a server?

No. The types are generated in the page and the sample is not sent anywhere.