Capítulo 1 de 61
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.
.parse(): validates input against a schema; throws on failure, returns typed data on success..optional(), .min(), etc.) returns a new schema instance instead of mutating the original.strict mode requirement: Zod expects "strict": true in tsconfig.json for correct type inference.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);
| Requirement | Value |
|---|---|
| TypeScript | v5.5+ (older versions may work, unsupported) |
| Install | npm install zod (also published as @zod/zod on jsr.io) |
| tsconfig | compilerOptions.strict: true required |
strict mode in tsconfig.json is not optional — inference correctness depends on it.llms.txt file for agent-assisted usage of its own docs.