The type of the value contained in the Some variant.
Readonly[A unique symbol property used to identify the variant of this Option.
Returns 'Some' if the Option contains a value, or 'None' if it represents absence.
This is used internally by the isOption utility function to verify that an object is a valid Option instance,
and to distinguish between Some and None variants without calling methods.
Note: The symbol itself is not exported as part of the public API.
Use the isOption utility function or the isSome()/isNone() methods for type checking.
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 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 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 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}`);
Transposes an Option of a Result into a Result of an Option.
The type of the success value in the Ok variant of the Result.
The type of the error value in the Err variant of the Result.
Ok containing Some if the Option is a Some containing Ok,
Err containing the error if the Option is a Some containing Err,
Ok containing None if the Option is None.
Returns the contained Some value.
Converts from Option<[U, R]> to [Option<U>, Option<R>].
If this is Some([a, b]), returns [Some(a), Some(b)].
If this is None, returns [None, None].
The type of the first value in the tuple.
The type of the second value in the tuple.
A tuple of Options, one for each element in the original Option of a tuple.
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.
Type
Optionrepresents an optional value: everyOptionis eitherSomeand contains a value, orNone, and does not. This interface includes methods that act on theOptiontype, similar to Rust'sOptionenum.As Rust Code:
Since
1.0.0
See
https://doc.rust-lang.org/std/option/enum.Option.html