Basic Usage
The.default() modifier provides a fallback value when the input is undefined.
.default() only applies to undefined values, not null or other invalid inputs.Type Inference
The.default() modifier changes the input type to include undefined, but the output type excludes it:
Dynamic Defaults
You can provide a function that generates the default value:In Object Schemas
Fields with.default() become optional in the input type:
Shallow Cloning
Default values are shallow cloned to prevent reference sharing:Chaining with Optional
Chaining with Transform
When chaining
.default() after .transform(), the default value is applied at the output stage, so it bypasses the transformation.Nested Defaults
Direction-Aware Behavior
Defaults only apply during parsing (forward direction), not during encoding:Unwrapping
You can remove the default wrapper:Use Cases
- Configuration objects: Provide sensible defaults for optional config
- API responses: Fill in missing fields with default values
- Form data: Supply default values for unpopulated fields
- Database records: Set default values for optional columns