← All guides
Guide

How JSON to TypeScript Interface Generation Works

Generating a TypeScript interface from a JSON sample is type inference from a single example, not from a schema — which means it’s a strong starting point, not a guaranteed-correct contract. The generator walks the JSON tree and maps each value to a TypeScript type: strings to string, whole numbers and decimals to number, true/false to boolean, and null to the literal type null.

How nested objects get named

A plain JSON object has no name of its own — only the key that points to it does. So a "user": { "name": ..., "email": ... } field produces a separate interface User { ... }, referenced from the parent as user: User. If the same object shape appears again under a different key, the generator reuses the existing interface instead of duplicating it.

Where it falls short of a real schema

Every field in the sample becomes a required field in the interface, because a single example can’t tell you which fields are actually optional across every possible response. Arrays are typed from their first element only, so a sparsely-populated array (e.g. one entry that’s null, mixed in with objects) may need a manual union-type adjustment afterward.

Try it on your own payload with the JSON to TypeScript generator, or generate a validation schema instead with JSON to JSON Schema.

Frequently asked questions

Will every field be marked required?

Yes, by default — manually add ? after any field name that’s actually optional in your real API responses.

Does it work for arrays of primitives?

Yes, an array of strings becomes string[], an array of numbers becomes number[], and so on.

What if two nested objects have the same shape?

The generator detects the duplicate shape by its sorted key signature and reuses one interface instead of generating a second identical one.