JavaScript/JSON to TypeScript Types

Paste JSON or a JS object, get TypeScript types

Input (JS object or JSON)

Output (TypeScript)

JavaScript/JSON to TypeScript Types guide

Typing an API response by hand is slow and easy to get wrong. Paste a JSON response or a JavaScript object and this tool writes the matching TypeScript interfaces for you, with a separate interface for every nested object.

How the conversion works

Each object becomes an interface, and every nested object gets its own named interface that the parent refers to. Arrays become typed arrays such as string[] or Order[], and the interface for the items is named in the singular.

When the items of an array have different fields, the fields that some items lack are marked optional with ?. Values that are null are typed as null, because there is nothing to infer from. Once you know the real type, change those to something like string | null.

Example

The second order has no total, so total becomes optional in the generated Order interface:

const User = {
  id: 1,
  name: "Ann",
  orders: [{ id: 1, total: 9.5 }, { id: 2 }],
};

// becomes

interface User {
  id: number;
  name: string;
  orders: Order[];
}

interface Order {
  id: number;
  total?: number;
}

JSON or JavaScript input

You can paste strict JSON or a JavaScript object literal with unquoted keys, single quotes, and trailing commas. Start with const User = to name the root interface User; without a name, the root interface is called Object, so it is worth adding one.

The input is evaluated as JavaScript in your browser to read the object, so paste data, not code you do not trust.

When it helps

  • Typing the response of a fetch or axios call instead of using any.
  • Writing types for a config file, fixture, or mock data.
  • Documenting the shape of a third-party webhook payload.

Frequently asked questions

What does the JavaScript/JSON to TypeScript Types tool do?
This tool converts JavaScript objects or JSON data into TypeScript type definitions, helping you define structured types for better code safety and autocompletion.
How do I use the JavaScript/JSON to TypeScript Types converter?
Paste your JavaScript object or JSON data into the input field, and the tool will automatically generate the corresponding TypeScript types. Click Copy to take the result.
Why should I convert JSON to TypeScript types?
Converting JSON to TypeScript types ensures type safety in your TypeScript projects, reducing errors and improving code maintainability, especially when working with APIs or external data.
Can this tool handle nested JavaScript objects?
Yes, the tool supports nested JavaScript objects and JSON data, generating accurate TypeScript interfaces for complex structures, including arrays and nested properties.