Combining Schemas in Zod

From the Zod cheat sheet ยท Unions, Intersections & Discriminated Unions ยท verified Jul 2026

Combining Schemas

Union, intersection, and discriminated union patterns

typescript
// Union (any of)
const StringOrNumber = z.union([z.string(), z.number()]);
// or: z.string().or(z.number())

// Discriminated union (faster, better errors)
const Shape = z.discriminatedUnion("kind", [
  z.object({ kind: z.literal("circle"), radius: z.number() }),
  z.object({ kind: z.literal("square"), size: z.number() }),
]);
๐Ÿ’ก Use discriminatedUnion when objects share a common literal field โ€” much faster than union
โšก Discriminated unions give clearer error messages by checking only the matching variant
๐Ÿ“Œ Prefer .extend() over intersection for combining object schemas โ€” same result, cleaner
๐ŸŸข z.union() tries each schema in order โ€” first match wins
unionintersectiondiscriminated
Back to the full Zod cheat sheet