Skip to content

optional

2 overloads

optional<T>(primitive: ArrayTypedPrimitive<T>): OptionalArrayTypedPrimitive<T>

Wraps a primitive to accept missing or empty-string inputs, mapping them to undefined.

An array primitive keeps its string | string[] calling convention through the wrapper (an array input falls through to the wrapped parser untouched).

primitive ArrayTypedPrimitive<T>

returns OptionalArrayTypedPrimitive<T>

Example:

const maybeInt = optional(int); // TypedPrimitive<number | undefined>
const maybeIds = optional(array(int)); // OptionalArrayTypedPrimitive<number>
optional<T>(primitive: TypedPrimitive<T>): TypedPrimitive<T | undefined>

Wraps a primitive to accept missing or empty-string inputs, mapping them to undefined.

An array primitive keeps its string | string[] calling convention through the wrapper (an array input falls through to the wrapped parser untouched).

primitive TypedPrimitive<T>

returns TypedPrimitive<T | undefined>

Example:

const maybeInt = optional(int); // TypedPrimitive<number | undefined>
const maybeIds = optional(array(int)); // OptionalArrayTypedPrimitive<number>

Learn: Primitives and builders

Last updated: