How to Generate TypeScript Types From JSON (and What They Miss)

Turn a JSON response into TypeScript interfaces, choose interface or type, handle optional and null fields, and see why types still need runtime checks.

Updated September 29, 2026 6 min read

Use the free JSON to TypeScriptGenerate TypeScript interfaces or type aliases from a JSON sample

You are integrating a new API. The documentation is thin, but you have a sample response in your network tab: forty fields, a few nested objects, an array of addresses. Typing that out by hand as TypeScript interfaces is slow and error-prone, and until you do, every access is any and your editor cannot help you.

Generating types from a JSON sample gets you most of the way in seconds. This guide shows how the conversion works, how to decide between interface and type, how optional and null fields are represented, and, just as important, what a sample-based generator cannot know.

How JSON maps to TypeScript

Each JSON value type has a natural TypeScript counterpart:

JSON TypeScript
"text" string
42, 3.14 number (TypeScript has no separate integer type)
true / false boolean
null null
{ ... } a named interface or object type
[ ... ] T[], where T is inferred from the items

The interesting work happens with arrays of objects. A good generator does not just look at the first item; it merges all of them. A key that appears in some items but not others becomes optional (postcode?: string). A key whose value is a string in one item and a number in another becomes a union (string | number).

Given this sample:

{
  "id": 1042,
  "email": "sam@example.com",
  "displayName": "Sam Lee",
  "manager": null,
  "roles": ["admin", "billing"],
  "addresses": [
    { "type": "office", "city": "Leeds", "postcode": "LS1 4AP" },
    { "type": "home", "city": "York" }
  ]
}

the JSON to TypeScript converter produces:

export interface User {
  id: number;
  email: string;
  displayName: string;
  manager: null;
  roles: string[];
  addresses: Address[];
}

export interface Address {
  type: string;
  city: string;
  postcode?: string;
}

Notice two things. The nested type was named Address automatically, singularised from the addresses key. And manager is typed as just null, because that is all the sample shows. We will come back to that.

Interface or type alias?

For object shapes like these, both work, and the difference is mostly style:

  • interface can be extended with extends and merged if declared twice with the same name. Error messages often show the interface name, which reads well.
  • type can express things interfaces cannot, such as unions (type Id = string | number) and mapped types. It cannot be re-opened.

Many teams use interfaces for object shapes and type aliases for everything else. Pick one convention and stay with it. If the root of your JSON is an array, the generator emits a type alias for the root anyway, for example type Users = User[], because an interface cannot describe an array directly.

Optional versus null

These are easy to confuse and they mean different things to an API:

  • postcode?: string means the key may be missing from the object.
  • postcode: string | null means the key is always present, but its value may be null.

With strictNullChecks on (part of strict mode), TypeScript treats them differently. Code that checks if (user.postcode) handles both, but code that serialises the object back will behave differently: an absent key disappears from the output, while a null key is sent as null.

Some APIs are inconsistent and use both. That is why the converter offers a switch to treat null fields as optional, when your code does not care about the distinction.

Generate types with our tool

  1. Open JSON to TypeScript and paste a sample into JSON sample, or click Open file or Load sample.
  2. Set the Root type name, for example User or OrderResponse. Nested types are named from their keys.
  3. Choose Declare as: interface or type alias.
  4. Toggle Add export if the types will live in their own module, and Null fields as optional if you want manager?: null rather than manager: null.
  5. Review the TypeScript types output, then Copy it or Download it as a .ts file.

The best input is a sample that shows as much variety as possible. If you have several responses, put them together in a JSON array and convert that: the tool merges the items, so fields that only appear in some responses become optional.

What a generator cannot know

A sample shows what the data was, not what it can be. Review the output with that in mind.

Null-only fields. manager: null tells you nothing about the real type. Replace it by hand with something like manager: Manager | null once you know the shape.

Empty arrays. "tags": [] has no items to inspect, so the element type is unknown[]. Fill it in.

String enums. "status": "shipped" becomes string. If you know the full set, narrow it to "pending" | "shipped" | "cancelled" for much better autocomplete and safety.

Dates. JSON has no date type, so ISO timestamps are strings. Keep them as string and convert explicitly, rather than pretending they are Date objects.

Large integers. IDs above 2^53 − 1 lose precision in JavaScript. Ask the API to send them as strings, and type them as string.

Types disappear at runtime

This is the most important limitation. TypeScript types are erased when your code compiles. If the API changes shape, nothing stops bad data at runtime unless you check it. A lightweight type guard is often enough for small objects:

export function isAddress(value: unknown): value is Address {
  if (typeof value !== "object" || value === null) return false;
  const v = value as Record<string, unknown>;
  return (
    typeof v.type === "string" &&
    typeof v.city === "string" &&
    (v.postcode === undefined || typeof v.postcode === "string")
  );
}

For larger payloads, a schema validation library, or a JSON Schema generated from the same sample with the JSON Schema Generator, scales better.

Other ways to get types

  • OpenAPI or GraphQL code generators. If the API publishes an OpenAPI document or a GraphQL schema, generate types from that instead. The spec describes every possible value, not just one sample, so it is more accurate.
  • Editor extensions can paste JSON as types. Convenient, with similar limitations to any sample-based tool.
  • Writing them by hand is still reasonable for a handful of fields, and forces you to think about which fields are really optional.

Tips before you commit the types

  • Validate the sample first. A syntax error means no types; the JSON Validator shows exactly where it is.
  • Explore large responses in the JSON Viewer to decide which parts you actually need to type.
  • Rename generated types that ended up with awkward names, such as DataItem, to domain names your team uses.
  • Keep API types in one module and import them everywhere, so a change is made in one place.

Frequently asked questions

Should I use interface or type for JSON data?

Either works for object shapes. Interfaces are slightly friendlier for extending and in error messages; type aliases are required for unions and array roots. Consistency across the codebase matters more than the choice.

How are keys with spaces or dashes handled?

Keys that are not valid identifiers, such as "first-name", are written in quotes inside the interface. Access them with bracket notation: user["first-name"].

Why is my field typed as unknown?

The sample had only an empty array or no usable value for it. Add a sample with real items, or write the type by hand.

Does TypeScript check JSON at runtime?

No. Types exist only at compile time. Use type guards or a validation library to check data that comes from outside your program.

Can I generate types from multiple JSON samples?

Yes. Wrap the samples in one array and convert it. Items are merged, so fields missing from some samples become optional and differing value types become unions.

Try it now: JSON to TypeScript

Loading tool…