The type of the value that the Optional may contain.
Check if the Optional contains some value.
This should be used in an if
or switch
to expose the value property.
example:
const maybeValue = optional.some("Hello");
if (maybeValue.is_some()) {
// maybeValue has been widened to OptionalWithValue<string> and has a `.value` property.
console.log(maybeValue.value); // prints "Hello"
} else {
// Type error: Property 'Value' does not exist on type 'Optional<string>'.
console.log(maybeValue.value);
}
@return true if the Optional contains a value and widens the type to
Get the value of the Optional if contains some value. Otherwise, returns the provided default value.
example:
const maybeValue = optional.none<string>();
console.log(maybeValue.value_or("Default")); // prints "Default"
The value to return if the Optional is empty.
The value of the Optional if it contains a value, otherwise value_if_empty
.
Transforms the value of the Optional using the provided function. If the Optional is empty, the mapping function will not be run.
example:
// with a value
const maybe = optional.some("Hello").map((v) => v.toUpperCase());
if (maybe.is_some()) {
console.log(maybe.value); // prints "HELLO"
}
// without a value
const empty = optional.none<string>().map((v) => v.toUpperCase());
if (!empty.is_some()) {
console.log("No value"); // prints "No value"
}
The function to transform the value, if present. Will not be called if the Optional is empty.
A new Optional containing the result of transforming the value or empty if this Optional is empty.
Chains another Optional operation if this optional contains some value. If this Optional is empty, the provided function will not be called.
example:
const maybe = optional.some("Hello").and_then((v) => optional.some(v.length));
if (maybe.is_some()) {
console.log(maybe.value); // prints 5
}
A function that accepts an argument of the ValueType of this optional and that returns another Optional.
A new Optional containing the value from the provided function, or an empty Optional if this Optional is empty.
Provides a fallback Optional if this Optional is empty. If this Optional contains a value, it will return itself and the fallback function will not be called.
example:
const maybe = optional.none<string>().or_else(() => optional.some("Default"));
if (maybe.is_some()) {
console.log(maybe.value); // prints "Default"
}
The current Optional if it contains a value, otherwise the Optional returned by the provided function.
Returns a generator that yields the contained value if this Optional has a value. If this Optional is empty, the generator yields nothing (completes immediately). This allows for easy iteration over present values in for-of loops and other iterator contexts.
A generator that yields the value if present, otherwise yields nothing
const someValue = optional.some(42);
for (const value of someValue) {
console.log(value); // 42
}
const emptyValue = optional.none<number>();
for (const value of emptyValue) {
console.log("This won't execute");
}
// Useful for collecting present values from multiple optionals
const optionals = [
optional.some(1),
optional.none<number>(),
optional.some(3)
];
const values = [];
for (const opt of optionals) {
for (const value of opt) {
values.push(value);
}
}
console.log(values); // [1, 3]
The public interface of an Optional value. An Optional either contains a value of type
ValueType
or is empty.Optionals should be constructed using
optional.some()
oroptional.none<ValueType>()
. These functions return objects implementing theOptional
interface.The value of an Optional can only be accessed after checking
is_some()
.When working with Optional values, it is preferred to use methods such as
map
,and_then
, andor_else
for sequential operations, and only checkis_some()
at the end. These methods allow consumers to write type-safe code using functional patterns that are more expressive than handling empty states at every step.