Optional
Optional:
object
Defined in: Core/Optional.ts:28
Utilities for reading, overwriting, and transforming values along nullable or indexed paths via composable optionals.
Type Declaration
Section titled “Type Declaration”andThen
Section titled “andThen”andThen: <
A,B>(inner) => <S>(outer) =>Optional<S,B>
Composes two optionals sequentially, yielding None if either segment is absent.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”Optional<A, B>
Returns
Section titled “Returns”<S>(outer) => Optional<S, B>
Optional.andThenLens to compose an Optional with a Lens.
Example
Section titled “Example”andThenLens
Section titled “andThenLens”andThenLens: <
A,B>(inner) => <S>(outer) =>Optional<S,B>
Composes an Optional with a Lens, producing an Optional. The Lens focuses within the value found by the Optional.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”Lens<A, B>
Returns
Section titled “Returns”<S>(outer) => Optional<S, B>
- Optional.andThen to compose two Optional paths.
- Lens.andThenOptional to compose a Lens with an Optional path.
Example
Section titled “Example”from:
object
Constructors for creating Optional instances from accessors, property keys, or array indices.
from.accessors
Section titled “from.accessors”accessors: <
S,A>(get,set) =>Optional<S,A> =makeAccessors
Constructs an Optional from a getter (returning Maybe) and a setter function.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”(structure) => Maybe<A>
(value) => (structure) => S
Returns
Section titled “Returns”Optional<S, A>
- Optional.from.property to create an Optional focused on an object property.
- Optional.from.index to focus on an array element by index.
Example
Section titled “Example”from.index
Section titled “from.index”index: <
A>(index) =>Optional<readonlyA[],A> =makeIndex
Creates an Optional that focuses on an element at a given index in an array. Returns None when the index is out of bounds; set is a no-op when out of bounds.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”number
Returns
Section titled “Returns”Optional<readonly A[], A>
- Optional.from.property to focus on an object property.
- Optional.from.accessors to construct an Optional from custom getter and setter functions.
Example
Section titled “Example”from.property
Section titled “from.property”property: <
S>() => <K>(key) =>Optional<S,NonNullable<S[K]>> =makeProperty
Creates an Optional that focuses on an optional or nullable property of an object. Call with the structure type first, then the key.
Type Parameters
Section titled “Type Parameters”S
Returns
Section titled “Returns”<K>(key) => Optional<S, NonNullable<S[K]>>
- Optional.from.accessors to construct an Optional from custom getter and setter functions.
- Optional.from.index to focus on an element of an array by index.
Example
Section titled “Example”get: <
S,A>(opt) => (structure) =>Maybe<A>
Reads the focused value from a structure, returning Maybe.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(structure) => Maybe<A>
- Optional.set to overwrite the focused value if present.
- Optional.modify to transform the focused value if present.
- Optional.getOrElse to extract the value with a fallback default.
- Optional.match to pattern match on present and absent states.
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
S,A>(opt) => <B>(fallback) => (structure) =>A|B
Returns the focused value or a fallback default when the focus is absent.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”<B>(fallback) => (structure) => A | B
- Optional.get to extract the value as a Maybe.
- Optional.match to pattern match on presence with custom handlers.
Example
Section titled “Example”match: <
S,A>(opt) => <B>(cases) => (structure) =>B
Pattern matches on an Optional focus using a named-case object.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”<B>(cases) => (structure) => B
- Optional.get to extract the value as a Maybe.
- Optional.getOrElse to extract the value with a default fallback.
Example
Section titled “Example”modify
Section titled “modify”modify: <
S,A>(opt) => (transform) => (structure) =>S
Applies a function to the focused value if it is present; returns the structure unchanged if the focus is absent.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(transform) => (structure) => S
- Optional.get to extract the focused value as a Maybe.
- Optional.set to overwrite the focused value directly.
Example
Section titled “Example”set: <
S,A>(opt) => (value) => (structure) =>S
Replaces the focused value within a structure. For indexed focuses, this is a no-op when the index is out of bounds.
Type Parameters
Section titled “Type Parameters”S
A
Parameters
Section titled “Parameters”Optional<S, A>
Returns
Section titled “Returns”(value) => (structure) => S
- Optional.get to extract the focused value as a Maybe.
- Optional.modify to transform the focused value if present.