Skip to content
gohacks.dev
Standard Library

JSON to TypeScript Types

Paste a JSON response and get TypeScript interfaces or type aliases, with nested objects, optional keys and nullable fields inferred.

Free · No sign-up · Your inputs stay in your browser

Input

Generate TypeScript types from JSON

Output

TypeScript

312 characters

Types are inferred from this one sample. A key missing from some items of an array becomes optional (?), and a null becomes | null. A field that is always null in the sample cannot be typed beyond null: give the sample a real value.

Runs in this browser. Nothing you paste is sent to a server.

One sample is a guess

A type generated from one response describes that response. Fields that were null in the sample, optional fields that happened to be present, and enums that happened to hold one value are all guessed wrong. Use the output as a first draft and correct it against the API's documentation or schema.

Types are not validation

TypeScript types disappear at runtime, so const user: User = await res.json() asserts a shape it never checks. For data crossing a trust boundary, validate it with a schema library and derive the type from the schema; the JSON to Zod tool on this site generates one from the same sample.

Interface or type?

For object shapes they are interchangeable in almost every case. The TypeScript handbook notes that interfaces can be extended by declaration merging and types cannot; pick one for a codebase and stay with it.

Share

Frequently asked questions

How are optional fields detected?
When the JSON is an array of objects, a key that is missing from at least one of them is marked optional with ?.
Why is a field typed as null?
Every value it had in the sample was null, so there was nothing else to infer. Put a real value in the sample, or correct the type by hand.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools