Skip to content

Optional

Optional: object

Defined in: Core/Optional.ts:28

Utilities for reading, overwriting, and transforming values along nullable or indexed paths via composable optionals.

andThen: <A, B>(inner) => <S>(outer) => Optional<S, B>

Composes two optionals sequentially, yielding None if either segment is absent.

A

B

Optional<A, B>

<S>(outer) => Optional<S, B>

Optional.andThenLens to compose an Optional with a Lens.

type Address = { landmark?: string };
type Account = { address?: Address };

const landmarkOptional = pipe(
  Optional.from.property<Account>()("address"),
  Optional.andThen(Optional.from.property<Address>()("landmark")),
);

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.

A

B

Lens<A, B>

<S>(outer) => Optional<S, B>

type Address = { city: string };
type Account = { address?: Address };

const cityOptional = pipe(
  Optional.from.property<Account>()("address"),
  Optional.andThenLens(Lens.from.property<Address>()("city")),
);

from: object

Constructors for creating Optional instances from accessors, property keys, or array indices.

accessors: <S, A>(get, set) => Optional<S, A> = makeAccessors

Constructs an Optional from a getter (returning Maybe) and a setter function.

S

A

(structure) => Maybe<A>

(value) => (structure) => S

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.
const firstChar = Optional.from.accessors(
  (text: string) => text.length > 0 ? Maybe.make.some(text[0]) : Maybe.make.none(),
  (char) => (text) => text.length > 0 ? char + text.slice(1) : text,
);

index: <A>(index) => Optional<readonly A[], 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.

A

number

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.
const firstItem = Optional.from.index<string>(0);

pipe(["ready", "running"], Optional.get(firstItem)); // Some("ready")
pipe([], Optional.get(firstItem));                   // None

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.

S

<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.
type Profile = { username: string; bio?: string };
const bioOptional = Optional.from.property<Profile>()("bio");

get: <S, A>(opt) => (structure) => Maybe<A>

Reads the focused value from a structure, returning Maybe.

S

A

Optional<S, A>

(structure) => Maybe<A>

type UserProfile = { bio?: string };
const bioOptional = Optional.from.property<UserProfile>()("bio");
const profile: UserProfile = { bio: "Platform engineer" };

pipe(profile, Optional.get(bioOptional)); // Some("Platform engineer")

getOrElse: <S, A>(opt) => <B>(fallback) => (structure) => A | B

Returns the focused value or a fallback default when the focus is absent.

S

A

Optional<S, A>

<B>(fallback) => (structure) => A | B

type UserProfile = { bio?: string };
const bioOptional = Optional.from.property<UserProfile>()("bio");
const profile: UserProfile = {};

pipe(profile, Optional.getOrElse(bioOptional)(() => "No bio provided"));
// "No bio provided"

match: <S, A>(opt) => <B>(cases) => (structure) => B

Pattern matches on an Optional focus using a named-case object.

S

A

Optional<S, A>

<B>(cases) => (structure) => B

type UserProfile = { bio?: string };
const bioOptional = Optional.from.property<UserProfile>()("bio");
const profile: UserProfile = { bio: "Staff engineer" };

pipe(
  profile,
  Optional.match(bioOptional)({
    none: () => "Empty profile",
    some: (bio) => `Bio: ${bio}`,
  }),
); // "Bio: Staff engineer"

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.

S

A

Optional<S, A>

(transform) => (structure) => S

type UserProfile = { bio?: string };
const bioOptional = Optional.from.property<UserProfile>()("bio");
const profile: UserProfile = { bio: "platform engineer" };

pipe(profile, Optional.modify(bioOptional)((bio) => bio.toUpperCase()));
// { bio: "PLATFORM ENGINEER" }

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.

S

A

Optional<S, A>

(value) => (structure) => S

type UserProfile = { bio?: string };
const bioOptional = Optional.from.property<UserProfile>()("bio");
const profile: UserProfile = { bio: "Platform engineer" };

pipe(profile, Optional.set(bioOptional)("Staff engineer"));
// { bio: "Staff engineer" }