Represents the type of the Option: either Some or None. Useful for debugging and runtime checks.
Returns the option provided as a parameter if the original Option is Some, otherwise returns None.
optb
if the original Option is Some, otherwise None.
const anotherOption = Some("another");
const someOption = Some("some").and(anotherOption); // Some("another")
const noneOption = None.and(anotherOption); // None
Transforms the Option into another by applying a function to the contained value, chaining multiple potentially failing operations.
The Option returned by the function if the original Option is Some, otherwise None.
const parse = (s: string) => {
const parsed = parseInt(s);
return isNaN(parsed) ? None : Some(parsed);
};
const result = Some("123").andThen(parse); // Some(123)
const noResult = Some("abc").andThen(parse); // None
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
Applies a function to the contained value (if any), or returns a default if None.
An Option containing the function's return value if the original Option is Some, otherwise None.
const length = Some("hello").map(s => s.length); // Some(5)
const noneLength = None.map(s => s.length); // None
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 this Option if it is Some, otherwise returns the option provided as a parameter.
The alternative Option to return if the original Option is None.
The original Option if it is Some, otherwise optb
.
const defaultOption = Some("default");
const someOption = Some("some").or(defaultOption); // Some("some")
const noneOption = None.or(defaultOption); // Some("default")
Returns this Option if it is Some, otherwise returns the option provided as a parameter.
The alternative Option to return if the original Option is None.
The original Option if it is Some, otherwise optb
.
const defaultOption = Some("default");
const someOption = Some("some").or(defaultOption); // Some("some")
const noneOption = None.or(defaultOption); // Some("default")
Returns this Option if it is Some, otherwise returns the option provided as a parameter.
The original Option if it is Some, otherwise optb
.
const defaultOption = Some("default");
const someOption = Some("some").or(defaultOption); // Some("some")
const noneOption = None.or(defaultOption); // Some("default")
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).