JSON to TypeScript

Paste JSON, get TypeScript interfaces.

TypeScript

Examples

Questions

When does a field become optional (key?:)?

In an array of objects, all items are merged into one interface. A key found in every item is required. A key missing from any item is optional. A key with different types becomes a union.

Why do two objects with the same shape get two interfaces?

Each interface is named from its own path, like BillingAddress and ShippingAddress. Merging them by shape would give one of them the wrong name. Merge them yourself if they are the same thing.

What happens to a key like "2fast" or "has space"?

It's quoted, like "2fast": string, as TypeScript requires for names that aren't valid identifiers. Reserved words like class are valid property names and stay unquoted.

What happens with duplicate keys?

The last one wins, the same as JSON.parse, so the type matches what your code will get.

What if the top level is an array or a plain value?

You get a type alias instead, like export type Root = number, or export type Root = RootItem[] with a RootItem interface.