JSON to TypeScript: Automatic Types

JSON to TypeScript: Automatic Types

Hand-writing TypeScript types for an API response is the developer equivalent of retyping a printed document. The JSON is the spec — the tool should generate the types.

Our JSON to TypeScript converter turns a JSON sample into interfaces in your browser.

What it produces

Paste this:

{ "user": { "name": "Ada", "age": 36 } }

And get a TypeScript interface:

interface Root {
  user: {
    name: string;
    age: number;
  };
}

Types are inferred from values: strings become string, numbers become number, booleans become boolean, arrays become arrays of the inferred element type.

Why generated beats handwritten

  • Speed: a 50-field response generates in a second instead of half an hour of typing.
  • Accuracy: no missed fields, no wrong types — the sample defines them exactly.
  • Consistency: field names match the API byte for byte.

The caveats

  • Types come from your sample: if the API sometimes returns null for age, a sample with 36 generates number — and null crashes at runtime. Union types (number | null) are your job when the API is inconsistent.
  • Empty arrays: [] gives the converter nothing to infer. Feed a populated sample, or widen the type by hand.
  • Optional fields: converters infer all fields as required unless told otherwise. Check the tool’s options — marking fields optional matters for real APIs.

The workflow

  1. Copy a representative response from your API (a real one, with data — use the JSON formatter to read it first if it’s minified).
  2. Paste into the JSON to TypeScript converter and generate interfaces.
  3. Paste into your codebase, then adjust unions and optionals to match reality.

Generated types are a starting point that’s 95% right. Hand-written types are 100% effort and still miss fields. Take the 95%.