Skip to main content

Class: Result<T, E, Success>

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

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 ParameterDefault type
T-
E-
Success extends booleanboolean

Constructors

new Result()

private new Result<T, E, Success>(value: If<Success, T, E>, success: Success): Result<T, E, Success>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:28

Parameters

ParameterType
valueIf<Success, T, E>
successSuccess

Returns

Result<T, E, Success>

Properties

__STATUS__

protected __STATUS__: Success

Defined in: projects/utilities/packages/result/src/lib/Result.ts:23

Internal

Branded value to ensure Success is typed correctly.


[SuccessProperty]

private readonly [SuccessProperty]: Success

Defined in: projects/utilities/packages/result/src/lib/Result.ts:26


[ValueProperty]

private readonly [ValueProperty]: If<Success, T, E>

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

Accessors

[toStringTag]

Get Signature

get [toStringTag](): If<Success, "Ok", "Err">

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1001

Returns

If<Success, "Ok", "Err">

Methods

[iterator]()

[iterator](): Generator<T>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:997

Returns an iterator over the possibly contained value.

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

Returns

Generator<T>

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


and()

and<OutputResult>(result: OutputResult): If<Success, OutputResult, Err<E, any>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:687

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

Type Parameters

Type Parameter
OutputResult extends Any

Parameters

ParameterTypeDescription
resultOutputResultThe result to check.

Returns

If<Success, OutputResult, Err<E, any>>

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


andThen()

andThen<OutputResult>(cb: (value: T) => OutputResult): If<Success, OutputResult, Err<E, any>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:710

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
OutputResult extends Any

Parameters

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

Returns

If<Success, OutputResult, Err<E, any>>

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


contains()

Call Signature

contains<Value>(this: Ok<T, any>, value: Value): this is Ok<Value, any>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:797

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

Type Parameters
Type Parameter
Value
Parameters
ParameterTypeDescription
thisOk<T, any>-
valueValueThe value to compare.
Returns

this is Ok<Value, any>

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

Call Signature

contains(this: Err<E, any>, value: T): false

Defined in: projects/utilities/packages/result/src/lib/Result.ts:798

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

Parameters
ParameterTypeDescription
thisErr<E, any>-
valueTThe value to compare.
Returns

false

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


containsErr()

Call Signature

containsErr(this: Ok<T, any>, error: E): false

Defined in: projects/utilities/packages/result/src/lib/Result.ts:825

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

Parameters
ParameterTypeDescription
thisOk<T, any>-
errorEThe error to compare.
Returns

false

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

Call Signature

containsErr<Value>(this: Err<E, any>, error: Value): this is Err<Value, any>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:826

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

Type Parameters
Type Parameter
Value
Parameters
ParameterTypeDescription
thisErr<E, any>-
errorValueThe error to compare.
Returns

this is Err<Value, any>

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


eq()

eq<OtherValue, OtherError, OtherSuccess>(other: Result<OtherValue, OtherError, OtherSuccess>): this is Result<OtherValue, OtherError, OtherSuccess>

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

Checks whether or not other equals with self.

Type Parameters

Type Parameter
OtherValue
OtherError
OtherSuccess extends boolean

Parameters

ParameterTypeDescription
otherResult<OtherValue, OtherError, OtherSuccess>The other result to compare.

Returns

this is Result<OtherValue, OtherError, OtherSuccess>

See

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


err()

err(): If<Success, None, Some<E>>

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

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

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

Returns

If<Success, None, Some<E>>

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


expect()

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

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

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

If<Success, T, never>

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


expectErr()

expectErr(message: string): If<Success, never, E>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:512

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
messagestringThe message for the error.

Returns

If<Success, never, E>

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


flatten()

flatten<InnerResult>(this: Result<InnerResult, E, Success>): If<Success, InnerResult, Err<E, any>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:875

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

Type Parameters

Type Parameter
InnerResult extends Any

Parameters

ParameterType
thisResult<InnerResult, E, Success>

Returns

If<Success, InnerResult, Err<E, any>>

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


inspect()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:359

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

Parameters

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

Returns

this

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


inspectAsync()

inspectAsync(cb: (value: T) => unknown): Promise<Result<T, E, Success>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:382

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

Parameters

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

Returns

Promise<Result<T, E, Success>>

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


inspectErr()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:405

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

Parameters

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

Returns

this

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


inspectErrAsync()

inspectErrAsync(cb: (error: E) => unknown): Promise<Result<T, E, Success>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:428

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<Result<T, E, Success>>

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


intoOkOrErr()

intoOkOrErr(): If<Success, T, E>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:895

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

Returns

If<Success, T, E>

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


intoPromise()

intoPromise(): Promise<If<Success, Ok<Awaited<T>, any>, Err<Awaited<E>, any>>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:910

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

Returns

Promise<If<Success, Ok<Awaited<T>, any>, Err<Awaited<E>, any>>>

Example

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

Note

This is an extension not supported in Rust


isErr()

isErr(): this is Err<E, T>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:96

Returns true if the result is Err.

Returns

this is Err<E, T>

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


isErrAnd()

Call Signature

isErrAnd<R>(cb: (error: E) => error is R): this is Err<R, T>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:122

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

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

this is Err<R, T>

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

Call Signature

isErrAnd<R>(cb: (error: E) => R): this is Err<E, T> & R

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

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

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

this is Err<E, T> & R

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


isOk()

isOk(): this is Ok<T, E>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:49

Returns true if the result is Ok.

Returns

this is Ok<T, E>

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


isOkAnd()

Call Signature

isOkAnd<R>(cb: (value: T) => value is R): this is Ok<R, E>

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

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

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

this is Ok<R, E>

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

Call Signature

isOkAnd<R>(cb: (value: T) => R): this is Ok<T, E> & R

Defined in: projects/utilities/packages/result/src/lib/Result.ts:75

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

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

this is Ok<T, E> & R

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


iter()

iter(): Generator<T>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:457

Returns an iterator over the possibly contained value.

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

Returns

Generator<T>

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


map()

map<OutputValue>(cb: (value: If<Success, T, never>) => OutputValue): Result<OutputValue, E, Success>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:190

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

Type Parameter
OutputValue

Parameters

ParameterTypeDescription
cb(value: If<Success, T, never>) => OutputValueThe predicate.

Returns

Result<OutputValue, E, Success>

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


mapErr()

mapErr<OutputError>(cb: (error: If<Success, never, E>) => OutputError): Result<T, OutputError, Success>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:304

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
OutputError

Parameters

ParameterTypeDescription
cb(error: If<Success, never, E>) => OutputErrorThe predicate.

Returns

Result<T, OutputError, Success>

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


mapErrInto()

mapErrInto<OutputResult>(cb: (error: If<Success, never, E>) => OutputResult): If<Success, Ok<T, any>, OutputResult>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:337

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
OutputResult extends Any

Parameters

ParameterTypeDescription
cb(error: If<Success, never, E>) => OutputResultThe predicate.

Returns

If<Success, Ok<T, any>, OutputResult>

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


mapInto()

mapInto<OutputResult>(cb: (value: If<Success, T, never>) => OutputResult): If<Success, OutputResult, Err<E, any>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:224

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

Type Parameter
OutputResult extends Any

Parameters

ParameterTypeDescription
cb(value: If<Success, T, never>) => OutputResultThe predicate.

Returns

If<Success, OutputResult, Err<E, any>>

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


mapOr()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:249

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
MappedOutputValue
DefaultOutputValue

Parameters

ParameterTypeDescription
defaultValueDefaultOutputValueThe default value to use.
cb(value: If<Success, T, never>) => MappedOutputValueThe predicate.

Returns

If<Success, MappedOutputValue, DefaultOutputValue>

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


mapOrElse()

mapOrElse<OutputValue, OutputError>(op: (error: If<Success, never, E>) => OutputError, cb: (value: If<Success, T, never>) => OutputValue): If<Success, OutputValue, OutputError>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:277

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
OutputValue
OutputError

Parameters

ParameterTypeDescription
op(error: If<Success, never, E>) => OutputErrorThe predicate that is run on Err.
cb(value: If<Success, T, never>) => OutputValueThe predicate that is run on Ok.

Returns

If<Success, OutputValue, OutputError>

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


match()

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

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

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

Type Parameters

Type Parameter
OkValue
ErrValue

Parameters

ParameterTypeDescription
branches{ err: ErrValue; ok: OkValue; }The branches to match.
branches.err-
branches.ok-

Returns

If<Success, OkValue, ErrValue>

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

ne()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:939

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

Parameters

ParameterTypeDescription
otherResult<T, E>The other result to compare.

Returns

boolean

See

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


ok()

ok(): If<Success, Some<T>, None>

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

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

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

Returns

If<Success, Some<T>, None>

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


or()

or<OutputResult>(result: OutputResult): If<Success, Ok<T, any>, OutputResult>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:748

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
OutputResult extends Any

Parameters

ParameterTypeDescription
resultOutputResultThe result to check.

Returns

If<Success, Ok<T, any>, OutputResult>

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


orElse()

orElse<OutputResult>(cb: (error: E) => OutputResult): If<Success, Ok<T, any>, OutputResult>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:771

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
OutputResult extends Any

Parameters

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

Returns

If<Success, Ok<T, any>, OutputResult>

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


transpose()

transpose<InnerValue>(this: Result<Option<InnerValue>, E, Success>): If<Success, Option<Ok<InnerValue, any>>, Some<Err<E, any>>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:845

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

Type Parameters

Type Parameter
InnerValue

Parameters

ParameterType
thisResult<Option<InnerValue>, E, Success>

Returns

If<Success, Option<Ok<InnerValue, any>>, Some<Err<E, any>>>

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


unwrap()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:543

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

If<Success, T, never>

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


unwrapErr()

unwrapErr(): If<Success, never, E>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:574

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

If<Success, never, E>

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


unwrapOr()

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

Defined in: projects/utilities/packages/result/src/lib/Result.ts:604

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
OutputValue

Parameters

ParameterTypeDescription
defaultValueOutputValueThe default value.

Returns

If<Success, T, OutputValue>

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


unwrapOrElse()

unwrapOrElse<OutputValue>(op: (error: E) => OutputValue): If<Success, T, OutputValue>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:627

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

Type Parameters

Type Parameter
OutputValue

Parameters

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

Returns

If<Success, T, OutputValue>

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


unwrapRaw()

unwrapRaw(): If<Success, T, never>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:655

Returns the contained Ok value.

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

Returns

If<Success, T, never>

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

[hasInstance]()

static [hasInstance](instance: unknown): boolean

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1035

Checks if the instance object is an instance of Result, or if it is a Result-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 Result.

Example

import { Result } from '@sapphire/result';
const { ok } = require('@sapphire/result');

ok(2) instanceof Result; // true

all()

static all<Entries>(this: void, results: Entries): Result<UnwrapOkArray<Entries>, UnwrapErrArray<Entries>[number]>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1117

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

Result<UnwrapOkArray<Entries>, UnwrapErrArray<Entries>[number]>

A new Result.


any()

static any<Entries>(this: void, results: Entries): Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1138

Returns the first encountered Ok, or an Err that is the combination of all collected error values.

Type Parameters

Type Parameter
Entries extends readonly Any[]

Parameters

ParameterTypeDescription
thisvoid-
resultsEntriesAn array of Results.

Returns

Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>

A new Result.


err()

static err<E, T>(this: void, value?: E): Err<E, T>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1013

Type Parameters

Type ParameterDefault type
Eundefined
Tany

Parameters

ParameterType
thisvoid
value?E

Returns

Err<E, T>


from()

static from<T, E>(this: void, op: ResultResolvable<T, E, boolean> | () => ResultResolvable<T, E, boolean>): Result<T, E>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1084

Creates a Result out of a callback.

Type Parameters

Type ParameterDefault type
T-
Eunknown

Parameters

ParameterType
thisvoid
opResultResolvable<T, E, boolean> | () => ResultResolvable<T, E, boolean>

Returns

Result<T, E>

Typeparam

T The result's type.

Typeparam

E The error's type.


fromAsync()

static fromAsync<T, E>(this: void, op: Awaitable<ResultResolvable<T, E, boolean>> | () => Awaitable<ResultResolvable<T, E, boolean>>): Promise<Result<T, E>>

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1098

Creates a Result out of a promise or async callback.

Type Parameters

Type ParameterDefault type
T-
Eunknown

Parameters

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

Returns

Promise<Result<T, E>>

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/Result.ts:1055

Parameters

ParameterTypeDescription
instanceunknownThe instance to check.

Returns

instance is Any

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

Deprecated

Use Result.isResult instead.

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

Example

import { Result } from '@sapphire/result';
const { ok } = require('@sapphire/result');

Result.isResult(ok(2)); // true

isResult()

static isResult(instance: unknown): instance is Any

Defined in: projects/utilities/packages/result/src/lib/Result.ts:1073

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

Parameters

ParameterTypeDescription
instanceunknownThe instance to check.

Returns

instance is Any

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

Example

import { Result } from '@sapphire/result';
const { ok } = require('@sapphire/result');

Result.isResult(ok(2)); // true

ok()

static ok<T, E>(this: void, value?: T): Ok<T, E>

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

Type Parameters

Type ParameterDefault type
Tundefined
Eany

Parameters

ParameterType
thisvoid
value?T

Returns

Ok<T, E>