JSON Input
paste or type · or drop a .json file
TypeScript
Valid · 9 lines · 140 chars json-to-ts

TypeScript interfaces vs Zod schemas

TypeScript interfaces exist only at compile time: they validate types during development and are erased at runtime. Zod schemas are runtime objects that can parse and validate data, generate TypeScript types via z.infer<>, and throw detailed errors when data doesn't match.

Use interfaces when you control the data source and just need IDE support. Use Zod (or similar: Valibot, Yup, ArkType) when you receive data from untrusted sources like APIs, form inputs, or config files.

How type inference works

This tool traverses your JSON value recursively. Arrays are typed by their elements: if all elements share the same structure, a single type is inferred; mixed arrays produce union types (string | number). Missing keys across objects are marked optional.

primitivestring, number, boolean, null
objectinterface or z.object() — one field per key
optional keykey?: Type or .optional()

Next steps after generation

The generated types are a starting point. For stricter validation, refine Zod schemas with constraints like z.string().email(), z.number().positive(), or z.array(...).min(1).

For TypeScript interfaces, consider adding readonly modifiers and replacing any with proper union types. If your JSON comes from an API with a published OpenAPI spec, tools like openapi-typescript or Orval can generate more complete, versioned types directly from the spec.