Zod: TypeScript-First Schema Validation

Zod creates a single source of truth for your data's shape, providing both runtime validation and static TypeScript type inference from one definition. Use it to parse API inputs or form data, ensuring data is correct and typed.
Why it exists
In TypeScript, types are erased at compile time. This means a type User = { name: string } offers no protection against an API returning { name: 123 } at runtime. Zod was created to bridge this gap, allowing you to define a single schema that both validates runtime data and infers a static TypeScript type.
The mental model
Think of a Zod schema as a blueprint and a bouncer for your data. The blueprint (z.object({ name: z.string() })) defines the exact structure. The bouncer (.parse(data)) checks every piece of incoming data against that blueprint. If it matches, it's let in as a fully-typed object; if not, it's rejected with an error.
How it works
You import Zod and use its functions like z.string(), z.number(), and z.object() to compose a schema. This schema object has a .parse() method. When you call schema.parse(untrustedData), Zod inspects the data. If it's valid, it returns the data, now with its TypeScript type correctly inferred from the schema. If the data is invalid, .parse() throws a detailed ZodError by default.
When to use it
Use Zod at the boundaries of your application where you receive untrusted data. This is ideal for validating API request bodies, query parameters, environment variables, or form submissions. It integrates seamlessly with frameworks like tRPC and React Hook Form to provide end-to-end type safety.
When not to use it
For simple, internal data transformations where types are already guaranteed by the TypeScript compiler, Zod might be overkill. While it works in plain JavaScript, its primary benefit is the combination of validation and static type inference, which is lost without TypeScript.
One canonical example
First, you define a schema, such as const UserSchema = z.object({ name: z.string() });. This declares that a valid user object must have a name property that is a string. Next, you take some untrusted data, like const input = { name: "Alice" };. To validate and get a typed result, you call const validatedUser = UserSchema.parse(input);. If input conforms to the schema, validatedUser is returned and its type is automatically inferred as { name: string; }. If the input were { name: 123 }, the .parse() call would throw an error that you should handle with a try/catch block.
Interview question
What is the primary benefit of using Zod in a TypeScript application for handling external data?
- a.It automatically transforms invalid incoming data to conform to the defined schema.
- b.It provides a unified definition for both runtime data validation and static TypeScript type inference.Correct
- c.It generates TypeScript types dynamically from raw runtime data without a schema.
- d.It ensures that TypeScript types are preserved and enforced at runtime.
Why? this is the answer
Zod's key strength is creating a single schema that simultaneously validates data at runtime and allows TypeScript to infer its static type, bridging the gap between compile-time types and dynamic data. TypeScript types are erased at runtime, so Zod does not preserve them but rather provides a new, validated type.
Just read this? Test yourself on what you have been reading.
Read the original → zod.dev
- #typescript
- #validation
- #schema
- #api
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