Model transformed values
A schema transforms a string into a number, and application code needs the resulting numeric type.
Zod · Type Alias
z.output<typeof schema> extracts the TypeScript type produced by a Zod schema. It is especially useful when parsing transforms the input into a different output type.
z.output<T> is a type-level utility that extracts the inferred output type from a Zod schema. It matters when a schema transforms or otherwise maps its input into a different parsed value, so the output type can differ from the input type.
z.output<typeof schema>| Name | Type | Description |
|---|---|---|
T | Zod schema type | A Zod schema type whose inferred parsed result should be extracted. |
The schema's inferred output TypeScript type.
z.output<typeof schema> is equivalent to z.infer<typeof schema>.z.input<typeof schema> when you need the schema's inferred input type.z.input and z.output independently.import { z } from "zod";
const schema = z.string().transform((value) => value.length);
type Result = z.output<typeof schema>;
const result: Result = schema.parse("Zod");
console.log(result); // 3z.output<typeof schema> extracts the TypeScript type produced by parsing the schema.
A schema transforms a string into a number, and application code needs the resulting numeric type.
An API boundary needs separate static types for raw input and validated output.
A codebase wants type declarations that clearly describe parsed results.
z.output do?z.output<typeof schema> extracts the schema's inferred output type at compile time. For an ordinary schema such as z.string(), that type is string; for a transforming schema, it is the transformed result.
Follow-up: How would you obtain the corresponding input type? Use z.input<typeof schema>, which can differ from the output type when the schema transforms values.
A transform can make the schema accept one type and produce another. For example, a string-to-number transform has string as its input type and number as its output type, so z.output captures number.
Follow-up: Why is this distinction useful? It lets APIs type raw data and parsed data separately instead of incorrectly treating them as the same type.
z.output relate to z.infer?z.infer<typeof schema> is equivalent to z.output<typeof schema> and returns the schema's output type. z.output is often clearer when the code is discussing the result produced by parsing.
Follow-up: What utility should be used for the pre-transform type? Use z.input<typeof schema> rather than z.infer, because z.infer denotes the output type.
z.inputz.infer.transform()z.preprocess()z.codec