Inferring Types from Schemas
Derive static types directly from Zod schemas.
Schemas Carry Type Information
A Zod schema knows the TypeScript type it validates. z.infer extracts that type, so you write the shape once.
import { z } from "zod";
const userSchema = z.object({ name: z.string(), age: z.number() });
type User = z.infer<typeof userSchema>;
// User is { name: string; age: number }The z.infer Utility
z.infer<typeof schema> produces the static type that the schema validates. Note the typeof: you pass the schema value.
import { z } from "zod";
const tagSchema = z.array(z.string());
type Tags = z.infer<typeof tagSchema>; // string[]All lessons in this course
- Zod Schema Basics
- Inferring Types from Schemas
- parse vs safeParse
- Composing and Refining Schemas