TypeScript · Middle · Question 57 of 75

How do you derive a TypeScript type from a Zod schema?

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

Explanation

Validation at the boundary (API, forms) plus inferred types avoids drift between runtime checks and compile-time types.

Further reading

Problem with a question

TypeScript · Middle · question 57

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz