Skip to main content

Class: Option<T, Exists>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:8

Type Parameters

Type ParameterDefault type
T-
Exists extends booleanboolean

Constructors

new Option()

private new Option<T, Exists>(value: If<Exists, T, null>, exists: Exists): Option<T, Exists>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:18

Parameters

ParameterType
valueIf<Exists, T, null>
existsExists

Returns

Option<T, Exists>

Properties

__STATUS__

protected __STATUS__: Exists

Defined in: projects/utilities/packages/result/src/lib/Option.ts:13

Internal

Branded value to ensure Success is typed correctly.


[ExistsProperty]

private readonly [ExistsProperty]: Exists

Defined in: projects/utilities/packages/result/src/lib/Option.ts:16


[ValueProperty]

private readonly [ValueProperty]: If<Exists, T, null>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:15


none

readonly static none: Option<any, false>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:901

Accessors

[toStringTag]

Get Signature

get [toStringTag](): If<Exists, "Some", "None">

Defined in: projects/utilities/packages/result/src/lib/Option.ts:897

Returns

If<Exists, "Some", "None">

Methods

[iterator]()

[iterator](): Generator<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:893

Returns an iterator over the possibly contained value.

The iterator yields one value if the result is Some, otherwise none.

Returns

Generator<T>

Examples

const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log

See


and()

and<OutputOption>(option: OutputOption): If<Exists, OutputOption, None<any>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:484

Returns None if the option is None, otherwise returns option.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
optionOutputOptionThe option.

Returns

If<Exists, OutputOption, None<any>>

Examples

const x: Option<number> = some(2);
const y: Option<string> = none;
assert.equal(x.and(y), none);
const x: Option<number> = none;
const y: Option<string> = some('foo');
assert.equal(x.and(y), none);
const x: Option<number> = some(2);
const y: Option<string> = some('foo');
assert.equal(x.and(y), some('foo'));
const x: Option<number> = none;
const y: Option<string> = none;
assert.equal(x.and(y), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.and


andThen()

andThen<OutputOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None<any>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:507

Calls cb if the result is Ok, otherwise returns the Err value of self.

This function can be used for control flow based on Result values.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
cb(value: T) => OutputOptionThe predicate.

Returns

If<Exists, OutputOption, None<any>>

Example

function fractionOf4(value: number) {
return value === 0 ? none : some(4 / value);
}

assert.equal(some(2).andThen(fractionOf4), some(4));
assert.equal(some(0).andThen(fractionOf4), none);
assert.equal(none.andThen(fractionOf4), none);

See

https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then


contains()

contains<Value>(value: If<Exists, Value, unknown>): this is Some<Value>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:658

Returns true if the option is a Some value containing the given value.

Type Parameters

Type Parameter
Value

Parameters

ParameterTypeDescription
valueIf<Exists, Value, unknown>The value to compare.

Returns

this is Some<Value>

Examples

const x: Option<number> = some(2);
assert.equal(x.contains(2), true);
const x: Option<number> = some(3);
assert.equal(x.contains(2), false);
const x: Option<number> = none;
assert.equal(x.contains(2), false);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.contains


eq()

eq<OtherValue, OtherExists>(other: Option<OtherValue, OtherExists>): this is Option<OtherValue, OtherExists>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:824

Checks whether or not other equals with self.

Type Parameters

Type Parameter
OtherValue
OtherExists extends boolean

Parameters

ParameterTypeDescription
otherOption<OtherValue, OtherExists>The other option to compare.

Returns

this is Option<OtherValue, OtherExists>

See

https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq


expect()

expect(message: string): If<Exists, T, never>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:139

Returns the contained Some value.

Parameters

ParameterTypeDescription
messagestringThe message for the error. If the value is an Err, it throws an OptionError with the given message.

Returns

If<Exists, T, never>

Examples

const x: Option<string> = some(2);
assert.equal(x.expect('Whoops!'), 2);
const x: Option<string> = none;
assert.throws(() => x.expect('Whoops!'), {
name: 'OptionError',
message: 'Whoops'
});

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.expect


filter()

Call Signature

filter<R>(predicate: (value: T) => value is R): Option<R>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:630

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), and
  • None if predicate returns false.
Type Parameters
Type Parameter
R
Parameters
ParameterTypeDescription
predicate(value: T) => value is RThe predicate.
Returns

Option<R>

Example
function isEven(value: number) {
return n % 2 === 0;
}

assert.equal(none.filter(isEven), none);
assert.equal(some(3).filter(isEven), none);
assert.equal(some(4).filter(isEven), some(4));
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.filter

Call Signature

filter(predicate: (value: T) => boolean): Option<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:631

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), and
  • None if predicate returns false.
Parameters
ParameterTypeDescription
predicate(value: T) => booleanThe predicate.
Returns

Option<T>

Example
function isEven(value: number) {
return n % 2 === 0;
}

assert.equal(none.filter(isEven), none);
assert.equal(some(3).filter(isEven), none);
assert.equal(some(4).filter(isEven), some(4));
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.filter


flatten()

flatten<InnerOption, Exists>(this: Option<InnerOption, Exists>): If<Exists, InnerOption, None<any>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:795

Converts from Result<Result<T, E>, E> to Result<T, E>.

Type Parameters

Type Parameter
InnerOption extends Any
Exists extends boolean

Parameters

ParameterType
thisOption<InnerOption, Exists>

Returns

If<Exists, InnerOption, None<any>>

Examples

const x: Option<Option<number>> = some(some(6));
assert.equal(x.flatten(), some(6));
const x: Option<Option<number>> = some(none);
assert.equal(x.flatten(), none);
const x: Option<Option<number>> = none;
assert.equal(x.flatten(), none);

See

https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten


inspect()

inspect(cb: (value: T) => void): this

Defined in: projects/utilities/packages/result/src/lib/Option.ts:351

Calls the provided closure with a reference to the contained value (if Some).

Parameters

ParameterTypeDescription
cb(value: T) => voidThe predicate.

Returns

this

Seealso

inspectAsync for the awaitable version.

Examples

some(2).inspect(console.log);
// Logs: 2
none.inspect(console.log);
// Doesn't log

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect


inspectAsync()

inspectAsync(cb: (value: T) => unknown): Promise<Option<T, Exists>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:374

Calls the provided closure with a reference to the contained value (if Some).

Parameters

ParameterTypeDescription
cb(value: T) => unknownThe predicate.

Returns

Promise<Option<T, Exists>>

Seealso

inspect for the sync version.

Examples

await some(2).inspectAsync(console.log);
// Logs: 2
await none.inspectAsync(console.log);
// Doesn't log

Note

This is an extension not supported in Rust


intoPromise()

intoPromise(): Promise<Option<Awaited<T>, Exists>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:810

Returns a Promise object with the awaited value (if Some).

Returns

Promise<Option<Awaited<T>, Exists>>

Example

let x = some(Promise.resolve(3));
assert.equal(await x.intoPromise(), some(3));

Note

This is an extension not supported in Rust


isNone()

isNone(): this is None<any>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:87

Returns true if the option is a None value.

Returns

this is None<any>

Examples

const x: Option<number> = some(2);
assert.equal(x.isNone(), false);
const x: Option<number> = none;
assert.equal(x.isNone(), true);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none


isNoneOr()

Call Signature

isNoneOr<R>(cb: (value: T) => value is R): this is None<any> | Some<R>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:112

Returns true if the option is a None value or the value inside of it matches a predicate.

Type Parameters
Type Parameter
R
Parameters
ParameterType
cb(value: T) => value is R
Returns

this is None<any> | Some<R>

Examples
const x: Option<number> = some(2);
assert.equal(x.isNoneOr((x) => x > 1), true);
const x: Option<number> = some(0);
assert.equal(x.isNoneOr((x) => x > 1), false);
const x: Option<number> = none;
assert.equal(x.isNoneOr((x) => x > 1), true);
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none_or

Call Signature

isNoneOr<R>(cb: (value: T) => R): If<Exists, R, true>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:113

Returns true if the option is a None value or the value inside of it matches a predicate.

Type Parameters
Type Parameter
R extends boolean
Parameters
ParameterType
cb(value: T) => R
Returns

If<Exists, R, true>

Examples
const x: Option<number> = some(2);
assert.equal(x.isNoneOr((x) => x > 1), true);
const x: Option<number> = some(0);
assert.equal(x.isNoneOr((x) => x > 1), false);
const x: Option<number> = none;
assert.equal(x.isNoneOr((x) => x > 1), true);
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none_or


isSome()

isSome(): this is Some<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:39

Returns true if the option is a Some value.

Returns

this is Some<T>

Examples

const x: Option<number> = some(2);
assert.equal(x.isSome(), true);
const x: Option<number> = none;
assert.equal(x.isSome(), false);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some


isSomeAnd()

Call Signature

isSomeAnd<R>(cb: (value: T) => value is R): this is Some<R>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:65

Returns true if the option is a Some and the value inside of it matches a predicate.

Type Parameters
Type Parameter
R
Parameters
ParameterTypeDescription
cb(value: T) => value is RThe predicate.
Returns

this is Some<R>

Examples
const x: Option<number> = some(2);
assert.equal(x.isSomeAnd((x) => x > 1), true);
const x: Option<number> = some(0);
assert.equal(x.isSomeAnd((x) => x > 1), false);
const x: Option<number> = none;
assert.equal(x.isSomeAnd((x) => x > 1), false);
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and

Call Signature

isSomeAnd<R>(cb: (value: T) => R): this is Some<R> & R

Defined in: projects/utilities/packages/result/src/lib/Option.ts:66

Returns true if the option is a Some and the value inside of it matches a predicate.

Type Parameters
Type Parameter
R extends boolean
Parameters
ParameterTypeDescription
cb(value: T) => RThe predicate.
Returns

this is Some<R> & R

Examples
const x: Option<number> = some(2);
assert.equal(x.isSomeAnd((x) => x > 1), true);
const x: Option<number> = some(0);
assert.equal(x.isSomeAnd((x) => x > 1), false);
const x: Option<number> = none;
assert.equal(x.isSomeAnd((x) => x > 1), false);
See

https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and


iter()

iter(): Generator<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:449

Returns an iterator over the possibly contained value.

The iterator yields one value if the result is Some, otherwise none.

Returns

Generator<T>

Examples

const x = some(7);
for (const value of x) {
console.log(value);
}
// Logs 7
const x = none;
for (const value of x) {
console.log(value);
}
// Doesn't log

See


map()

map<U>(cb: (value: T) => U): If<Exists, Some<U>, None<any>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:227

Maps an Option<T> to Option<U> by applying a function to a contained value.

Type Parameters

Type Parameter
U

Parameters

ParameterTypeDescription
cb(value: T) => UThe predicate.

Returns

If<Exists, Some<U>, None<any>>

Example

const maybeSomeString = some('Hello, world!');
const maybeSomeLength = maybeSomeString.map((value) => value.length);

assert.equal(maybeSomeLength, some(13));

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.map


mapInto()

mapInto<OutputOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None<any>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:253

Maps a Some<T> to the returned Option<U> by applying a function to a contained value, leaving None untouched.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
cb(value: T) => OutputOptionThe predicate.

Returns

If<Exists, OutputOption, None<any>>

Examples

const input: Option<string> = some('Hello, world!');
const result = input.mapInto((value) => some(value.length));

assert.equal(result, some(13));
const input: Option<string> = none;
const result = input.mapInto((value) => some(value.length));

assert.equal(result, none);

Note

This is an extension not supported in Rust


mapNoneInto()

mapNoneInto<OutputOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:329

Maps a None to the returned Option<U> by applying a function to a contained value, leaving Some<T> untouched.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
cb() => OutputOptionThe predicate.

Returns

If<Exists, Some<T>, OutputOption>

Examples

const input: Option<string> = some('Hello, world!');
const result = input.mapNoneInto(() => some(13));

assert.equal(result, some('Hello, world!'));
const input: Option<string> = none;
const result = input.mapNoneInto(() => some(13));

assert.equal(result, some(13));

Note

This is an extension not supported in Rust


mapOr()

mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: T) => MappedOutputValue): If<Exists, MappedOutputValue, DefaultOutputValue>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:278

Returns the provided default result (if none), or applies a function to the contained value (if any).

Arguments passed to mapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use mapOrElse, which is lazily evaluated.

Type Parameters

Type Parameter
MappedOutputValue
DefaultOutputValue

Parameters

ParameterTypeDescription
defaultValueDefaultOutputValueThe default value.
cb(value: T) => MappedOutputValueThe predicate.

Returns

If<Exists, MappedOutputValue, DefaultOutputValue>

Examples

const x: Option<string> = some('hello');
assert.equal(x.mapOr(42, (value) => value.length), 5);
const x: Option<string> = none;
assert.equal(x.mapOr(42, (value) => value.length), 42);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or


mapOrElse()

mapOrElse<OutputValue, OutputNone>(defaultValue: () => OutputNone, cb: (value: T) => OutputValue): If<Exists, OutputValue, OutputNone>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:303

Computes a default function result (if none), or applies a different function to the contained value (if any).

Type Parameters

Type Parameter
OutputValue
OutputNone

Parameters

ParameterTypeDescription
defaultValue() => OutputNoneThe default value.
cb(value: T) => OutputValueThe predicate.

Returns

If<Exists, OutputValue, OutputNone>

Examples

const x: Option<string> = some('hello');
assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
const x: Option<string> = none;
assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else


match()

match<SomeValue, NoneValue>(branches: object): If<Exists, SomeValue, NoneValue>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:860

Runs ok function if self is Ok, otherwise runs err function.

Type Parameters

Type Parameter
SomeValue
NoneValue

Parameters

ParameterTypeDescription
branches{ none: NoneValue; some: SomeValue; }The branches to match.
branches.none-
branches.some-

Returns

If<Exists, SomeValue, NoneValue>

Examples

const option = some(4).match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 4);
const option = none.match({
some: (v) => v,
none: () => 0
});
assert.equal(option, 0);

ne()

ne(other: Option<T>): boolean

Defined in: projects/utilities/packages/result/src/lib/Option.ts:835

Checks whether or not other doesn't equal with self.

Parameters

ParameterTypeDescription
otherOption<T>The other option to compare.

Returns

boolean

See

https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne


okOr()

okOr<ErrorValue>(error: ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:399

Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

Arguments passed to okOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use okOrElse, which is lazily evaluated.

Type Parameters

Type Parameter
ErrorValue

Parameters

ParameterType
errorErrorValue

Returns

If<Exists, Ok<T>, Err<ErrorValue>>

Examples

const x: Option<string> = some('hello');
assert.equal(x.okOr(0), ok('hello'));
const x: Option<string> = none;
assert.equal(x.okOr(0), err(0));

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or


okOrElse()

okOrElse<ErrorValue>(cb: () => ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:420

Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err()).

Type Parameters

Type Parameter
ErrorValue

Parameters

ParameterTypeDescription
cb() => ErrorValueThe error to be used.

Returns

If<Exists, Ok<T>, Err<ErrorValue>>

Examples

const x: Option<string> = some('hello');
assert.equal(x.okOrElse(() => 0), ok('hello'));
const x: Option<string> = none;
assert.equal(x.okOrElse(() => 0), err(0));

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else


or()

or<OutputOption>(option: OutputOption): If<Exists, Some<T>, OutputOption>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:542

Returns the option if it contains a value, otherwise returns option.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
optionOutputOptionThe option.

Returns

If<Exists, Some<T>, OutputOption>

Examples

const x: Option<number> = some(2);
const y: Option<number> = none;
assert.equal(x.or(y), some(2));
const x: Option<number> = none;
const y: Option<number> = some(100);
assert.equal(x.or(y), some(100));
const x: Option<number> = some(2);
const y: Option<number> = some(100);
assert.equal(x.or(y), some(2));
const x: Option<number> = none;
const y: Option<number> = none;
assert.equal(x.or(y), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.or


orElse()

orElse<OutputOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:564

Calls cb if the result is Ok, otherwise returns the Err value of self.

This function can be used for control flow based on Result values.

Type Parameters

Type Parameter
OutputOption extends Any

Parameters

ParameterTypeDescription
cb() => OutputOptionThe predicate.

Returns

If<Exists, Some<T>, OutputOption>

Example

const nobody = (): Option<string> => none;
const vikings = (): Option<string> => some('vikings');

assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
assert.equal(none.orElse(vikings), some('vikings'));
assert.equal(none.orElse(nobody), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else


transpose()

transpose<ResultValue, ResultError, ResultSuccess, Exists>(this: Option<Result<ResultValue, ResultError, ResultSuccess>, Exists>): If<Exists, Result<Some<ResultValue>, ResultError, ResultSuccess>, Ok<None<any>>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:765

Transposes an Option of a Result into a Result of an Option.

none will be mapped to ok(none). some(ok(v)) and some(err(e)) will be mapped to ok(some(v)) and err(e).

Type Parameters

Type Parameter
ResultValue
ResultError
ResultSuccess extends boolean
Exists extends boolean

Parameters

ParameterType
thisOption<Result<ResultValue, ResultError, ResultSuccess>, Exists>

Returns

If<Exists, Result<Some<ResultValue>, ResultError, ResultSuccess>, Ok<None<any>>>

Example

const x: Option<Result<number, Error>> = some(ok(5));
const y: Result<Option<number>, Error> = ok(some(5));
assert.equal(x.transpose(), y);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose


unwrap()

unwrap(): If<Exists, T, never>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:168

Returns the contained Some value.

If the value is an Err, it throws an OptionError with the message.

Returns

If<Exists, T, never>

Seealso

unwrapOr

Seealso

unwrapOrElse

Examples

const x: Option<string> = some(2);
assert.equal(x.unwrap(), 2);
const x: Option<string> = none;
assert.throws(() => x.unwrap(), {
name: 'OptionError',
message: 'Unwrap failed'
});

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap


unwrapOr()

unwrapOr<OutputValue>(defaultValue: OutputValue): If<Exists, T, OutputValue>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:191

Returns the contained Some value or a provided default.

Arguments passed to unwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapOrElse, which is lazily evaluated.

Type Parameters

Type Parameter
OutputValue

Parameters

ParameterType
defaultValueOutputValue

Returns

If<Exists, T, OutputValue>

Examples

assert.equal(some(2).unwrapOr(0), 2);
assert.equal(none.unwrapOr(0), 0);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or


unwrapOrElse()

unwrapOrElse<OutputValue>(cb: () => OutputValue): If<Exists, T, OutputValue>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:209

Returns the contained Some value or computes it from a closure.

Type Parameters

Type Parameter
OutputValue

Parameters

ParameterType
cb() => OutputValue

Returns

If<Exists, T, OutputValue>

Examples

assert.equal(some(2).unwrapOrElse(() => 0), 2);
assert.equal(none.unwrapOrElse(() => 0), 0);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else


unzip()

unzip<Value0, Value1, Exists>(this: Option<readonly [Value0, Value1], Exists>): [Option<Value0, Exists>, Option<Value1, Exists>]

Defined in: projects/utilities/packages/result/src/lib/Option.ts:741

Unzips an option containing a tuple of two options.

If self is Some([a, b]) this method returns [Some(a), Some(b)]. Otherwise, [None, None] is returned.

Type Parameters

Type Parameter
Value0
Value1
Exists extends boolean

Parameters

ParameterType
thisOption<readonly [Value0, Value1], Exists>

Returns

[Option<Value0, Exists>, Option<Value1, Exists>]

Examples

const x: Option<[number, string]> = some([1, 'hi']);
assert.equal(x.unzip(), [some(1), some('hi')]);
const x: Option<[number, string]> = none;
assert.equal(x.unzip(), [none, none]);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip


xor()

xor<OtherValue, OtherExists>(option: Option<OtherValue, OtherExists>): If<Exists, If<OtherExists, None<any>, Some<T>>, Option<OtherValue, OtherExists>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:599

Returns Some if exactly one of self or option is Some, otherwise returns None.

Type Parameters

Type Parameter
OtherValue
OtherExists extends boolean

Parameters

ParameterTypeDescription
optionOption<OtherValue, OtherExists>The option to compare.

Returns

If<Exists, If<OtherExists, None<any>, Some<T>>, Option<OtherValue, OtherExists>>

Examples

const x: Option<number> = some(2);
const y: Option<number> = none;
assert.equal(x.xor(y), some(2));
const x: Option<number> = none;
const y: Option<number> = some(2);
assert.equal(x.xor(y), some(2));
const x: Option<number> = some(2);
const y: Option<number> = some(2);
assert.equal(x.xor(y), none);
const x: Option<number> = none;
const y: Option<number> = none;
assert.equal(x.xor(y), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.xor


zip()

zip<OtherValue, OtherExists>(other: Option<OtherValue, OtherExists>): Option<[T, OtherValue], If<Exists, OtherExists, false>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:680

Zips self with another Option.

If self is Some(s) and other is Some(o), this method returns Some([s, o]). Otherwise, None is returned.

Type Parameters

Type Parameter
OtherValue
OtherExists extends boolean

Parameters

ParameterTypeDescription
otherOption<OtherValue, OtherExists>The option to zip self with.

Returns

Option<[T, OtherValue], If<Exists, OtherExists, false>>

Example

const x = some(1);
const y = some('hi');
const z = none;

assert.equal(x.zip(y), some([1, 'hi']));
assert.equal(x.zip(z), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.zip


zipWith()

zipWith<OtherValue, OtherExists, ReturnValue>(other: Option<OtherValue, OtherExists>, f: (value0: T, value1: OtherValue) => ReturnValue): Option<ReturnValue, If<Exists, OtherExists, false>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:715

Zips self and another Option with function f.

If self is Some(s) and other is Some(o), this method returns Some(f(s, o)). Otherwise, None is returned.

Type Parameters

Type Parameter
OtherValue
OtherExists extends boolean
ReturnValue

Parameters

ParameterTypeDescription
otherOption<OtherValue, OtherExists>The option to zip self with.
f(value0: T, value1: OtherValue) => ReturnValueThe function that computes the returned value.

Returns

Option<ReturnValue, If<Exists, OtherExists, false>>

Example

class Point {
public readonly x: number;
public readonly y: number;

public constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}

const x = some(17.5);
const y = some(42.7);

assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);

See

https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with


[hasInstance]()

static [hasInstance](instance: unknown): boolean

Defined in: projects/utilities/packages/result/src/lib/Option.ts:926

Checks if the instance object is an instance of Option, or if it is a Option-like object. This override exists to interoperate with other versions of this class, such as the one coming from another version of this library or from a different build.

Parameters

ParameterTypeDescription
instanceunknownThe instance to check.

Returns

boolean

Whether or not the instance is a Option.

Example

import { Option } from '@sapphire/result';
const { some } = require('@sapphire/result');

some(2) instanceof Option; // true

all()

static all<Entries>(this: void, results: Entries): Option<UnwrapSomeArray<Entries>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:1006

Creates an Ok that is the combination of all collected Ok values as an array, or the first Err encountered.

Type Parameters

Type Parameter
Entries extends readonly Any[]

Parameters

ParameterTypeDescription
thisvoid-
resultsEntriesAn array of Results.

Returns

Option<UnwrapSomeArray<Entries>>

A new Result.


any()

static any<Entries>(this: void, results: Entries): Option<UnwrapSome<Entries[number]>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:1024

Returns the first encountered Some, or a None if none was found.

Type Parameters

Type Parameter
Entries extends readonly Any[]

Parameters

ParameterType
thisvoid
resultsEntries

Returns

Option<UnwrapSome<Entries[number]>>

A new Option.


from()

static from<T>(this: void, op: OptionResolvable<T, boolean> | () => OptionResolvable<T, boolean>): Option<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:975

Creates a Result out of a callback.

Type Parameters

Type Parameter
T

Parameters

ParameterType
thisvoid
opOptionResolvable<T, boolean> | () => OptionResolvable<T, boolean>

Returns

Option<T>

Typeparam

T The result's type.

Typeparam

E The error's type.


fromAsync()

static fromAsync<T>(this: void, op: Awaitable<OptionResolvable<T, boolean>> | () => Awaitable<OptionResolvable<T, boolean>>): Promise<Option<T>>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:990

Creates a Result out of a promise or async callback.

Type Parameters

Type Parameter
T

Parameters

ParameterType
thisvoid
opAwaitable<OptionResolvable<T, boolean>> | () => Awaitable<OptionResolvable<T, boolean>>

Returns

Promise<Option<T>>

Typeparam

T The result's type.

Typeparam

E The error's type.


is()

static is(instance: unknown): instance is Any

Defined in: projects/utilities/packages/result/src/lib/Option.ts:946

Parameters

ParameterTypeDescription
instanceunknownThe instance to check.

Returns

instance is Any

true if the instance is a Option or a Option-like object, false otherwise.

Deprecated

Use Option.isOption instead.

Checks if the instance object is an instance of Option, or if it is a Option-like object.

Example

import { Option } from '@sapphire/result';
const { some } = require('@sapphire/result');

Option.isOption(some(2)); // true

isOption()

static isOption(instance: unknown): instance is Any

Defined in: projects/utilities/packages/result/src/lib/Option.ts:964

Checks if the instance object is an instance of Option, or if it is a Option-like object.

Parameters

ParameterTypeDescription
instanceunknownThe instance to check.

Returns

instance is Any

true if the instance is a Option or a Option-like object, false otherwise.

Example

import { Option } from '@sapphire/result';
const { some } = require('@sapphire/result');

Option.isOption(some(2)); // true

some()

static some<T>(this: void, value?: T): Some<T>

Defined in: projects/utilities/packages/result/src/lib/Option.ts:904

Type Parameters

Type ParameterDefault type
Tundefined

Parameters

ParameterType
thisvoid
value?T

Returns

Some<T>