Capítulo 1 de 61

Chapter 1: Introduction & Installation

Core Idea

Zod is a TypeScript-first schema validation library: you define a schema once, and get both runtime validation and a statically inferred TypeScript type from it, with zero external dependencies.

Key Concepts

  • Schema: an object describing a type (primitive, object, array, union, etc.) that can validate unknown input at runtime.
  • .parse(): validates input against a schema; throws on failure, returns typed data on success.
  • Immutable API: every schema method (.optional(), .min(), etc.) returns a new schema instance instead of mutating the original.
  • Zero dependencies, ~2kb gzipped core: designed to be safe to add to any project without bundle-size concerns.
  • Built-in JSON Schema conversion: schemas can be converted to and from JSON Schema.
  • strict mode requirement: Zod expects "strict": true in tsconfig.json for correct type inference.

Code Examples

import * as z from "zod";

const User = z.object({
  name: z.string(),
});

// some untrusted data...
const input = { /* stuff */ };

// the parsed result is validated and type safe!
const data = User.parse(input);

// so you can use it with confidence :)
console.log(data.name);
  • What it demonstrates: the core workflow — define a schema, parse unknown input, get back typed data.

Reference Tables

RequirementValue
TypeScriptv5.5+ (older versions may work, unsupported)
Installnpm install zod (also published as @zod/zod on jsr.io)
tsconfigcompilerOptions.strict: true required

Key Takeaways

  1. Zod schemas double as runtime validators and TypeScript type sources — no separate type definitions needed.
  2. The API is immutable: chaining methods builds new schemas rather than mutating in place.
  3. strict mode in tsconfig.json is not optional — inference correctness depends on it.
  4. Zod ships an MCP server and an llms.txt file for agent-assisted usage of its own docs.

Connects To

  • Defining Schemas: the next step after installation — see the chapters on primitives, objects, and composition.
  • Ecosystem & Community Resources: for framework integrations (tRPC, React Hook Form) built on top of Zod.