Skip to main content

Optional

@ts-rust/std


@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

T

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

U

Parameters​

ParameterType

x

Option<U>

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

U

Parameters​

ParameterType

f

(x) => Option<U>

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

U

Parameters​

ParameterType

this

Option<Cloneable<U>>

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

U extends Option<unknown>[]

Parameters​

ParameterType

...opts

U

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]>

contains()​

contains(value): boolean;

Defined in: packages/std/src/option/interface.ts:151

Returns true if the option is Some and the contained value equals value using the === operator.

Parameters​

ParameterType

value

T

Returns​

boolean

Example​

const x = some(2);
const y = none<number>();

expect(x.contains(2)).toBe(true);
expect(x.contains(3)).toBe(false);
expect(y.contains(2)).toBe(false);

copy()​

copy(): Option<T>;

Defined in: packages/std/src/option/interface.ts:168

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:187

Returns the value if Some, or throws an OptionError with msg (or a default message) if None.

Parameters​

ParameterType

this

SettledOption<T>

msg?

string

Returns​

T

Throws​

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:207

Returns the option if Some and f returns true, otherwise returns None.

Parameters​

ParameterType

f

(x) => boolean

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:223

Flattens an Option of an Option into a single Option.

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

this

Option<Option<U>>

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:245

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​

ParameterType

this

SettledOption<T>

x

T

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:272

Returns the value if Some, or inserts and returns the result of f if None.

Parameters​

ParameterType

this

SettledOption<T>

f

() => T

Returns​

T

Throws​

Notes​

  • Mutation: Mutates this option to Some with f’s result if None. If f 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:294

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​

ParameterType

this

SettledOption<T>

x

T

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:317

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​

ParameterType

f

(x) => unknown

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:331

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:350

Returns true if the option is None or if f returns true for the contained value.

Parameters​

ParameterType

f

(x) => boolean

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:364

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): boolean;

Defined in: packages/std/src/option/interface.ts:384

Returns true if the option is Some and f returns true for the contained value.

Parameters​

ParameterType

f

(x) => boolean

Returns​

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);

map()​

map<U>(f): Option<U>;

Defined in: packages/std/src/option/interface.ts:403

Maps the contained value with f if Some, returning a new Option; otherwise, returns None.

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

f

(x) => Awaited<U>

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:427

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

U

Parameters​
ParameterType

f

(x) => Option<U>

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:453

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

U

Parameters​
ParameterType

f

(x) => Promise<Option<U>>

Returns​

PendingOption<Awaited<U>>

Notes​
  • Default: If f returns a Promise 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:471

Returns f applied to the value if Some, otherwise returns def.

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

this

SettledOption<T>

def

Awaited<U>

f

(x) => Awaited<U>

Returns​

U

Notes​

  • Default: If f throws, returns def.

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:496

Returns f applied to the contained value if Some, otherwise returns the result of mkDef.

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

this

SettledOption<T>

mkDef

() => Awaited<U>

f

(x) => Awaited<U>

Returns​

U

Throws​

Notes​

  • Default: If f throws, the error is silently ignored and result of mkDef 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): InferType<U, F>;

Defined in: packages/std/src/option/interface.ts:525

Matches the option, returning f applied to the value if Some, or g if None.

Type Parameters​

Type ParameterDefault type

U

‐

F

U

Parameters​

ParameterType

this

SettledOption<T>

f

(x) => Awaited<U>

g

() => Awaited<F>

Returns​

InferType<U, F>

Throws​

Notes​

  • If f or g returns a Promise 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:546

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

E

Parameters​

ParameterType

y

Awaited<E>

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:564

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

E

Parameters​

ParameterType

mkErr

() => Awaited<E>

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:581

Returns the current option if it is Some, otherwise returns a copy of x.

Parameters​

ParameterType

x

Option<T>

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:600

Returns the current option if Some, otherwise returns the result of f.

Parameters​

ParameterType

f

() => Option<T>

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:619

Replaces the current value with x and returns the old Option.

Parameters​

ParameterType

x

T

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:638

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:663

Takes the value out of the Option, but only if f returns true. Similar to take, but conditional.

Parameters​

ParameterType

f

(x) => boolean

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:686

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​

ParameterType

f

(x) => unknown

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:713

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:740

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​

ParameterType

this

Option<Cloneable<T>>

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:754

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:774

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

U

E

Parameters​

ParameterType

this

Option<Result<U, E>>

Returns​

Result<Option<U>, E>

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:791

Returns the value if Some, or throws an OptionError if None.

Parameters​

ParameterType

this

SettledOption<T>

Returns​

T

Throws​

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:805

Returns the contained value if Some, or def if None.

Parameters​

ParameterType

this

SettledOption<T>

def

Awaited<T>

Returns​

T

Example​

const x = some(2);
const y = none<number>();

expect(x.unwrapOr(0)).toBe(2);
expect(y.unwrapOr(0)).toBe(0);

unwrapOrDefault()​

unwrapOrDefault(): T | undefined;

Defined in: packages/std/src/option/interface.ts:819

Returns the contained value if Some, or undefined if None.

Returns​

T | undefined

Example​

const x = some(2);
const y = none<number>();

expect(x.unwrapOrDefault()).toBe(2);
expect(y.unwrapOrDefault()).toBeUndefined();

unwrapOrElse()​

unwrapOrElse(this, mkDef): T;

Defined in: packages/std/src/option/interface.ts:838

Returns the contained value if Some, or the result of mkDef if None.

Parameters​

ParameterType

this

SettledOption<T>

mkDef

() => Awaited<T>

Returns​

T

Throws​

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:854

Returns Some if exactly one of this or y is Some, otherwise returns None.

Parameters​
ParameterType

y

Option<T>

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:870

Returns a PendingOption with Some if exactly one of this or y is Some, otherwise with None.

Parameters​
ParameterType

y

Promise<Option<T>>

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));