nullish vs optional vs nullable in Zod
Warning
This post is more than a year old. Information may be outdated.
.optional()
- Type:
T | undefined - Accepts:
undefined, missing key in objects, any value passing the base schemaT - Rejects:
null(unless separately allowed) - Shorthand for "allow the value to be absent"
.nullable()
- Type:
T | null - Accepts: explicit
null, any value passingT - Rejects:
undefined, missing key - Purpose: value must be supplied but may be
null
.nullish()
- Type:
T | null | undefined - Accepts:
null, , missing key, any value passing
Backlinks1
250509Comments