Sub topic 14
Input Types Versus Output Types
This chapter assumes you know basic TypeScript types, JavaScript objects, and how to import npm packages. It also assumes you understand that parse validates data at runtime.
A Zod schema can have two different types:
- The input type describes values the schema accepts.
- The output type describes the value returned after validation and transformation.
For schemas without transformations, these types are usually the same. They differ when a schema converts, supplies, or reshapes data.
Transforming a value
Consider a schema that accepts a numeric string but produces a number:
import { z } from "zod";
const UserId = z
.string()
.regex(/^\d+$/, "User ID must contain only digits")
.transform((value) => Number(value));
type UserIdInput = z.input<typeof UserId>;
// string
type UserIdOutput = z.output<typeof UserId>;
// number
const id = UserId.parse("42");
console.log(id); // 42
console.log(typeof id); // "number"Before parsing, "42" is a string. After parsing, the result is the number 42. The input and output types describe those two stages.
z.infer is an alias for the output type:
type InferredUserId = z.infer<typeof UserId>;
// numberThis means z.infer<typeof Schema> is normally the type your application should use after parsing.
Where the two types matter
Use z.input when describing data before parsing. Use z.output or z.infer when describing validated data inside your application.
function parseUserId(value: z.input<typeof UserId>): z.output<typeof UserId> {
return UserId.parse(value);
}
const id = parseUserId("42");
// id is a numberThe function accepts a string because that is the schema's input type. It returns a number because that is the schema's output type.
A common mistake is to annotate raw data with the output type:
// Incorrect for raw input:
// const rawId: z.output<typeof UserId> = "42";The string has not been transformed yet, so it is not an output value.
Defaults also change the output shape
A default value may be optional on input but guaranteed on output:
const Settings = z.object({
theme: z.enum(["light", "dark"]).default("light"),
});
type SettingsInput = z.input<typeof Settings>;
// { theme?: "light" | "dark" | undefined }
type SettingsOutput = z.output<typeof Settings>;
// { theme: "light" | "dark" }
const settings = Settings.parse({});
console.log(settings.theme); // "light"The caller may omit theme. The parsed result always contains it. This distinction helps TypeScript reflect what the rest of your program can safely assume.
Common errors
- Use
z.inputfor values received from a request, form, or other untrusted source. - Use
z.outputorz.inferafter parsing succeeds. - Do not use a type assertion to pretend raw data has already been transformed.
- Remember that
safeParsealso returns parsed output inresult.datawhen successful.
Exercise
What are the input and output types of z.string().transform((value) => value.length)?
Show answer
The input type is string, and the output type is number.