Zod: Validate Data, Infer Types

Zod acts as a bouncer for your data, validating it against a schema you define and inferring TypeScript types automatically. It's essential for validating API inputs or form submissions. The main footgun is forgetting to enable "strict": true in your tsconfig.
Why it exists
TypeScript types are erased at compile time, leaving your application vulnerable to incorrect data at runtime, like from an API or user input. Zod solves this by providing a single source of truth: a schema that both validates runtime data and generates static TypeScript types, preventing mismatches between your types and your validation logic.
The mental model
Think of Zod as a contract for your data. You write the contract (the schema) once. When data arrives, Zod checks if it fulfills the contract. If it does, Zod gives you back the data, now with a TypeScript type guarantee that it matches the contract. If it fails, Zod throws a detailed error explaining exactly which part of the contract was broken. It bridges the gap between runtime uncertainty and compile-time safety.
How it works
You import Zod and use its functions like z.string(), z.number(), and z.object() to build a schema. This schema is a runtime object, not just a type. To validate data, you call the .parse() method on your schema with the untrusted input. If validation passes, it returns the data, now correctly typed. If it fails, .parse() throws an error. The magic is that TypeScript can infer a static type directly from your runtime schema definition.
When to use it
Use Zod at the boundaries of your application where untrusted data enters. This includes validating incoming API request bodies in a server (like a Next.js API route), parsing form submissions from a user (often with React Hook Form), or checking environment variables on application startup. It's a core part of the tRPC ecosystem for end-to-end typesafe APIs.
When not to use it
For simple, internal-only data transformations where the data source is already trusted and typed, Zod might be overkill. If you're not using TypeScript, you lose its main benefit of static type inference. Zod is not a database schema migration tool; it's for application-layer data validation.
One canonical example
A common use case is validating a user registration form submission on a server. First, define the schema: const UserSchema = z.object({ email: z.string().email(), password: z.string().min(8) }); Then, in your handler, you can parse the input: const validatedData = UserSchema.parse(request.body); Now, you can safely use validatedData.email without any extra type checks, because Zod has guaranteed its shape and type.
Interview question
What core limitation of TypeScript does Zod primarily address in an application?
- a.TypeScript's type system being entirely erased at runtime.Correct
- b.The absence of a standard way to manage database schema changes.
- c.The complexity of defining nested object types and arrays in TypeScript.
- d.The inability to automatically generate API client code from server-side types.
Why? this is the answer
Zod's primary purpose, as detailed in the card, is to provide runtime validation because "TypeScript types are erased at compile time," making applications vulnerable to untrusted data. Option D describes a benefit Zod enables in certain ecosystems, not the fundamental limitation of TypeScript's type system that Zod directly addresses.
Just read this? Test yourself on what you have been reading.
Read the original → zod.dev
- #typescript
- #validation
- #schema
- #react
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.
See open roles