Impossible branches
Model an impossible branch in a union or conditional schema.
Zod · Module
z.never() creates a Zod schema that rejects every input and infers the TypeScript type never. Use it to model an impossible schema branch.
z.never() creates a schema whose inferred TypeScript type is never and whose validation always fails. It is useful when a schema branch must be impossible; unlike z.NEVER, which is a special transform-return value, this is a schema factory.
z.never()A Zod schema that accepts no values and infers the TypeScript type never.
never.z.never() factory.z.NEVER value is used in transforms, not as a replacement for the schema factory.import { z } from "zod";
const NoValue = z.never();
console.log(NoValue.safeParse("anything").success); // false
Creates a schema that rejects every input.
Model an impossible branch in a union or conditional schema.
Explicitly prevent all runtime values from satisfying a placeholder schema.
Represent an uninhabitable TypeScript output type alongside runtime validation.
z.never() validate?z.never() returns a schema that accepts no values. Its inferred TypeScript type is never, making it suitable for representing an impossible branch.
Follow-up: How does it differ from z.NEVER? z.never() is a schema, while z.NEVER is a special value that can be returned from a transform without changing the inferred output type.
Use z.never() when a schema branch should be impossible and must reject all runtime inputs. Parsing against it fails regardless of the supplied value.
Follow-up: What runtime behavior should callers expect? They should handle a failed parse or use a safe parsing method because successful parsing cannot occur.
z.never()?The declaration is z.never(), and the documentation shows its inferred type as never. It is distinct from ordinary schemas because its accepted set is empty.
Follow-up: Can it produce a successful parsed value? No; the documented contract is that no value passes validation.
z.NEVERz.any()z.unknown()