Skip to content

Maybe

Maybe: object

Defined in: Core/Maybe.ts:26

apply: <A>(arg) => <B>(maybe) => Maybe<B>

Applies a function wrapped in a Maybe to a value wrapped in a Maybe.

A

Maybe<A>

<B>(maybe) => Maybe<B>

Maybe.map to apply a plain unary function to a Maybe value.

const add = (a: number) => (b: number) => a + b;
pipe(
  Maybe.make.some(add),
  Maybe.apply(Maybe.make.some(5)),
  Maybe.apply(Maybe.make.some(3))
); // Some(8)

assign: <K, A, B>(key, transform) => (maybe) => Maybe<A & { [P in string]: B }>

Evaluates a new Maybe using the current accumulator and attaches the output to a new key.

K extends string

A

B

K

(value) => Maybe<B>

(maybe) => Maybe<A & { [P in string]: B }>

Maybe.assignTo to initialize a pipeline accumulator record.

pipe(
  Maybe.make.some({ a: 1 }),
  Maybe.assign("b", ({ a }) => Maybe.make.some(a + 1))
); // Some({ a: 1, b: 2 })

assignTo: <K>(key) => <A>(maybe) => Maybe<{ [P in string]: A }>

Converts a Maybe value into an object containing a single property. Initiates the pipeline accumulator record.

K extends string

K

<A>(maybe) => Maybe<{ [P in string]: A }>

Maybe.assign to append additional fields to an accumulator record.

pipe(Maybe.make.some(42), Maybe.assignTo("value")); // Some({ value: 42 })

chain: <A, B>(transform) => (maybe) => Maybe<B>

Chains Maybe computations. If the first is Some, passes the value to transform. If the first is None, propagates None.

A

B

(value) => Maybe<B>

(maybe) => Maybe<B>

Maybe.map for transforming with plain non-optional functions.

const parseNumber = (s: string): Maybe<number> => {
  const n = parseInt(s, 10);
  return isNaN(n) ? Maybe.make.none() : Maybe.make.some(n);
};

pipe(Maybe.make.some("42"), Maybe.chain(parseNumber)); // Some(42)
pipe(Maybe.make.some("abc"), Maybe.chain(parseNumber)); // None

filter: {<A, B>(refinement): (maybe) => Maybe<B>; <A>(predicate): (maybe) => Maybe<A>; }

Filters a Maybe based on a predicate or type guard. Returns None if the predicate returns false or if the Maybe is already None.

<A, B>(refinement): (maybe) => Maybe<B>

A

B

(value) => value is B

(maybe) => Maybe<B>

<A>(predicate): (maybe) => Maybe<A>

A

(value) => boolean

(maybe) => Maybe<A>

  • Maybe.from.Predicate to lift a raw value into a Maybe based on a predicate.
  • Maybe.map to transform the value without filtering.
pipe(Maybe.make.some(5), Maybe.filter(n => n > 3)); // Some(5)
pipe(Maybe.make.some(2), Maybe.filter(n => n > 3)); // None
pipe(Maybe.make.some("hi"), Maybe.filter((x): x is string => typeof x === "string")); // Some("hi")

from: object

nullable: <A>(value) => Maybe<NonNullable<A>>

Creates a Maybe from a nullable value. Returns None if the value is null or undefined, Some otherwise.

A

A

Maybe<NonNullable<A>>

Maybe.to.nullable to extract a value back into a nullable primitive.

Maybe.from.nullable(null); // None
Maybe.from.nullable(42); // Some(42)

Predicate: {<A, B>(refinement): (value) => Maybe<B>; <A>(predicate): (value) => Maybe<A>; }

Creates a Maybe from a predicate or type guard applied to a value. Returns Some if the predicate passes, None otherwise.

<A, B>(refinement): (value) => Maybe<B>

A

B

(value) => value is B

(value) => Maybe<B>

<A>(predicate): (value) => Maybe<A>

A

(value) => boolean

(value) => Maybe<A>

Maybe.filter to filter an existing Maybe with a predicate or type guard.

Maybe.from.Predicate((n: number) => n >= 18)(21); // Some(21)
Maybe.from.Predicate((n: number) => n >= 18)(15); // None

pipe("hello", Maybe.from.Predicate((s: string) => s.length > 0)); // Some("hello")
pipe("", Maybe.from.Predicate((s: string) => s.length > 0));      // None

Result: <E, A>(result) => Maybe<A>

Creates a Maybe from a Result. Ok becomes Some, Err becomes None (the error is discarded).

E

A

Result<E, A>

Maybe<A>

Maybe.to.Result to convert a Maybe to a Result with an explicit error.

Maybe.from.Result(Result.make.ok(42)); // Some(42)
Maybe.from.Result(Result.make.err("oops")); // None

Validation: <E, A>(validation) => Maybe<A>

Creates a Maybe from a Validation. Passed becomes Some, Failed becomes None (the errors are discarded).

E

A

Validation<E, A>

Maybe<A>

Maybe.to.Validation to convert a Maybe to a Validation with an explicit error.

Maybe.from.Validation(Validation.make.passed(42)); // Some(42)
Maybe.from.Validation(Validation.make.failed("oops")); // None

getOrElse: <B>(fallback) => <A>(maybe) => B | A

Returns the value inside a Maybe, or a default value if None. The fallback is a thunk () => B — evaluated only when the Maybe is None. The fallback can be a different type, widening the result to A | B.

B

() => B

<A>(maybe) => B | A

  • Maybe.match to handle both the Some and None cases.
  • Maybe.to.nullable to extract the value as a nullable primitive.
pipe(Maybe.make.some(5), Maybe.getOrElse(() => 0)); // 5
pipe(Maybe.make.none(), Maybe.getOrElse(() => 0)); // 0
pipe(Maybe.make.none<string>(), Maybe.getOrElse(() => null)); // null — typed as string | null

is: object

none: <A>(maybe) => maybe is None = isNone

Type guard that checks if a Maybe is None.

A

Maybe<A>

maybe is None

Maybe.is.some to check if a Maybe is a Some variant containing a value.

const value = Maybe.make.none();
if (Maybe.is.none(value)) {
  console.log("No value present");
}

some: <A>(maybe) => maybe is Some<A> = isSome

Type guard that checks if a Maybe is Some.

A

Maybe<A>

maybe is Some<A>

Maybe.is.none to check if a Maybe is a None (empty) variant.

const value = Maybe.make.some(42);
if (Maybe.is.some(value)) {
  console.log(value.value); // 42
}

make: object

none: () => None = makeNone

Creates a None (empty Maybe).

None

Maybe.make.none(); // None

some: <A>(value) => Some<A> = makeSome

Creates a Some containing the given value.

A

A

Some<A>

Maybe.make.some(42); // Some(42)

map: <A, B>(transform) => (maybe) => Maybe<B>

Transforms the value inside a Maybe if it exists.

A

B

(value) => B

(maybe) => Maybe<B>

Maybe.chain for functions that return a Maybe.

pipe(Maybe.make.some(5), Maybe.map(n => n * 2)); // Some(10)
pipe(Maybe.make.none(), Maybe.map(n => n * 2)); // None

match: <A, B>(cases) => (maybe) => B

Pattern matches on a Maybe, returning the result of the matching case.

A

B

() => B

(value) => B

(maybe) => B

Maybe.getOrElse to extract the value with a fallback.

pipe(
  optionUser,
  Maybe.match({
    some: user => `Hello, ${user.name}`,
    none: () => "Hello, stranger"
  })
);

recover: <B>(fallback) => <A>(maybe) => Maybe<B | A>

Recovers from a None by providing a fallback Maybe. The fallback can produce a different type, widening the result to Maybe<A | B>.

B

() => Maybe<B>

<A>(maybe) => Maybe<B | A>

Maybe.getOrElse to unwrap a Maybe with a fallback value instead of another Maybe.

pipe(Maybe.make.none(), Maybe.recover(() => Maybe.make.some(42))); // Some(42)
pipe(Maybe.make.some(10), Maybe.recover(() => Maybe.make.some(42))); // Some(10)

sequence: object

Result: <E, A>(maybe) => Result<E, Maybe<A>>

Swaps the outer Maybe and inner Result context. Some(Ok(a)) becomes Ok(Some(a)), Some(Err(e)) becomes Err(e), and None becomes Ok(None).

E

A

Maybe<Result<E, A>>

Result<E, Maybe<A>>

Result.sequence.Maybe to swap outer Result and inner Maybe contexts.

Maybe.sequence.Result(Maybe.make.some(Result.make.ok(42)));  // Ok(Some(42))
Maybe.sequence.Result(Maybe.make.some(Result.make.err("e"))); // Err("e")
Maybe.sequence.Result(Maybe.make.none());                     // Ok(None)

struct: <T>(fields) => Maybe<{ [K in string | number | symbol]: T[K] extends Maybe<A> ? A : never }>

Combines a record of Maybes into a single Maybe of a record. Evaluates fields in key order and short-circuits on the first None.

T extends Record<string, Maybe<unknown>>

T

Maybe<{ [K in string | number | symbol]: T[K] extends Maybe<A> ? A : never }>

Maybe.assign to combine values sequentially with inter-field dependencies.

Maybe.struct({
  name: Maybe.make.some("Alice"),
  age: Maybe.make.some(30)
}); // Some({ name: "Alice", age: 30 })

tap: <A>(sideEffect) => (maybe) => Maybe<A>

Executes a side effect on the value without changing the Maybe. Useful for logging or debugging.

A

(value) => void

(maybe) => Maybe<A>

Maybe.tapNone for running side effects on None.

pipe(
  Maybe.make.some(5),
  Maybe.tap(n => console.log("Value:", n)),
  Maybe.map(n => n * 2)
);

tapNone: (sideEffect) => <A>(maybe) => Maybe<A>

Executes a side effect when the Maybe is None, without changing the Maybe.

() => void

<A>(maybe) => Maybe<A>

Maybe.tap for running side effects on Some.

pipe(
  Maybe.make.none(),
  Maybe.tapNone(() => console.log("Value missing")),
);

to: object

nullable: <A>(maybe) => A | null

Extracts the value from a Maybe, returning null if None.

A

Maybe<A>

A | null

  • Maybe.to.undefined to extract value returning undefined on None.
  • Maybe.from.nullable to create a Maybe from a nullable value.
Maybe.to.nullable(Maybe.make.some(42)); // 42
Maybe.to.nullable(Maybe.make.none());   // null

Result: <E>(onNone) => <A>(maybe) => Result<E, A>

Converts a Maybe to a Result. Some becomes Ok, None becomes Err with the provided error.

E

() => E

<A>(maybe) => Result<E, A>

  • Maybe.from.Result to convert a Result back into a Maybe.
  • Maybe.to.Validation to convert a Maybe into an error-accumulating Validation.
pipe(
  Maybe.make.some(42),
  Maybe.to.Result(() => "Value was missing")
); // Ok(42)

pipe(
  Maybe.make.none(),
  Maybe.to.Result(() => "Value was missing")
); // Err("Value was missing")

undefined: <A>(maybe) => A | undefined

Extracts the value from a Maybe, returning undefined if None.

A

Maybe<A>

A | undefined

Maybe.to.nullable to extract value returning null on None.

Maybe.to.undefined(Maybe.make.some(42)); // 42
Maybe.to.undefined(Maybe.make.none());   // undefined

Validation: <E>(onNone) => <A>(maybe) => Validation<E, A>

Converts a Maybe to a Validation. Some becomes Passed, None becomes Failed with error produced by onNone.

E

() => E

<A>(maybe) => Validation<E, A>

  • Maybe.to.Result to convert a Maybe into a fail-fast Result.
  • Maybe.from.Validation to convert a Validation back into a Maybe.
pipe(Maybe.make.some(42), Maybe.to.Validation(() => "missing")); // Passed(42)
pipe(Maybe.make.none(), Maybe.to.Validation(() => "missing"));   // Failed(["missing"])

tryCatch: <A>(operation) => Maybe<A>

Wraps a synchronous operation that may throw, returning a Maybe<A>. Returns Some(value) if successful, or None if an exception is thrown.

A

() => A

Maybe<A>

const safeParse = (s: string) => Maybe.tryCatch(() => JSON.parse(s));
safeParse('{"a": 1}'); // Some({ a: 1 })
safeParse('invalid');   // None