Optional
@ts-rust/std / Option / Optional
Interface: Optional<T>
Defined in: packages/std/src/option/interface.ts:59
Interface defining the core functionality of an Option, inspired by Rust's Option type, with additional methods tailored for TypeScript.
Represents a value that may or may not be present, offering a robust alternative to
null
or undefined
. It includes most Rust Option
methods (e.g., map
, andThen
,
unwrap
) for safe value handling, plus TypeScript-specific extensions like
toPending and async variants of and with Promise
support.
For methods accepting predicates (e.g., orElse, filter, map, andThen), exceptions in the provided function result in None, ensuring predictable, type-safe behavior. If error handling is a concern, use okOr or okOrElse to convert to a Result.
Implementations like Some and None enable pattern matching, transformations, and error handling in a type-safe way.
Type Parameters
Type Parameter |
---|
|
Methods
and()
and<U>(x): Option<U>;
Defined in: packages/std/src/option/interface.ts:74
Returns None if this option is None, otherwise returns x
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
Returns
Option
<U
>
Example
const x = some(2);
const y = none<number>();
expect(x.and(some(3))).toStrictEqual(some(3));
expect(x.and(none())).toStrictEqual(none());
expect(y.and(some(3))).toStrictEqual(none());
expect(y.and(none())).toStrictEqual(none());
andThen()
andThen<U>(f): Option<U>;
Defined in: packages/std/src/option/interface.ts:94
Applies f
to the value if Some, returning its result; otherwise,
returns None. Also known as flatMap
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<U
>
Notes
- Default: If
f
throws, returns None.
Example
const x = some(2);
const y = none<number>();
expect(x.andThen((n) => some(n * 2))).toStrictEqual(some(4));
expect(
x.andThen((_) => {
throw new Error();
}),
).toStrictEqual(none());
expect(x.andThen((_) => none())).toStrictEqual(none());
expect(y.andThen((n) => some(n * 2))).toStrictEqual(none());
clone()
clone<U>(this): Option<U>;
Defined in: packages/std/src/option/interface.ts:112
Returns a clone of the Option.
Only available on Options with Cloneable values.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
Returns
Option
<U
>
Example
const x = some(1);
const y = some({ a: 1, clone: () => ({ a: 1 }) });
expect(x.clone()).toStrictEqual(some(1));
expect(x.clone()).not.toBe(x); // Different reference
expect(x.clone().unwrap()).toBe(1);
expect(y.clone()).toStrictEqual(some({ a: 1 }));
combine()
combine<U>(...opts): Option<[T, ...SomeValues<U>[]]>;
Defined in: packages/std/src/option/interface.ts:133
Combines this Option with other Option
instances into a single
Option
containing a tuple of values.
The combine
method takes an arbitrary number of Option
instances,
all sharing the same error-free structure. If all Option
instances
(including this one) are Some
, it returns an Option
with a tuple of
their values in the order provided. If any Option
is None
, it returns
None
. The resulting tuple includes the value of this Option
as the first
element, followed by the values from the provided Option
instances.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
... |
|
Returns
Option
<[T
, ...SomeValues<U>[]
]>
Example
const a = some(Promise.resolve(1));
const b = some("hi");
const c = none<Date>();
const d = a.combine(b, c); // Option<[Promise<number>, string, Date]>
copy()
copy(): Option<T>;
Defined in: packages/std/src/option/interface.ts:152
Returns a shallow copy of the Option.
Returns
Option
<T
>
Example
const value = { a: 1 };
const x = some(value);
const y = none<{ a: number }>();
expect(x.copy()).toStrictEqual(some({ a: 1 }));
expect(x.copy()).not.toBe(x); // Different option reference
expect(x.copy().unwrap()).toBe(value); // Same value reference
expect(y.copy()).toStrictEqual(none());
expect()
expect(this, msg?): T;
Defined in: packages/std/src/option/interface.ts:171
Returns the value if Some, or throws an OptionError with msg
(or a default message) if None.
Parameters
Parameter | Type |
---|---|
| |
|
|
Returns
T
Throws
- OptionError if this is None
Example
const x = some(42);
const y = none<number>();
expect(x.expect("Missing value")).toBe(42);
expect(() => y.expect("Missing value")).toThrow("Missing value");
expect(() => y.expect()).toThrow("`expect`: called on `None`");
filter()
filter(f): Option<T>;
Defined in: packages/std/src/option/interface.ts:191
Returns the option if Some and f
returns true
, otherwise
returns None.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<T
>
Notes
- Default: If
f
throws, None is returned.
Example
const x = some(2);
const y = none<number>();
expect(x.filter((n) => n > 0)).toStrictEqual(some(2));
expect(x.filter((n) => n < 0)).toStrictEqual(none());
expect(
x.filter((_) => {
throw new Error();
}),
).toStrictEqual(none());
expect(y.filter((n) => n > 0)).toStrictEqual(none());
flatten()
flatten<U>(this): Option<U>;
Defined in: packages/std/src/option/interface.ts:207
Flattens an Option of an Option into a single Option.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
Returns
Option
<U
>
Example
const x: Option<Option<Option<number>>> = some(some(some(6)));
const y: Option<Option<number>> = x.flatten();
const z = none<Option<Option<number>>>();
expect(x.flatten()).toStrictEqual(some(some(6)));
expect(y.flatten()).toStrictEqual(some(6));
expect(z.flatten()).toStrictEqual(none());
getOrInsert()
getOrInsert(this, x): T;
Defined in: packages/std/src/option/interface.ts:229
Returns the contained value if Some, or inserts and returns x
if None.
See also insert method, which updates the value even if the option already contains Some.
Parameters
Parameter | Type |
---|---|
| |
|
|
Returns
T
Notes
- Mutation: This method mutates the Option.
Example
const x = some(2);
const y = none<number>();
expect(x.getOrInsert(5)).toBe(2);
expect(y.getOrInsert(5)).toBe(5);
expect(y).toStrictEqual(some(5)); // y is mutated
getOrInsertWith()
getOrInsertWith(this, f): T;
Defined in: packages/std/src/option/interface.ts:256
Returns the value if Some, or inserts and returns the result of f
if None.
Parameters
Parameter | Type |
---|---|
| |
| () => |
Returns
T
Throws
- OptionError if
f
throws, with the original error as OptionError.reason
Notes
- Mutation: Mutates this option to Some with
f
’s result if None. Iff
throws, the option remains unchanged.
Example
const x = some(2);
const y = none<number>();
const z = none<number>();
expect(x.getOrInsertWith(() => 5)).toBe(2);
expect(y.getOrInsertWith(() => 5)).toBe(5);
expect(y).toStrictEqual(some(5)); // Mutated
expect(() =>
z.getOrInsertWith(() => {
throw new Error();
}),
).toThrow(OptionError);
expect(z).toStrictEqual(none()); // Unchanged
insert()
insert(this, x): T;
Defined in: packages/std/src/option/interface.ts:278
Inserts x
into the option and returns it, overwriting any existing value.
See also getOrInsert method, which doesn’t update the value if the option already contains Some.
Parameters
Parameter | Type |
---|---|
| |
|
|
Returns
T
Notes
- Mutation: This method mutates the Option.
Example
const x = some(2);
const y = none<number>();
expect(x.insert(5)).toBe(5);
expect(x).toStrictEqual(some(5));
expect(y.insert(5)).toBe(5);
expect(y).toStrictEqual(some(5));
inspect()
inspect(f): Option<T>;
Defined in: packages/std/src/option/interface.ts:301
Calls f
with the value if Some, then returns a copy of this option.
If f
throws or returns a Promise
that rejects, the error is ignored.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<T
>
Notes
- Returns a new Option instance, not the original reference.
Example
const x = some(2);
const y = none<number>();
let sideEffect = 0;
expect(x.inspect((n) => (sideEffect = n))).toStrictEqual(some(2));
expect(
x.inspect((_) => {
throw new Error();
}),
).toStrictEqual(some(2));
expect(sideEffect).toBe(2);
expect(y.inspect((n) => (sideEffect = n))).toStrictEqual(none());
expect(sideEffect).toBe(2); // Unchanged
isNone()
isNone(): this is None<T>;
Defined in: packages/std/src/option/interface.ts:315
Returns true
if the option is None.
Returns
this is None<T>
Example
const x = some(2);
const y = none<number>();
expect(x.isNone()).toBe(false);
expect(y.isNone()).toBe(true);
isNoneOr()
isNoneOr(f): boolean;
Defined in: packages/std/src/option/interface.ts:334
Returns true
if the option is None or if f
returns true
for the contained value.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
boolean
Notes
- Default: If
f
throws,false
is returned.
Example
const x = some(2);
const y = none<number>();
expect(x.isNoneOr((n) => n > 0)).toBe(true);
expect(
x.isNoneOr((_) => {
throw new Error();
}),
).toBe(false);
expect(x.isNoneOr((n) => n < 0)).toBe(false);
expect(y.isNoneOr((n) => n > 0)).toBe(true);
isSome()
isSome(): this is Some<T>;
Defined in: packages/std/src/option/interface.ts:348
Returns true
if the option is Some.
Returns
this is Some<T>
Example
const x = some(2);
const y = none<number>();
expect(x.isSome()).toBe(true);
expect(y.isSome()).toBe(false);
isSomeAnd()
isSomeAnd(f): this is Optional<T> & { [phantom]: "some"; value: T } & boolean;
Defined in: packages/std/src/option/interface.ts:368
Returns true
if the option is Some and f
returns true
for the contained value.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
this is Optional<T> & { [phantom]: "some"; value: T } & boolean
Notes
- Default: If
f
throws,false
is returned.
Example
const x = some(2);
const y = none<number>();
expect(x.isSomeAnd((n) => n > 0)).toBe(true);
expect(
x.isSomeAnd((_) => {
throw new Error();
}),
).toBe(false);
expect(x.isSomeAnd((n) => n < 0)).toBe(false);
expect(y.isSomeAnd((n) => n > 0)).toBe(false);
iter()
iter(): IterableIterator<T, T, void>;
Defined in: packages/std/src/option/interface.ts:394
Returns an iterator over this option’s value, yielding it if Some or nothing if None.
Returns
IterableIterator
<T
, T
, void
>
Notes
- Yields exactly one item for Some, or zero items for None.
- Compatible with
for...of
loops and spread operators.
Example
const x = some(42);
const y = none<number>();
const iterX = x.iter();
expect(iterX.next()).toEqual({ value: 42, done: false });
expect(iterX.next()).toEqual({ done: true });
const iterY = y.iter();
expect(iterY.next()).toEqual({ done: true });
expect([...x.iter()]).toEqual([42]);
expect([...y.iter()]).toEqual([]);
map()
map<U>(f): Option<U>;
Defined in: packages/std/src/option/interface.ts:413
Maps the contained value with f
if Some, returning a new
Option; otherwise, returns None.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<U
>
Notes
- Default: If
f
throws, None is returned.
Example
const x = some(2);
const y = none<number>();
expect(x.map((n) => n * 2)).toStrictEqual(some(4));
expect(
x.map((_) => {
throw new Error();
}),
).toStrictEqual(none());
expect(y.map((n) => n * 2)).toStrictEqual(none());
mapAll()
Call Signature
mapAll<U>(f): Option<U>;
Defined in: packages/std/src/option/interface.ts:437
Maps this option by applying a callback to its full state, executing the callback for both Some and None, returning a new Option.
Unlike andThen, which only invokes the callback for Some,
this method always calls f
, passing the entire Option as its argument.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<U
>
Notes
- Default: If
f
throws, the error is silently ignored and None is returned.
Example
const someOpt = some(42);
const noneOpt = none<number>();
const undefOpt = some(undefined);
expect(someOpt.mapAll((opt) => some(opt.unwrapOr(0) + 1))).toStrictEqual(
some(43),
);
expect(noneOpt.mapAll((opt) => some(opt.unwrapOr(0) + 1))).toStrictEqual(
some(1),
);
expect(
undefOpt.mapAll((opt) => some(opt.isSome() ? "some" : "none")),
).toStrictEqual(some("some"));
Call Signature
mapAll<U>(f): PendingOption<Awaited<U>>;
Defined in: packages/std/src/option/interface.ts:463
Maps this option by applying a callback to its full state, executing the callback for both Some and None, returning a PendingOption.
Unlike andThen, which only invokes the callback for Some,
this method always calls f
, passing the entire Option as its argument.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
PendingOption
<Awaited
<U
>>
Notes
- Default: If
f
returns aPromise
that rejects, the resulting PendingOption resolves to None.
Example
const someOpt = some(42);
const noneOpt = none<number>();
const mappedSome = someOpt.mapAll((opt) =>
Promise.resolve(some(opt.unwrapOr(0))),
);
expect(isPendingOption(mappedSome)).toBe(true);
expect(await mappedSome).toStrictEqual(some(42));
const mappedNone = noneOpt.mapAll((opt) =>
Promise.resolve(some(opt.unwrapOr(0) + 1)),
);
expect(isPendingOption(mappedNone)).toBe(true);
expect(await mappedNone).toStrictEqual(some(1));
mapOr()
mapOr<U>(
this,
def,
f): U;
Defined in: packages/std/src/option/interface.ts:481
Returns f
applied to the value if Some, otherwise returns def
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| |
|
|
| ( |
Returns
U
Notes
- Default: If
f
throws, returnsdef
.
Example
const x = some(2);
const y = none<number>();
expect(x.mapOr(0, (n) => n * 2)).toBe(4);
expect(
x.mapOr(0, (_) => {
throw new Error();
}),
).toBe(0);
expect(y.mapOr(0, (n) => n * 2)).toBe(0);
mapOrElse()
mapOrElse<U>(
this,
mkDef,
f): U;
Defined in: packages/std/src/option/interface.ts:506
Returns f
applied to the contained value if Some, otherwise
returns the result of mkDef
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| |
| () => |
| ( |
Returns
U
Throws
- OptionError if
mkDef
is called and throws an exception. Original error will be set as OptionError.reason.
Notes
- Default: If
f
throws, the error is silently ignored and result ofmkDef
is returned.
Example
const x = some(2);
const y = none<number>();
expect(
x.mapOrElse(
() => 0,
(n) => n * 2,
),
).toBe(4);
expect(
x.mapOrElse(
() => 1,
(_) => {
throw new Error();
},
),
).toBe(1);
expect(() =>
x.mapOrElse(
() => {
throw new Error();
},
(_) => {
throw new Error();
},
),
).toThrow(OptionError);
expect(
y.mapOrElse(
() => 0,
(n) => n * 2,
),
).toBe(0);
match()
match<U, F>(
this,
f,
g): U | F;
Defined in: packages/std/src/option/interface.ts:535
Matches the option, returning f
applied to the value if Some,
or g
if None.
Type Parameters
Type Parameter | Default type |
---|---|
| ‐ |
|
|
Parameters
Parameter | Type |
---|---|
| |
| ( |
| () => |
Returns
U
| F
Throws
- OptionError if
f
org
throws an exception, original error will be set as OptionError.reason.
Notes
- If
f
org
returns aPromise
that rejects, the caller is responsible for handling the rejection.
Example
const x = some(2);
const y = none<number>();
expect(
x.match(
(n) => n * 2,
() => 0,
),
).toBe(4);
expect(() =>
x.match(
(_) => {
throw new Error();
},
() => 0,
),
).toThrow(OptionError);
expect(
y.match(
(n) => n * 2,
() => 0,
),
).toBe(0);
expect(() =>
y.match(
(n) => n * 2,
() => {
throw new Error();
},
),
).toThrow(OptionError);
okOr()
okOr<E>(y): Result<T, E>;
Defined in: packages/std/src/option/interface.ts:556
Converts to a Result, using y
as the error value if None.
Some(v) is mapped to Ok(v) and None to Err(y).
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
Returns
Result
<T
, E
>
Example
const x = some(2);
const y = none<number>();
expect(x.okOr("error")).toStrictEqual(ok(2));
expect(y.okOr("error")).toStrictEqual(err("error"));
okOrElse()
okOrElse<E>(mkErr): Result<T, E>;
Defined in: packages/std/src/option/interface.ts:574
Converts to a Result, using the result of mkErr
as the error
value if None.
Some(v) is mapped to Ok(v) and None to Err(mkErr()).
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| () => |
Returns
Result
<T
, E
>
Example
const x = some(2);
const y = none<number>();
expect(x.okOrElse(() => "error")).toStrictEqual(ok(2));
expect(y.okOrElse(() => "error")).toStrictEqual(err("error"));
or()
or(x): Option<T>;
Defined in: packages/std/src/option/interface.ts:590
Returns the current option if it is Some, otherwise returns x
.
Parameters
Parameter | Type |
---|---|
|
|
Returns
Option
<T
>
Example
const x = some(2);
const y = none<number>();
expect(x.or(some(3))).toStrictEqual(some(2));
expect(x.or(none())).toStrictEqual(some(2));
expect(y.or(some(3))).toStrictEqual(some(3));
expect(y.or(none())).toStrictEqual(none());
orElse()
orElse(f): Option<T>;
Defined in: packages/std/src/option/interface.ts:609
Returns the current option if Some, otherwise returns the result of f
.
Parameters
Parameter | Type |
---|---|
| () => |
Returns
Option
<T
>
Notes
- Default: If
f
throws, None is returned.
Example
const x = some(2);
const y = none<number>();
expect(x.orElse(() => some(3))).toStrictEqual(some(2));
expect(y.orElse(() => some(3))).toStrictEqual(some(3));
expect(
y.orElse(() => {
throw new Error();
}),
).toStrictEqual(none());
expect(y.orElse(() => none())).toStrictEqual(none());
replace()
replace(x): Option<T>;
Defined in: packages/std/src/option/interface.ts:628
Replaces the current value with x
and returns the old Option.
Parameters
Parameter | Type |
---|---|
|
|
Returns
Option
<T
>
Notes
- Mutation: This method mutates the Option.
Example
const x = some(2);
const y = none<number>();
expect(x.replace(5)).toStrictEqual(some(2));
expect(x).toStrictEqual(some(5));
expect(y.replace(5)).toStrictEqual(none());
expect(y).toStrictEqual(some(5)); // y is mutated
take()
take(): Option<T>;
Defined in: packages/std/src/option/interface.ts:647
Takes the value out of the Option, leaving None in its place.
Returns
Option
<T
>
Notes
- Mutation: This method mutates the Option.
Example
const x = some(2);
const y = none<number>();
expect(x.take()).toStrictEqual(some(2));
expect(x).toStrictEqual(none());
expect(y.take()).toStrictEqual(none());
expect(y).toStrictEqual(none());
takeIf()
takeIf(f): Option<T>;
Defined in: packages/std/src/option/interface.ts:672
Takes the value out of the Option, but only if f
returns true
.
Similar to take, but conditional.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<T
>
Notes
- Mutation: This method mutates the Option.
- Default: If
f
throws, None is returned and the original value remains unchanged.
Example
const x = some(2);
const y = none<number>();
const z = some(1);
expect(x.takeIf((n) => n > 0)).toStrictEqual(some(2));
expect(x).toStrictEqual(none());
expect(x.takeIf((n) => n < 0)).toStrictEqual(none());
expect(y.takeIf((n) => n > 0)).toStrictEqual(none());
expect(
z.takeIf((_) => {
throw new Error();
}),
).toStrictEqual(none());
expect(z).toStrictEqual(some(1));
tap()
tap(f): Option<T>;
Defined in: packages/std/src/option/interface.ts:695
Executes f
with a copy of this option, then returns a new copy unchanged.
Useful for side-effects like logging, works with both Some and None.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Option
<T
>
Notes
- If
f
throws or rejects, the error is ignored. - If
f
returns a promise, the promise is not awaited before returning.
Example
const x = some(42);
const y = none<number>();
let log = "";
expect(x.tap((opt) => (log = opt.toString()))).toStrictEqual(some(42));
expect(log).toBe("Some { 42 }");
expect(y.tap((opt) => (log = opt.toString()))).toStrictEqual(none());
expect(log).toBe("None");
toPending()
toPending(): PendingOption<Awaited<T>>;
Defined in: packages/std/src/option/interface.ts:722
Maps this option to a PendingOption by supplying a shallow copy of this option to PendingOption factory.
Useful for transposing an option with PromiseLike
value to a
PendingOption with Awaited
value.
Returns
PendingOption
<Awaited
<T
>>
Notes
- Default: If inner
T
is a promise-like that rejects, maps to a PendingOption with None.
Example
const value = { a: 1 };
const x = some(value);
const y = none<number>();
const pendingX = x.toPending();
expect(isPendingOption(pendingX)).toBe(true);
expect(await pendingX).toStrictEqual(some({ a: 1 }));
value.a = 2;
expect(await pendingX).toStrictEqual(some({ a: 2 }));
expect(await y.toPending()).toStrictEqual(none());
toPendingCloned()
toPendingCloned(this): PendingOption<Awaited<T>>;
Defined in: packages/std/src/option/interface.ts:749
Maps this option to a PendingOption by supplying a clone of this option to PendingOption factory.
Useful for transposing an option with PromiseLike
value to a
PendingOption with Awaited
value.
Parameters
Parameter | Type |
---|---|
|
Returns
PendingOption
<Awaited
<T
>>
Notes
- Default: If inner
T
is a promise-like that rejects, maps to a PendingOption with None.
Example
const value = { a: 0, clone: () => ({ a: 0 }) };
const x = some(value);
const y = none<number>();
const pendingX = x.toPendingCloned();
expect(isPendingOption(pendingX)).toBe(true);
expect((await pendingX).unwrap().a).toBe(0);
value.a = 42;
expect((await pendingX).unwrap().a).toBe(0);
expect(await y.toPendingCloned()).toStrictEqual(none());
toString()
toString(): string;
Defined in: packages/std/src/option/interface.ts:763
Returns a string representation of the Option.
Returns
string
Example
const x = some(2);
const y = none<number>();
expect(x.toString()).toBe("Some { 2 }");
expect(y.toString()).toBe("None");
transpose()
transpose<U, E>(this): Result<Option<U>, E>;
Defined in: packages/std/src/option/interface.ts:783
Transposes an Option of a Result into a Result of an Option.
Maps None
to Ok(None)
, Some(Ok(_))
to Ok(Some(_))
,
and Some(Err(_))
to Err(_)
.
Type Parameters
Type Parameter |
---|
|
|
Parameters
Parameter | Type |
---|---|
|
Returns
Example
const x = none<Result<number, string>>();
const y = some<Result<number, string>>(ok(2));
const z = some<Result<number, string>>(err("error"));
expect(x.transpose()).toStrictEqual(ok(none()));
expect(y.transpose()).toStrictEqual(ok(some(2)));
expect(z.transpose()).toStrictEqual(err("error"));
unwrap()
unwrap(this): T;
Defined in: packages/std/src/option/interface.ts:800
Returns the value if Some, or throws an OptionError if None.
Parameters
Parameter | Type |
---|---|
|
Returns
T
Throws
- OptionError if this is None
Example
const x = some(2);
const y = none<number>();
expect(x.unwrap()).toBe(2);
expect(() => y.unwrap()).toThrow("`unwrap`: called on `None`");
unwrapOr()
unwrapOr(this, def): T;
Defined in: packages/std/src/option/interface.ts:814
Returns the contained value if Some, or def
if None.
Parameters
Parameter | Type |
---|---|
| |
|
|
Returns
T
Example
const x = some(2);
const y = none<number>();
expect(x.unwrapOr(0)).toBe(2);
expect(y.unwrapOr(0)).toBe(0);
unwrapOrElse()
unwrapOrElse(this, mkDef): T;
Defined in: packages/std/src/option/interface.ts:833
Returns the contained value if Some, or the result of mkDef
if None.
Parameters
Parameter | Type |
---|---|
| |
| () => |
Returns
T
Throws
- OptionError if
mkDef
throws, original error will be set as OptionError.reason.
Example
const x = some(2);
const y = none<number>();
expect(x.unwrapOrElse(() => 0)).toBe(2);
expect(y.unwrapOrElse(() => 0)).toBe(0);
expect(() =>
y.unwrapOrElse(() => {
throw new Error();
}),
).toThrow(OptionError);
xor()
Call Signature
xor(y): Option<T>;
Defined in: packages/std/src/option/interface.ts:849
Returns Some if exactly one of this
or y
is Some, otherwise returns None.
Parameters
Parameter | Type |
---|---|
|
|
Returns
Option
<T
>
Example
const x = some(2);
const y = none<number>();
expect(x.xor(some(3))).toStrictEqual(none());
expect(x.xor(none())).toStrictEqual(some(2));
expect(y.xor(some(3))).toStrictEqual(some(3));
expect(y.xor(none())).toStrictEqual(none());
Call Signature
xor(y): PendingOption<Awaited<T>>;
Defined in: packages/std/src/option/interface.ts:865
Returns a PendingOption with Some if exactly one of this
or y
is
Some, otherwise with None.
Parameters
Parameter | Type |
---|---|
|
|
Returns
PendingOption
<Awaited
<T
>>
Example
const x = some(2);
const y = none<number>();
expect(isPendingOption(x.xor(Promise.resolve(some(3))))).toBe(true);
expect(await x.xor(Promise.resolve(some(3)))).toStrictEqual(none());
expect(await x.xor(Promise.resolve(none()))).toStrictEqual(some(2));
expect(await y.xor(Promise.resolve(some(3)))).toStrictEqual(some(3));