Skip to content
Toolcroft

Developer Tools

JSON to TypeScript Interface Generator

Convert JSON objects to TypeScript interfaces automatically. Handles nested objects, arrays, null, and mixed types.

interface Root {
  id: number;
  name: string;
  email: string;
  active: boolean;
  score: number;
  tags: string[];
  address: Address;
}

interface Address {
  city: string;
  zip: string;
}

How JSON-to-TypeScript inference works

The tool analyzes your JSON structure and infers TypeScript interface or type definitions. Each JSON object becomes an interface; arrays become typed array types (T[]). Primitive JSON values map to TypeScript primitives:

JSON typeTypeScript type
stringstring
numbernumber
booleanboolean
nullnull or T | null
objectinline interface
arrayT[]

Readonly and optional properties

The generated interfaces use plain properties by default. After generation, consider:

  • Adding readonly to properties that should never be mutated after creation
  • Adding ? (e.g. name?: string) to fields that may be absent in some API responses
  • Using Readonly<T> or ReadonlyArray<T> utility types for deep immutability

Discriminated unions

When an API returns objects with different shapes based on a type or kind discriminant field, TypeScript's discriminated union pattern provides precise type narrowing. The auto-generated interface won't produce this pattern automatically; you'll need to refine the generated types manually after generation.

Runtime validation with Zod

TypeScript types are erased at runtime - they provide no protection against unexpected API responses. To validate actual API responses at runtime, pair your generated types with a runtime schema library:

  • Zod: the most popular TypeScript-first validation library; infers types from schemas so you maintain one source of truth
  • Valibot: similar API to Zod with a smaller bundle size
  • AJV: JSON Schema-based validation; integrates with existing JSON Schemas