Readonlyempty: booleantrue if the monad does not hold a non-nullish value; always the inverse
of ok.
Readonlyerror: NonNullish | undefined | nullThe error associated with an empty monad, or null / undefined when
no error is present (i.e. the monad is ok or was emptied via
maybe.empty).
Readonlyok: booleantrue if the monad holds a non-nullish value; always the inverse of
empty.
Readonlyvalue: Mandatory<TypedValue>The non-nullish value held by an ok monad.
Returns a new monad produced by next when the current monad has an
error. Returns the current monad unchanged when ok or empty without
an error.
Factory errors thrown inside next are caught and produce a new error
monad.
A function that receives the current error and returns a
recovery value or null / undefined to stay empty.
A monad for the recovery value, or the current monad.
Returns next wrapped in a monad when the current monad is empty
and has no error. Returns the current monad unchanged in every other
case (i.e. when ok, or when empty with an error).
next may be a plain value, a zero-argument factory, or undefined.
Factory errors are caught and produce an error monad.
Fallback value, factory, or undefined.
A monad for next or the current monad.
Returns an empty monad when the current monad is ok but predicate
returns false. Returns the current monad unchanged when empty.
Supports type-guard predicates to narrow the result type.
A boolean-returning test, or a type-guard, applied to the current value.
A monad whose value satisfies the predicate, or an empty monad.
Transforms the current value by applying next when the monad is ok.
Returns an empty or error monad unchanged.
If next returns a Maybe, that monad is returned as-is (flat-map
behaviour). If next returns a nullish value, an empty monad is returned.
Errors thrown inside next are caught and produce an error monad.
A mapping function that receives the current value and
returns the next value, Maybe, or null / undefined.
A monad wrapping the mapped value.
maybe(21).map((n) => n * 2).value; // 42
maybe('hello').map((s) => s.length).value; // 5
maybe<number>(null).map((n) => n * 2).ok; // false (empty – no-op)
// Flat-map: returning a Maybe is unwrapped
maybe(42).map((n) => maybe(n > 0 ? n : null)).value; // 42
// Errors thrown in next are caught
maybe(42).map(() => { throw new Error('!'); }).error; // Error: !
Returns a single-element tuple containing the value when ok, or an
empty tuple when empty. Useful for destructuring or spreading into
other arrays.
[value] when ok, otherwise [].
// Basic value wrapping
const m = maybe(42);
m.ok; // true
m.value; // 42
// Chaining
const result = maybe<string>(fetchName())
.map((name) => name.trim())
.filter((name) => name.length > 0)
.else('Anonymous')
.value;
// Error handling
const parsed = maybe(() => JSON.parse(rawInput))
.catch(() => ({}));
A lightweight Maybe monad for representing values that may or may not be present, with optional error context.
A
Maybe<T>is always in one of two states:valueof typeT.error.Reading state
Use the
ok,empty,value, anderrorproperties, ortoArray().Deriving new monads
mapempty).filterempty).elseemptywithout an error.catcherror(no-op without one).Creating monads
Use the maybe factory and its static helpers maybe.empty and maybe.error.