Readonly[When using None alone, the following overrides can make type inference more accurate.
Readonly[toThe well-known symbol Symbol.toStringTag used by Object.prototype.toString().
Returns 'Option' so that Object.prototype.toString.call(option) produces '[object Option]'.
This enables reliable type identification even across different execution contexts (e.g., iframes, different module instances).
Implements the Iterator protocol, allowing Option to be used with for...of loops and spread syntax.
Some(value), yields the contained value once.None, yields nothing (empty iterator).This is similar to Rust's Option::iter() method.
An iterator that yields zero or one value.
// Using with for...of
for (const value of Some(5)) {
console.log(value); // 5
}
for (const value of None) {
console.log(value); // never executed
}
// Using with spread operator
const arr1 = [...Some(5)]; // [5]
const arr2 = [...None]; // []
// Using with Array.from
Array.from(Some('hello')); // ['hello']
Array.from(None); // []
Returns None if the Option is None, otherwise returns other.
This is sometimes called "and then" because it is similar to a logical AND operation.
The type of the value in the other Option.
None if this is None, otherwise returns other.
Returns None if the Option is None, otherwise calls fn with the wrapped value and returns the result.
This function can be used for control flow based on Option values.
The type of the value returned by the function.
The result of fn if this is Some, otherwise None.
Asynchronous version of andThen.
The type of the value returned by the function.
A function that takes the contained value and returns PromiseLike<Option<U>> or Option<U>.
A promise that resolves to None if this is None, otherwise the result of fn.
Tests whether this and other are both Some containing equal values, or both are None.
This method can be used for comparing Option instances in a value-sensitive manner.
true if this and other are both Some with equal values, or both are None, otherwise false.
Returns the contained Some value, with a provided error message if the value is a None.
The error message to provide if the value is a None.
Returns None if the Option is None, otherwise calls predicate with the wrapped value and returns:
Some(t) if predicate returns true (where t is the wrapped value), andNone if predicate returns false.A function that takes the contained value and returns a boolean.
Calls the provided function with the contained value if this is Some.
This is primarily for side effects and does not transform the Option.
A function to call with the contained value.
this, unmodified, for chaining additional methods.
Returns true if the Option is None, or the predicate returns true for the contained value.
A function that takes the contained value and returns a boolean.
Asynchronous version of isNoneOr.
A function that takes the contained value and returns a PromiseLike<boolean> or boolean.
A promise that resolves to true if the Option is None or the predicate resolves to true.
Returns true if the Option is a Some value and the predicate returns true for the contained value.
A function that takes the contained value and returns a boolean.
Asynchronous version of isSomeAnd.
A function that takes the contained value and returns a PromiseLike<boolean> or boolean.
A promise that resolves to true if the Option is Some and the predicate resolves to true.
Maps an Option<T> to Option<U> by applying a function to a contained value.
The type of the value returned by the map function.
A function that takes the contained value and returns a new value.
Maps an Option<T> to U by applying a function to the contained value (if any), or returns the provided default (if not).
The type of the value returned by the map function or the default value.
Maps an Option<T> to U by applying a function to a contained value (if any), or computes a default (if not).
The type of the value returned by the map function or the default function.
Asynchronous version of orElse.
A function that produces PromiseLike<Option<T>> or Option<T>.
A promise that resolves to this if it is Some, otherwise the result of fn.
Reduces two Option values into one using the provided function.
this and other are Some, applies fn to both values and returns Some(result).this is Some, returns Some(this_value).other is Some, returns Some(other_value).None, returns None.This is similar to Rust's Option::reduce with Into<R> constraints.
In TypeScript, when only one Option is Some, that value is returned as-is
(relying on TypeScript's structural typing for compatibility with R).
The type of the value in the other Option.
The result type (defaults to T | U).
The reduced Option.
const a = Some(10);
const b = Some(20);
console.log(a.reduce(b, (x, y) => x + y).unwrap()); // 30
const c = None as Option<number>;
console.log(a.reduce(c, (x, y) => x + y).unwrap()); // 10
console.log(c.reduce(b, (x, y) => x + y).unwrap()); // 20
console.log(c.reduce(c, (x, y) => x + y).isNone()); // true
// With different types that share a common supertype
const str = Some('hello');
const num = Some(42);
const result: Option<string> = str.reduce(num, (s, n) => `${s}-${n}`);
Combines this with another Option by zipping their contained values.
If this is Some(s) and other is Some(o), returns Some([s, o]).
If either this or other is None, returns None.
The type of the value in the other Option.
An Option containing a tuple of the values if both are Some, otherwise None.
Zips this with another Option using a provided function to combine their contained values.
If this is Some(s) and other is Some(o), returns Some(fn(s, o)).
If either this or other is None, returns None.
The type of the value in the other Option.
The return type of the combining function.
An Option containing the result of fn if both Options are Some, otherwise None.
Represents the absence of a value, as a specialized
Optiontype. The type parameter is set toneverbecauseNonedoes not hold a value.