Code Reference

Infer Types

Zod · Reference cheat sheet

Infer Types

Zod · Reference cheat sheet


📋 Overview

Zod schemas are a single source of truth: validate at runtime and derive TypeScript types with z.infer, z.input, and z.output.

🔧 Core concepts

HelperYields
z.infer<typeof S>Output type (usual choice)
z.input<typeof S>Type before transforms
z.output<typeof S>Type after transforms
z.infer on objectsNested object types
PatternBenefit
Schema-firstNo drift between types and validators
Shared DTO moduleAPI + client agree
Env schemaTyped process.env

💡 Examples

Basic infer:

const User = z.object({
  id: z.string(),
  email: z.string().email(),
});
type User = z.infer<typeof User>;
// { id: string; email: string }

Input vs output:

const S = z.string().transform((s) => s.length);
type In = z.input<typeof S>;   // string
type Out = z.output<typeof S>; // number

Function boundary:

function save(user: z.infer<typeof User>) {
  /* ... */
}

⚠️ Pitfalls

  • Don't hand-write a duplicate interface that can drift — infer from the schema.
  • Optional fields (?) vs | undefined differ slightly in TS excess property checks.
  • Circular types need z.lazy — plain infer won't recurse infinitely by itself.

On this page