Skip to content

Optional

Optional<S, A> = object

Defined in: Core/Optional.ts:28

Focuses on a property or indexed value A inside a parent structure S that may or may not be present, providing a composable way to safely read, overwrite, and immutably transform nested nullable data.

Reads return Maybe<A>. Overwriting or modifying returns the original structure unchanged if the path is broken.

Compose with other Optionals via andThen, or with a Lens via andThenLens.

type Profile = { username: string; bio?: string };

const bioOptional = Optional.from.property<Profile>()("bio");
const profile: Profile = { username: "alex", bio: "Engineer" };

pipe(profile, Optional.get(bioOptional)); // Some("Engineer")
pipe(profile, Optional.set(bioOptional)("Staff Engineer"));
// { username: "alex", bio: "Staff Engineer" }
pipe(profile, Optional.modify(bioOptional)((bio) => bio.toUpperCase()));
// { username: "alex", bio: "ENGINEER" }

S

A

readonly get: (structure) => Maybe<A>

Defined in: Core/Optional.ts:29

S

Maybe<A>


readonly set: (value) => (structure) => S

Defined in: Core/Optional.ts:30

A

(structure) => S