npmzodv4.3.6
npmBeginner-friendly

zod

TypeScript-first schema declaration and validation library with static type inference

validationtypescriptschemaruntime-typesparsingtypeinferenceruntime-validationschema-validationstatic-types
The Problem

TypeScript types exist only at compile time โ€” they're completely erased at runtime. When you receive JSON from an API, there's no guarantee it matches your TypeScript interface. You could get a null where you expect a string and TypeScript can't catch it. Zod bridges the gap by providing runtime validation that produces TypeScript types.

Mental Model

A Zod schema is both a validator and a type: z.object({ name: z.string(), age: z.number() }) defines a schema you can call .parse() on at runtime AND extracts a TypeScript type from it with z.infer<typeof schema>. One definition, both compile-time and runtime safety.

60-second start

Install zod

npm install zod
import { } from 'zod';

Use it when

Use Zod at every trust boundary: parsing API responses, validating form inputs, reading environment variables, or loading config files. Any time external data enters your application, a Zod schema is the right gatekeeper.

Skip it when

Zod adds bundle size (around 12KB gzipped). For purely server-side validation where bundle size doesn't matter but performance does, Typebox or Ajv (JSON Schema) are faster. If you're already using Pydantic on the Python side, you may want to generate Zod schemas from it rather than maintain both.

Alternatives
valibotSimilar API to Zod but tree-shakable โ€” much smaller bundle for browser apps that only use a subset
yupOlder schema validation library; less TypeScript-native but battle-tested in form libraries
typeboxJSON Schema-based; generates both TypeScript types and JSON Schema from one definition โ€” useful for OpenAPI

Ready to explore the full API?

See every class, function, and relationship in an interactive graph.