Private
constructorPrivate
isPrivate
valuePrivate
setExtracts the value from the Maybe if it exists. Throws an error if the Maybe contains Nothing.
The contained value
const maybe = Maybe.just(42);
const value = maybe.unwrap(); // Returns 42
const nothing = Maybe.nothing<number>();
nothing.unwrap(); // Throws Error: "Called unwraped on a Nothing value"
If the Maybe contains Nothing
Returns the contained value or a provided default value if the Maybe is Nothing.
The default value to return if Maybe is Nothing
The contained value or the default value
const maybe = Maybe.just(42);
const value1 = maybe.unwrapOr(0); // Returns 42
const nothing = Maybe.nothing<number>();
const value2 = nothing.unwrapOr(0); // Returns 0
Static
justCreates a new Maybe containing the provided value (Just).
The value to wrap in a Maybe
A new Maybe instance containing the value
const just = Maybe.just("hello");
just.isJust(); // Returns true
just.unwrap(); // Returns "hello"
Static
nothing
The Maybe class represents an optional value: every Maybe is either Just and contains a value, or Nothing, and contains no value. This is a safer alternative to using null or undefined.
Example