JSON to TypeScript

TypeScript types from a JSON sample.

Free. Runs in your browser: nothing you enter or open is uploaded.

TypeScript

Types are inferred from the sample in your browser. Give it several items in an array when fields vary: the more realistic the sample, the more accurate the types. Nested objects get their own named interfaces, and identical shapes share one name.

How to use it

  1. Paste a JSON response or file. An array of several records gives the best result.
  2. Set the root name and choose interface or type, how null is handled and whether to export.
  3. Copy the TypeScript or download it as a .ts file.

Questions

How are optional fields found?

When a JSON array holds several objects, a field that is missing from some of them is marked optional with a question mark. A field that is present everywhere is required.

What happens when a field has different types?

The types are joined into a union, such as string | number. Arrays with mixed items become arrays of a union, written (string | number)[].

Should I use interface or type?

Either works for plain data shapes. Interfaces can be extended and merged, which suits public APIs; type aliases are needed for unions. Pick the one your codebase already uses.

Why is an empty array typed unknown[]?

An empty array says nothing about what it will hold. Replace unknown with the real item type, or add an example item to the sample.