JSON to TypeScript Converter

Paste JSON and get TypeScript interfaces for it, with nested objects named, arrays typed and missing keys marked optional.

Ta treść jest po angielsku.

Rozwiązane przykłady

A simple object

{"id":1,"name":"Ann"}

interface Root { id: number; name: string; }

Numbers and strings map to number and string.

Optional keys in an array

[{"a":1},{"a":2,"b":"x"}]

type Rows = RowsItem[]; interface RowsItem { a: number; b?: string; }

The key b is missing from the first item, so it is optional.

O narzędziu JSON to TypeScript Converter

This converter writes TypeScript types from a sample of JSON. Paste a response from an API or a config file and get interfaces you can drop straight into your code.

Nested objects get their own interface, named from their key. Arrays take the type of their items, and when an array holds several objects they are merged into one interface with any key that is not in every object marked optional. Mixed values become a union.

Jak korzystać z narzędzia JSON to TypeScript Converter

  1. Paste your JSON.
  2. Set the name of the main type, and choose interfaces or type aliases.
  3. Copy the TypeScript, or download it as a .ts file.

Typowe zastosowania

  • Typing the response of an API you are calling
  • Describing a configuration file
  • Starting a type for test data
  • Documenting the shape of a payload

Najczęstsze pytania

How are optional properties decided?

When an array holds several objects, any key that is missing from at least one of them is marked with a question mark.

What about null and empty arrays?

A null value gives the type null, added at the end of a union. An empty array is typed unknown[] because there is nothing to learn the item type from.

Can the root be an array?

Yes. You get a type for the array and an interface for its items, named with Item on the end.

Will the types cover every possible response?

Only what is in your sample. Include examples with optional fields and different value types so the result is accurate.