Represents the type of the Option: either Some or None. Useful for debugging and runtime checks.
Determines if the Option is None.
true if the Option is None, otherwise false.
console.log(Some(5).isNone()); // false
console.log(None.isNone()); // true
Determines if the Option is a Some.
true if the Option is Some, otherwise false.
console.log(Some(5).isSome()); // true
console.log(None.isSome()); // false
Performs a match operation on the Option, allowing for branching logic based on its state. This method takes an object with functions for each case (Some or None) and executes the corresponding function based on the Option's state, returning the result.
The result of applying the corresponding function based on the Option's state.
const optionSome = Some(5);
const matchResultSome = optionSome.match({
some: (value) => `The value is ${value}.`,
none: () => 'There is no value.',
});
console.log(matchResultSome); // Outputs: "The value is 5."
const optionNone = None;
const matchResultNone = optionNone.match({
some: (value) => `The value is ${value}.`,
none: () => 'There is no value.',
});
console.log(matchResultNone); // Outputs: "There is no value."
Returns the contained value if Some, otherwise returns the provided default value.
The default value to return if the Option is None.
The contained value if Some, otherwise def
.
const someValue = Some("value").unwrapOr("default"); // "value"
const noneValue = None.unwrapOr("default"); // "default"
Implementation of Option representing the absence of a value (None).