.optional()
Makes a schema acceptundefined in addition to its base type.
Type Inference
The.optional() modifier affects both input and output types:
Unwrapping
You can remove the optional wrapper using.unwrap():
In Object Schemas
Fields with.optional() can be omitted from objects:
When a field is
.optional(), you can omit it entirely from the object. The field will be undefined in the parsed result..exactOptional()
Similar to.optional(), but enforces that undefined must be explicitly present in objects rather than being omitted.
Type Inference
Use Cases
.optional(): Use when a field can be omitted from objects.exactOptional(): Use when you want to distinguish between “field not provided” and “field set to undefined”