In Zod 4, a user schema turns createdAt from an ISO string into a Date and defaults role when absent; what do z.input and z.output give, and which does each signature need?
answer
- two types per schema
- before parse versus after parse
- a default makes the input key optional
- pipe: first stage in, last stage out
basics
~20 sz.input is what parse accepts: createdAt a string, role an optional key. z.output, which z.infer aliases, is what parse returns: createdAt a Date, role required. Raw data before parsing takes the input type; code after parsing takes the output type.
solid answer
~40 sEvery Zod schema has two static types. `z.input<typeof User>` is what `.parse()` accepts: `createdAt: string` and `role?: "admin" | "member" | undefined`, because a default makes the key optional on the way in. `z.output<typeof User>`, the same as `z.infer`, is what `.parse()` returns: `createdAt: Date` and a required `role`. The rule is before versus after the parse: form state, fixtures and a request body being built take `z.input`; handlers and services working on the parsed value take `z.output`. A JSON round trip turns the `Date` back into a string, so the receiver sees the input shape and must parse again. In Zod 4 a `.default()` value is typed by the output side, because the default short-circuits the parse.
code
typescript · 22 linesimport * as z from "zod";
const User = z.object({
name: z.string(),
role: z.enum(["admin", "member"]).default("member"),
createdAt: z.iso.datetime().transform((s) => new Date(s)),
});
type UserInput = z.input<typeof User>;
// { name: string; role?: "admin" | "member" | undefined; createdAt: string }
type UserOutput = z.output<typeof User>;
// { name: string; role: "admin" | "member"; createdAt: Date }
function buildFixture(): UserInput {
return { name: "Ada", createdAt: "2026-01-15T10:30:00Z" };
}
function describe(user: UserOutput): string {
return `${user.name} (${user.role}) since ${user.createdAt.getFullYear()}`;
}
console.log(describe(User.parse(buildFixture()))); // Ada (member) since 2026go deeper
Recall that z.infer is the parsed result and that a second helper, z.input, describes what parse accepts.
Walk through the example field by field: the transform splits createdAt into string and Date, and the default makes role optional only on the input side.
Apply the before-or-after-parse rule to real signatures, including the JSON round trip that hands the receiver the input shape again.
Decide which of the two types a shared schema package exports for clients and which for services, and make the choice visible in the names.
## Two types per schema Every Zod schema carries **two** static types: - **`z.input<typeof S>`**: what `S.parse()` accepts, the shape of the raw data. - **`z.output<typeof S>`**: what `S.parse()` returns, the shape after Zod has run every transform and filled every default. `z.infer` is an alias of `z.output`. For a plain schema the two are identical, which is why many people never notice there are two. They split as soon as a schema **changes the value on the way through**: a `.transform()`, a `.default()`, a `z.preprocess()` or a codec. ## The worked example ```ts import * as z from "zod"; const User = z.object({ name: z.string(), role: z.enum(["admin", "member"]).default("member"), createdAt: z.iso.datetime().transform((s) => new Date(s)), }); type UserInput = z.input<typeof User>; // { name: string; role?: "admin" | "member" | undefined; createdAt: string } type UserOutput = z.output<typeof User>; // same as z.infer<typeof User> // { name: string; role: "admin" | "member"; createdAt: Date } ``` | Field | `z.input` | `z.output` | |---|---|---| | `name` | `string` | `string` | | `role` | optional key, `"admin" \| "member" \| undefined` | required key, `"admin" \| "member"` | | `createdAt` | `string` (an ISO date-time) | `Date` | Two mechanisms are at work: 1. A **pipe** (which `.transform()` builds) takes its input type from the first stage and its output type from the last. The ISO-string check is first, the `Date` conversion last. 2. A **default** widens the input with `undefined`, and inside `z.object()` that makes the key optional on the input side. On the output side the default guarantees a value, so the key is required and `undefined` is removed. ## Which type each signature needs The rule of thumb: **before the parse, use the input type; after the parse, use the output type.** | Place in the code | Helper | Why | |---|---|---| | Form state, a request body being built, a test fixture fed to `parse` | `z.input` | The data has not been converted yet | | A client that sends the user as JSON | `z.input` | JSON carries `createdAt` as a string | | A handler or service working on the parsed value | `z.output` / `z.infer` | `createdAt` is a `Date`, `role` is set | | Whatever reads the user back from a JSON response | `z.input`, then parse again | `JSON.stringify` turned the `Date` back into a string | The last row is the one people miss. A `Date` in the output does not survive serialisation: the receiving side sees the **input** shape and must parse it again to get the `Date` back. Form libraries that derive their value type from `z.input` follow the same logic, because a form holds values before they are parsed. ## Other schemas that split the two types The user example uses a transform and a default, but several other constructs make `z.input` and `z.output` differ: - **`z.coerce.*`**: in Zod 4 the input type of every coerced schema is `unknown`, because it accepts anything and converts it; the output is the target type. In Zod 3 the input of `z.coerce.string()` was `string`. - **`z.preprocess()`**: the input type is `unknown` by default; annotating the preprocessor's parameter narrows it, for example to `string | null | undefined`. - **`z.codec()`**: the input comes from the first schema (an ISO string, say) and the output from the second (a `Date`). - **`.optional()` and `.nullable()`** change both sides alike, so they do not split anything on their own. ## Defaults are typed by the output side In Zod 4, `.default()` **short-circuits**: when the input is `undefined`, Zod returns the default value as-is, without parsing it. So the default must be assignable to the **output** type. On the transformed `createdAt`, `.default(() => new Date(0))` compiles, and `.default("2026-01-01T00:00:00Z")` does not. If you want a raw value that runs through the parse, Zod 4 offers `.prefault()`, whose value is typed by the input side. This is a Zod 4 change: in Zod 3, `.default()` took an input-typed value and parsed it. ## Traps worth naming - Typing a form's state or a fixture as `z.infer` when the schema transforms: the compiler demands a `Date` where the user typed a string. - Typing a handler's **raw** body as `z.output` "because parse will fix it": the type lies until the parse has happened. - Assuming a defaulted key is optional in the result: it is required in `z.output`. - Assuming the `Date` survives a round trip through JSON: the receiver gets a string. - Forgetting that a codec (`z.codec`) splits the types the same way, with input and output read by the same two helpers. The question "which one does this signature need?" therefore has a mechanical answer: ask whether the value at that point has been through `parse` yet.
- What value may you pass to .default() on the transformed createdAt field in Zod 4?A `Date`, or a function returning one. In Zod 4 a default short-circuits the parse and is returned as-is, so it must match the output type; an ISO string fails to compile. To supply a raw string that runs through the ISO check and the transform, use `.prefault()`, which is typed by the input side. Zod 3 behaved the other way: its `.default()` took an input-typed value and parsed it.
- A frontend receives this user from a JSON API. Which type describes what arrives, and what must it do?The input type. `JSON.stringify` writes the `Date` as an ISO string, so the payload matches `z.input<typeof User>`, not the output. To work with a `Date` again, the frontend parses the payload with the same schema. Typing the response as `z.output` claims a `Date` that is really a string.
- z.infer gives the input type, so it is the right type for form state.
- A Zod schema's input and output types are always identical.
- A field with .default() is still an optional key in the parsed result.
- In Zod 4 a .default() value must match the input type, so pass the ISO string.
- Typing a raw request body as z.output is fine because parse will fix it later.
- The Date in z.output survives JSON, so the receiver also gets a Date.
Assumes Zod 4, where .default() short-circuits and takes an output-typed value and .prefault() takes the input-typed one, and z.coerce inputs are unknown; in Zod 3, .default() took an input-typed value and parsed it.