Skip to main content

Class: Ok<T>

@sapphire/result.Ok

A type used to express computations that can fail, it can be used for returning and propagating errors. This is a type union with the variants Ok(T), representing success and containing a value, and Err(E), representing error and containing an error value.

Typeparam

T The result's type.

Typeparam

E The error's type.

See

https://doc.rust-lang.org/std/result/index.html

Type parameters

Name
T

Implements

Constructors

constructor

new Ok<T>(value): Ok<T>

Type parameters

Name
T

Parameters

NameType
valueT

Returns

Ok<T>

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:13

Properties

value

Private Readonly value: T

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:11

Methods

[iterator]

[iterator](): Generator<T, any, unknown>

Returns an iterator over the possibly contained value.

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

Returns

Generator<T, any, unknown>

Example

const x = ok(7);
for (const value of x) {
console.log(value);
}
// Logs 7

Example

const x = err('Nothing!');
for (const value of x) {
console.log(value);
}
// Doesn't log

See

Implementation of

IResult.[iterator]

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:188


and

and<R>(result): R

Returns result if the result is Ok, otherwise returns the Err value of itself.

Type parameters

NameType
Rextends Result<any, any>

Parameters

NameTypeDescription
resultRThe result to check.

Returns

R

Example

const x: Result<number, string> = ok(2);
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Late error'));

Example

const x: Result<number, string> = err('Early error');
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Early error'));

Example

const x: Result<number, string> = ok(2);
const y: Result<string, string> = ok('Hello');
assert.equal(x.and(y), ok('Hello'));

See

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

Implementation of

IResult.and

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:123


andThen

andThen<R>(cb): R

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

NameType
Rextends Result<any, any>

Parameters

NameTypeDescription
cb(value: T) => RThe predicate.

Returns

R

Example

function fractionOf4(value: number) {
return value === 0 ? err('overflowed') : ok(4 / value);
}

assert.equal(ok(2).andThen(fractionOf4), ok(4));
assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));

See

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

Implementation of

IResult.andThen

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:127


contains

contains(value): boolean

Returns true if the result is an Ok and the given value strict equals it.

Parameters

NameTypeDescription
valueTThe value to compare.

Returns

boolean

Example

const x: Result<number, string> = ok(2);
assert.equal(x.contains(2), true);

Example

const x: Result<number, string> = ok(3);
assert.equal(x.contains(2), false);

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.contains(2), false);

See

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

Implementation of

IResult.contains

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:141


containsErr

containsErr(error?): false

Returns true if the result is an Err and the given error strict equals it.

Parameters

NameTypeDescription
error?unknownThe error to compare.

Returns

false

Example

const x: Result<number, string> = ok(2);
assert.equal(x.containsErr('Some error message'), false);

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.containsErr('Some error message'), true);

Example

const x: Result<number, string> = err('Some other error message');
assert.equal(x.containsErr('Some error message'), false);

See

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

Implementation of

IResult.containsErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:145


eq

eq(other): false

Checks whether or not other equals with self.

Parameters

NameTypeDescription
otherErr<any>The other result to compare.

Returns

false

See

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

Implementation of

IResult.eq

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:172

eq(other): boolean

Parameters

NameType
otherResult<T, any>

Returns

boolean

Implementation of

IResult.eq

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:173


err

err(): None

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

Converts itself into an Option<E>, and discarding the successful value, if any.

Returns

None

Example

const x: Result<number, string> = ok(2);
assert.equal(x.err(), none);

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.err(), 'Some error message');

See

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

Implementation of

IResult.err

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:38


expect

expect(message?): T

Returns the contained Ok value.

If the value is an Err, it throws a ResultError with the given message and the content of the Err.

Parameters

NameTypeDescription
message?stringThe message for the error.

Returns

T

Example

const x = ok(2);
assert.equal(x.expect('Whoops!'), 2);

Example

const x = err('Emergency failure');
assert.throws(() => x.expect('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 'Emergency failure'
});

See

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

Implementation of

IResult.expect

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:92


expectErr

expectErr(message): never

Returns the contained Err value.

If the value is an Ok, it throws a ResultError with the given message and the content of the Ok.

Parameters

NameTypeDescription
messagestringThe message for the error.

Returns

never

Example

const x = ok(2);
assert.throws(() => x.expectErr('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 2
});

Example

const x = err('Emergency failure');
assert.equal(x.expectErr('Whoops!'), 'Emergency failure');

See

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

Implementation of

IResult.expectErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:97


flatten

flatten<Inner>(this): Inner

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

Type parameters

NameType
Innerextends Result<any, any>

Parameters

NameType
thisOk<Inner>

Returns

Inner

Example

const x: Result<Result<string, number>, number> = ok(ok('Hello'));
assert.equal(x.flatten(), ok('Hello'));

Example

const x: Result<Result<string, number>, number> = ok(err(6));
assert.equal(x.flatten(), err(6));

Example

const x: Result<Result<string, number>, number> = err(6);
assert.equal(x.flatten(), err(6));

See

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

Implementation of

IResult.flatten

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:160


inspect

inspect(cb): this

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

Parameters

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

Returns

this

Seealso

inspectAsync for the awaitable version.

Example

ok(2).inspect(console.log);
// Logs: 2

Example

err('Some error message').inspect(console.log);
// Doesn't log

See

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

Implementation of

IResult.inspect

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:68


inspectAsync

inspectAsync(cb): Promise<Ok<T>>

Calls the provided closure with a reference to the contained value (if Ok) and awaits it.

Parameters

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

Returns

Promise<Ok<T>>

Seealso

inspect for the sync version.

Example

await ok(2).inspectAsync(console.log);
// Logs: 2

Example

await err('Some error message').inspectAsync(console.log);
// Doesn't log

Note

This is an extension not supported in Rust

Implementation of

IResult.inspectAsync

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:73


inspectErr

inspectErr(cb?): this

Calls the provided closure with a reference to the contained error (if Err).

Parameters

NameTypeDescription
cb?(error: never) => voidThe predicate.

Returns

this

Seealso

inspectErrAsync for the awaitable version.

Example

ok(2).inspectErr(console.log);
// Doesn't log

Example

err('Some error message').inspectErr(console.log);
// Logs: Some error message

See

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

Implementation of

IResult.inspectErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:78


inspectErrAsync

inspectErrAsync(cb?): Promise<Ok<T>>

Calls the provided closure with a reference to the contained error (if Err) and awaits it.

Parameters

NameTypeDescription
cb?(error: never) => unknownThe predicate.

Returns

Promise<Ok<T>>

Seealso

inspectErr for the sync version.

Example

await ok(2).inspectErrAsync(console.log);
// Doesn't log

Example

await err('Some error message').inspectErrAsync(console.log);
// Logs: Some error message

Note

This is an extension not supported in Rust

Implementation of

IResult.inspectErrAsync

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:83


intoOkOrErr

intoOkOrErr(): T

Returns the Ok value if self is Ok, and the Err value if self is Err.

Returns

T

Example

let x: Result<number, number> = ok(3);
assert.equal(x.intoOkOrErr(), 3);

Example

let x: Result<number, number> = err(4);
assert.equal(x.intoOkOrErr(), 4);

See

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

Implementation of

IResult.intoOkOrErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:164


intoPromise

intoPromise(): Promise<Ok<Awaited<T>>>

Returns a Promise object with the awaited value (if Ok) or the awaited error (if Err).

Returns

Promise<Ok<Awaited<T>>>

Example

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

Note

This is an extension not supported in Rust

Implementation of

IResult.intoPromise

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:168


isErr

isErr(): false

Returns true if the result is Err.

Returns

false

Example

const x = ok(-3);
assert.equal(x.isErr(), false);

Example

const x = err('Some error message');
assert.equal(x.isErr(), true);

See

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

Implementation of

IResult.isErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:25


isErrAnd

isErrAnd(cb?): false

Returns true if the result is Err and the value inside of it matches a predicate.

Parameters

NameTypeDescription
cb?(error: never) => booleanThe predicate.

Returns

false

Example

const x = ok(2);
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);

Example

const x = err(new Error('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);

Example

const x = err(new TypeError('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), true);

See

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

Implementation of

IResult.isErrAnd

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:29


isOk

isOk(): this is Ok<T>

Returns true if the result is Ok.

Returns

this is Ok<T>

Example

const x = ok(-3);
assert.equal(x.isOk(), true);

Example

const x = err('Some error message');
assert.equal(x.isOk(), false);

See

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

Implementation of

IResult.isOk

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:17


isOkAnd

isOkAnd<R>(cb): R

Returns true if the result is Ok and the value inside of it matches a predicate.

Type parameters

NameType
Rextends boolean

Parameters

NameType
cb(value: T) => R

Returns

R

Example

const x = ok(2);
assert.equal(x.isOkAnd((value) => value > 1), true);

Example

const x = ok(0);
assert.equal(x.isOkAnd((value) => value > 1), false);

Example

const x = err('Some error message');
assert.equal(x.isOkAnd((value) => value > 1), false);

See

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

Implementation of

IResult.isOkAnd

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:21


iter

iter(): Generator<T, any, unknown>

Returns an iterator over the possibly contained value.

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

Returns

Generator<T, any, unknown>

Example

const x = ok(7);
for (const value of x.iter()) {
console.log(value);
}
// Logs 7

Example

const x = err('Nothing!');
for (const value of x.iter()) {
console.log(value);
}
// Doesn't log

See

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

Implementation of

IResult.iter

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:88


map

map<U>(cb): Ok<U>

Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.

Type parameters

Name
U

Parameters

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

Returns

Ok<U>

Example

const x: Result<number, string> = ok(2);
assert.equal(x.map((value) => value * 2), ok(4));

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.map((value) => value * 2), err('Some error message'));

See

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

Implementation of

IResult.map

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:42


mapErr

mapErr(cb?): this

Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.

This function can be used to pass through a successful result while handling an error.

Parameters

NameTypeDescription
cb?(error: never) => anyThe predicate.

Returns

this

Example

const x: Result<number, Error> = ok(2);
assert.equal(x.mapErr((error) => error.message), ok(2));

Example

const x: Result<number, Error> = err(new Error('Some error message'));
assert.equal(x.mapErr((error) => error.message), err('Some error message'));

See

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

Implementation of

IResult.mapErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:58


mapErrInto

mapErrInto(cb): this

Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.

This function can be used to pass through a successful result while handling an error.

Unlike mapErr, this method does not wrap the returned value inside Err, but instead, it returns the returned value.

Parameters

NameTypeDescription
cb(error: never) => Result<any, any>The predicate.

Returns

this

Example

const x: Result<number, Error> = ok(2);
assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));

Example

const x: Result<number, Error> = err(new Error('Some error message'));
assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));

Example

const x: Result<number, Error> = err(new Error('Some error message'));
assert.equal(x.mapErrInto((error) => ok(4)), ok(4));

Note

This is an extension not supported in Rust

Implementation of

IResult.mapErrInto

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:63


mapInto

mapInto<R>(cb): R

Maps a Result<T, E> to Result<T, F> by applying a function to a contained Ok value, leaving an Err value untouched.

Unlike map, this method does not wrap the returned value inside Ok, but instead, it returns the returned value.

Type parameters

NameType
Rextends Result<any, any>

Parameters

NameTypeDescription
cb(value: T) => RThe predicate.

Returns

R

Example

const x: Result<number, string> = ok(2);
assert.equal(x.mapInto((value) => ok(value * value)), ok(4));

Example

const x: Result<number, string> = ok(0);
assert.equal(
x.mapInto((value) => (value === 0 ? err('zero is not divisible') : ok(1 / value))),
err('zero is not divisible')
);

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.mapInto((value) => ok(4)), err('Some error message'));

Note

This is an extension not supported in Rust

Implementation of

IResult.mapInto

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:46


mapOr

mapOr<U>(_, cb): U

Returns the provided default (if Err), or applies a function to the contained value (if Ok),

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

Name
U

Parameters

NameTypeDescription
_UThe default value to use.
cb(value: T) => UThe predicate.

Returns

U

Example

const x = ok('hello');
assert.equal(x.mapOr(42, (value) => value.length), 5);

Example

const x = err('Some error message');
assert.equal(x.mapOr(42, (value) => value.length), 42);

See

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

Implementation of

IResult.mapOr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:50


mapOrElse

mapOrElse<U>(_, cb): U

Maps a Result<T, E> to U by applying fallback function default to a contained Err value, or function cb to a contained Ok value.

This function can be used to unpack a successful result while handling an error.

Type parameters

Name
U

Parameters

NameTypeDescription
_(error: never) => UThe predicate that is run on Err.
cb(value: T) => UThe predicate that is run on Ok.

Returns

U

Example

const x: Result<string, string> = ok('hello');
assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);

Example

const x: Result<string, string> = err('Some error message');
assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 18);

See

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

Implementation of

IResult.mapOrElse

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:54


match

match<OkValue, ErrValue>(branches): OkValue

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

Type parameters

Name
OkValue
ErrValue

Parameters

NameTypeDescription
branchesObjectThe branches to match.
branches.err(error: never) => ErrValue-
branches.ok(value: T) => OkValue-

Returns

OkValue

Example

const result = ok(4).match({
ok: (v) => v,
err: () => 0
});
assert.equal(result, 4);

Example

const result = err('Hello').match({
ok: (v) => v,
err: () => 0
});
assert.equal(result, 0);

Implementation of

IResult.match

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:184


ne

ne(other): true

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

Parameters

NameTypeDescription
otherErr<any>The other result to compare.

Returns

true

See

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

Implementation of

IResult.ne

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:178

ne(other): boolean

Parameters

NameType
otherResult<T, any>

Returns

boolean

Implementation of

IResult.ne

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:179


ok

ok(): Some<T>

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

Converts itself into an Option<T>, and discarding the error, if any.

Returns

Some<T>

Example

const x: Result<number, string> = ok(2);
assert.equal(x.ok(), some(2));

Example

const x: Result<number, string> = err('Some error message');
assert.equal(x.ok(), none);

See

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

Implementation of

IResult.ok

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:34


or

or(result): this

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

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

Parameters

NameTypeDescription
resultResult<T, any>The result to check.

Returns

this

Example

const x: Result<number, string> = ok(2);
const y: Result<number, string> = err('Late error');
assert.equal(x.or(y), ok(2));

Example

const x: Result<number, string> = err('Early error');
const y: Result<number, string> = ok(2);
assert.equal(x.or(y), ok(2));

Example

const x: Result<number, string> = err('Early error');
const y: Result<number, string> = err('Late error');
assert.equal(x.or(y), err('Late error'));

Example

const x: Result<number, string> = ok(2);
const y: Result<number, string> = ok(100);
assert.equal(x.or(y), ok(2));

See

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

Implementation of

IResult.or

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:131


orElse

orElse(cb): this

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

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

Parameters

NameTypeDescription
cb(error: never) => Result<T, any>The predicate.

Returns

this

Example

const square = (x: number): Result<number, string> => ok(x * x);
const wrapErr = (x: number): Result<number, string> => err(x);

assert.equal(ok(2).orElse(square).orElse(square), ok(2));
assert.equal(ok(2).orElse(wrapErr).orElse(square), ok(2));
assert.equal(err(3).orElse(square).orElse(wrapErr), ok(9));
assert.equal(err(3).orElse(wrapErr).orElse(wrapErr), err(3));

See

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

Implementation of

IResult.orElse

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:136


transpose

transpose(this): None

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

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

Parameters

NameType
thisOk<None>

Returns

None

Example

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

See

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

Implementation of

IResult.transpose

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:150

transpose<Inner>(this): Some<Ok<Inner>>

Type parameters

Name
Inner

Parameters

NameType
thisOk<Some<Inner>>

Returns

Some<Ok<Inner>>

Implementation of

IResult.transpose

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:151

transpose<Inner>(this): Option<Ok<Inner>>

Type parameters

Name
Inner

Parameters

NameType
thisOk<Option<Inner>>

Returns

Option<Ok<Inner>>

Implementation of

IResult.transpose

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:152


unwrap

unwrap(): T

Returns the contained Ok value.

If the value is an Err, it throws a ResultError with the message, and the content of the Err.

Returns

T

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapErr

Seealso

unwrapRaw

Example

const x = ok(2);
assert.equal(x.unwrap(), 2);

Example

const x = err('Emergency failure');
assert.throws(() => x.unwrap(), {
name: 'ResultError',
message: 'Unwrap failed',
value: 'Emergency failure'
});

See

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

Implementation of

IResult.unwrap

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:101


unwrapErr

unwrapErr(): never

Returns the contained Err value.

If the value is an Ok, it throws a ResultError with the message, and the content of the Ok.

Returns

never

Seealso

unwrap

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapRaw

Example

const x = ok(2);
assert.throws(() => x.unwrapErr(), {
name: 'ResultError',
message: 'Unwrap failed',
value: 2
});

Example

const x = err('Emergency failure');
assert.equal(x.unwrapErr(), 'Emergency failure');

See

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

Implementation of

IResult.unwrapErr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:105


unwrapOr

unwrapOr(defaultValue): T

Returns the contained Ok value or the 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.

Parameters

NameTypeDescription
defaultValueunknownThe default value.

Returns

T

Seealso

unwrap

Seealso

unwrapOrElse

Seealso

unwrapErr

Seealso

unwrapRaw

Example

const x: Result<number, string> = ok(9);
assert.equal(x.unwrapOr(2), 9);

Example

const x: Result<number, string> = err('Error');
assert.equal(x.unwrapOr(2), 2);

See

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

Implementation of

IResult.unwrapOr

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:109


unwrapOrElse

unwrapOrElse(op): T

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

Parameters

NameTypeDescription
op(error: any) => unknownThe predicate.

Returns

T

Seealso

unwrap

Seealso

unwrapOr

Seealso

unwrapErr

Seealso

unwrapRaw

Example

const count = (x: string) => x.length;

assert.equal(ok(2).unwrapOrElse(count), 2);
assert.equal(err('hello').unwrapOrElse(count), 5);

See

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

Implementation of

IResult.unwrapOrElse

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:114


unwrapRaw

unwrapRaw(): T

Returns the contained Ok value.

If the value is an Err, it throws the contained error.

Returns

T

Seealso

unwrap

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapErr

Example

const x = ok(2);
assert.equal(x.unwrapRaw(), 2);

Example

const x = err('Emergency failure');
assert.throws(() => x.unwrapRaw(), {
name: 'Error',
message: 'Unwrap failed',
value: 'Emergency failure'
});

Implementation of

IResult.unwrapRaw

Defined in

projects/utilities/packages/result/src/lib/Result/Ok.ts:119