Skip to main content

Class: Err<E>

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

Type parameter
E

Implements

Constructors

new Err()

new Err<E>(error: E): Err<E>

Parameters

ParameterType
errorE

Returns

Err<E>

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:12

Properties

error

private readonly error: E

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:10

Methods

[iterator]()

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

Returns an iterator over the possibly contained value.

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

Returns

Generator<never, any, unknown>

Implementation of

IResult . [iterator]

Examples

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

See

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:182


and()

and(result?: Result<any, E>): this

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

Parameters

ParameterTypeDescription
result?Result<any, E>The result to check.

Returns

this

Implementation of

IResult . and

Examples

const x: Result<number, string> = ok(2);
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Late error'));
const x: Result<number, string> = err('Early error');
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Early error'));
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

Source

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


andThen()

andThen(cb?: (value: never) => Result<any, E>): this

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.

Parameters

ParameterTypeDescription
cb?(value: never) => Result<any, E>The predicate.

Returns

this

Implementation of

IResult . andThen

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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:128


contains()

contains(value?: any): false

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

Parameters

ParameterTypeDescription
value?anyThe value to compare.

Returns

false

Implementation of

IResult . contains

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.contains(2), true);
const x: Result<number, string> = ok(3);
assert.equal(x.contains(2), false);
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

Source

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


containsErr()

containsErr(error: E): boolean

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

Parameters

ParameterTypeDescription
errorEThe error to compare.

Returns

boolean

Implementation of

IResult . containsErr

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.containsErr('Some error message'), false);
const x: Result<number, string> = err('Some error message');
assert.equal(x.containsErr('Some error message'), true);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:146


eq()

eq(other)

eq(other: Ok<any>): false

Checks whether or not other equals with self.

Parameters
ParameterTypeDescription
otherOk<any>The other result to compare.
Returns

false

Implementation of

IResult . eq

See

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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:166

eq(other)

eq(other: Result<any, E>): boolean

Parameters
ParameterType
otherResult<any, E>
Returns

boolean

Implementation of

IResult.eq

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:167


err()

err(): Some<E>

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

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

Returns

Some<E>

Implementation of

IResult . err

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.err(), none);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:37


expect()

expect(message: string): never

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

ParameterTypeDescription
messagestringThe message for the error.

Returns

never

Implementation of

IResult . expect

Examples

const x = ok(2);
assert.equal(x.expect('Whoops!'), 2);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:93


expectErr()

expectErr(message?: string): E

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

ParameterTypeDescription
message?stringThe message for the error.

Returns

E

Implementation of

IResult . expectErr

Examples

const x = ok(2);
assert.throws(() => x.expectErr('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 2
});
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

Source

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


flatten()

flatten(): this

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

Returns

this

Implementation of

IResult . flatten

Examples

const x: Result<Result<string, number>, number> = ok(ok('Hello'));
assert.equal(x.flatten(), ok('Hello'));
const x: Result<Result<string, number>, number> = ok(err(6));
assert.equal(x.flatten(), err(6));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:154


inspect()

inspect(cb?: (value: never) => void): this

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

Parameters

ParameterTypeDescription
cb?(value: never) => voidThe predicate.

Returns

this

Implementation of

IResult . inspect

Seealso

inspectAsync for the awaitable version.

Examples

ok(2).inspect(console.log);
// Logs: 2
err('Some error message').inspect(console.log);
// Doesn't log

See

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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:69


inspectAsync()

inspectAsync(cb?: (value: never) => unknown): Promise <Err<E>>

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

Parameters

ParameterTypeDescription
cb?(value: never) => unknownThe predicate.

Returns

Promise <Err<E>>

Implementation of

IResult . inspectAsync

Seealso

inspect for the sync version.

Examples

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

Note

This is an extension not supported in Rust

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:74


inspectErr()

inspectErr(cb: (error: E) => void): this

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

Parameters

ParameterTypeDescription
cb(error: E) => voidThe predicate.

Returns

this

Implementation of

IResult . inspectErr

Seealso

inspectErrAsync for the awaitable version.

Examples

ok(2).inspectErr(console.log);
// Doesn't log
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:79


inspectErrAsync()

inspectErrAsync(cb: (error: E) => unknown): Promise <Err<E>>

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

Parameters

ParameterTypeDescription
cb(error: E) => unknownThe predicate.

Returns

Promise <Err<E>>

Implementation of

IResult . inspectErrAsync

Seealso

inspectErr for the sync version.

Examples

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

Note

This is an extension not supported in Rust

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:84


intoOkOrErr()

intoOkOrErr(): E

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

Returns

E

Implementation of

IResult . intoOkOrErr

Examples

let x: Result<number, number> = ok(3);
assert.equal(x.intoOkOrErr(), 3);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:158


intoPromise()

intoPromise(): Promise <Err <Awaited<E>>>

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

Returns

Promise <Err <Awaited<E>>>

Implementation of

IResult . intoPromise

Example

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

Note

This is an extension not supported in Rust

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:162


isErr()

isErr(): this is Err<E>

Returns true if the result is Err.

Returns

this is Err<E>

Implementation of

IResult . isErr

Examples

const x = ok(-3);
assert.equal(x.isErr(), false);
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

Source

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


isErrAnd()

isErrAnd(cb: (error: E) => boolean): boolean

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

Parameters

ParameterTypeDescription
cb(error: E) => booleanThe predicate.

Returns

boolean

Implementation of

IResult . isErrAnd

Examples

const x = ok(2);
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
const x = err(new Error('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
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

Source

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


isOk()

isOk(): false

Returns true if the result is Ok.

Returns

false

Implementation of

IResult . isOk

Examples

const x = ok(-3);
assert.equal(x.isOk(), true);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:16


isOkAnd()

isOkAnd(cb?: (value: never) => boolean): false

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

Parameters

ParameterType
cb?(value: never) => boolean

Returns

false

Implementation of

IResult . isOkAnd

Examples

const x = ok(2);
assert.equal(x.isOkAnd((value) => value > 1), true);
const x = ok(0);
assert.equal(x.isOkAnd((value) => value > 1), false);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:20


iter()

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

Returns an iterator over the possibly contained value.

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

Returns

Generator<never, any, unknown>

Implementation of

IResult . iter

Examples

const x = ok(7);
for (const value of x.iter()) {
console.log(value);
}
// Logs 7
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:89


map()

map(cb?: (value: never) => unknown): this

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

Parameters

ParameterTypeDescription
cb?(value: never) => unknownThe predicate.

Returns

this

Implementation of

IResult . map

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.map((value) => value * 2), ok(4));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:41


mapErr()

mapErr<F>(cb: (error: E) => F): Err<F>

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.

Type parameters

Type parameter
F

Parameters

ParameterTypeDescription
cb(error: E) => FThe predicate.

Returns

Err<F>

Implementation of

IResult . mapErr

Examples

const x: Result<number, Error> = ok(2);
assert.equal(x.mapErr((error) => error.message), ok(2));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:61


mapErrInto()

mapErrInto<R>(cb: (error: E) => R): R

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.

Type parameters

Type parameter
R extends Result<any, any>

Parameters

ParameterTypeDescription
cb(error: E) => RThe predicate.

Returns

R

Implementation of

IResult . mapErrInto

Examples

const x: Result<number, Error> = ok(2);
assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));
const x: Result<number, Error> = err(new Error('Some error message'));
assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:65


mapInto()

mapInto(cb: (value: never) => Result<any, any>): this

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.

Parameters

ParameterTypeDescription
cb(value: never) => Result<any, any>The predicate.

Returns

this

Implementation of

IResult . mapInto

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.mapInto((value) => ok(value * value)), ok(4));
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')
);
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

Source

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


mapOr()

mapOr<U>(defaultValue: U, cb?: (value: never) => U): 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

Type parameter
U

Parameters

ParameterTypeDescription
defaultValueUThe default value to use.
cb?(value: never) => UThe predicate.

Returns

U

Implementation of

IResult . mapOr

Examples

const x = ok('hello');
assert.equal(x.mapOr(42, (value) => value.length), 5);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:51


mapOrElse()

mapOrElse<U>(op: (error: E) => U, cb?: (value: never) => U): 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

Type parameter
U

Parameters

ParameterTypeDescription
op(error: E) => UThe predicate that is run on Err.
cb?(value: never) => UThe predicate that is run on Ok.

Returns

U

Implementation of

IResult . mapOrElse

Examples

const x: Result<string, string> = ok('hello');
assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:56


match()

match<OkValue, ErrValue>(branches: object): ErrValue

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

Type parameters

Type parameter
OkValue
ErrValue

Parameters

ParameterTypeDescription
branchesobjectThe branches to match.
branches.err-
branches.ok-

Returns

ErrValue

Implementation of

IResult . match

Examples

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

Source

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


ne()

ne(other)

ne(other: Ok<any>): true

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

Parameters
ParameterTypeDescription
otherOk<any>The other result to compare.
Returns

true

Implementation of

IResult . ne

See

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

Source

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

ne(other)

ne(other: Result<any, E>): boolean

Parameters
ParameterType
otherResult<any, E>
Returns

boolean

Implementation of

IResult.ne

Source

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


ok()

ok(): None

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

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

Returns

None

Implementation of

IResult . ok

Examples

const x: Result<number, string> = ok(2);
assert.equal(x.ok(), some(2));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:33


or()

or<R>(result: R): R

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.

Type parameters

Type parameter
R extends Result<any, any>

Parameters

ParameterTypeDescription
resultRThe result to check.

Returns

R

Implementation of

IResult . or

Examples

const x: Result<number, string> = ok(2);
const y: Result<number, string> = err('Late error');
assert.equal(x.or(y), ok(2));
const x: Result<number, string> = err('Early error');
const y: Result<number, string> = ok(2);
assert.equal(x.or(y), ok(2));
const x: Result<number, string> = err('Early error');
const y: Result<number, string> = err('Late error');
assert.equal(x.or(y), err('Late error'));
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:133


orElse()

orElse<R>(cb: (error: E) => R): R

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.

Type parameters

Type parameter
R extends Result<any, any>

Parameters

ParameterTypeDescription
cb(error: E) => RThe predicate.

Returns

R

Implementation of

IResult . orElse

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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:137


transpose()

transpose(): Some <Err<E>>

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

Returns

Some <Err<E>>

Implementation of

IResult . transpose

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

Source

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


unwrap()

unwrap(): never

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

never

Implementation of

IResult . unwrap

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapErr

Seealso

unwrapRaw

Examples

const x = ok(2);
assert.equal(x.unwrap(), 2);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:102


unwrapErr()

unwrapErr(): E

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

E

Implementation of

IResult . unwrapErr

Seealso

unwrap

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapRaw

Examples

const x = ok(2);
assert.throws(() => x.unwrapErr(), {
name: 'ResultError',
message: 'Unwrap failed',
value: 2
});
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:106


unwrapOr()

unwrapOr<T>(defaultValue: T): 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.

Type parameters

Type parameter
T

Parameters

ParameterTypeDescription
defaultValueTThe default value.

Returns

T

Implementation of

IResult . unwrapOr

Seealso

unwrap

Seealso

unwrapOrElse

Seealso

unwrapErr

Seealso

unwrapRaw

Examples

const x: Result<number, string> = ok(9);
assert.equal(x.unwrapOr(2), 9);
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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:110


unwrapOrElse()

unwrapOrElse<T>(op: (error: E) => T): T

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

Type parameters

Type parameter
T

Parameters

ParameterTypeDescription
op(error: E) => TThe predicate.

Returns

T

Implementation of

IResult . unwrapOrElse

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

Source

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


unwrapRaw()

unwrapRaw(): never

Returns the contained Ok value.

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

Returns

never

Implementation of

IResult . unwrapRaw

Seealso

unwrap

Seealso

unwrapOr

Seealso

unwrapOrElse

Seealso

unwrapErr

Examples

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

Source

projects/utilities/packages/result/src/lib/Result/Err.ts:118