0Pricing
TypeScript Academy · Lesson

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

  1. Zod Schema Basics
  2. Inferring Types from Schemas
  3. parse vs safeParse
  4. Composing and Refining Schemas
← Back to TypeScript Academy