Model transformed inputs
A schema accepts one type but parses to another, so application types must describe the pre-parse value.
Zod · Type Alias
z.input extracts the TypeScript input type of a Zod schema. It is useful when a schema’s accepted input differs from its parsed output, such as with coercion or preprocessing.
z.input is a TypeScript utility type that derives the input type associated with a Zod schema. It is especially useful when a schema transforms or coerces values, because the accepted input can differ from the parsed output.
z.input<typeof schema>| Name | Type | Description |
|---|---|---|
schema | typeof schema | A Zod schema type, supplied through typeof schema, whose input type should be extracted. |
The TypeScript input type associated with the supplied Zod schema.
z.input to a schema value with typeof to extract its accepted TypeScript input type.unknown as their default input type.unknown.unknown for input unless the preprocessor input is narrowed.z.input<typeof schema> is unknown unless the schema's input generic is made more specific.unknown input type because the preprocessor may receive arbitrary values.import { z } from "zod";
const schema = z.coerce.number();
type Input = z.input<typeof schema>;
type Output = z.output<typeof schema>;
const value: Input = "42";
const parsed: Output = schema.parse(value);
console.log(parsed); // 42z.input extracts the TypeScript type accepted by a schema, while parsing produces the schema's output type.
A schema accepts one type but parses to another, so application types must describe the pre-parse value.
A coercion schema receives external values whose compile-time type is broader than its output.
A preprocessing function needs an explicit input annotation to avoid an overly broad inferred type.
z.input do?z.input<typeof schema> extracts the type accepted as input by a schema. It differs from z.infer<typeof schema> when transformations or coercion make the input and output types different.
Follow-up: How would you obtain the parsed type? Use z.output<typeof schema>; z.infer<typeof schema> is also the schema's output type.
For z.coerce schemas, the input type defaults to unknown, while the output type reflects the coerced schema. This models the fact that coercion can accept arbitrary runtime input.
Follow-up: How can the input be narrowed? Pass a more specific generic parameter to the coercion helper.
unknown?A z.preprocess() schema also defaults to unknown input because preprocessing is designed to handle arbitrary values. The preprocessor's parameter can be annotated directly to narrow that input type.
Follow-up: Why can input and output differ? Preprocessing or transformation changes the value before or during parsing, so the accepted and produced types need not match.
z.outputz.inferz.coercez.preprocessZodType