Skip to main content

Resultant

@ts-rust/std


@ts-rust/std / Result / Resultant

Interface: Resultant<T, E>

Defined in: packages/std/src/result/interface.ts:126

Interface representing the resultant state of an operation, either a success (Ok\<T\>) or an error (Err\<E\>).

Inspired by Rust’s Result, it provides a type-safe alternative to exceptions for handling success or failure outcomes.

Type Parameters​

Type Parameter

T

E

Methods​

[iterator]()​

iterator: Generator<Err<never, E>, T>;

Defined in: packages/std/src/result/interface.ts:472

Makes this result usable with yield* inside a generator passed to runGenerator or runAsyncGenerator, emulating Rust's ? operator.

An Ok resumes the generator with the contained value. An Err is yielded to the runner, which returns that error and does not resume.

This is not a value iterator: for...of / spread do not yield T.

Returns​

Generator<Err<never, E>, T>

Example​

const result = runGenerator(function* () {
const n = yield* ok<number, string>(1);
return ok(n + 1);
});

expect(result).toStrictEqual(ok(2));

and()​

and<U, F>(x): Result<U, InferType<E, F>>;

Defined in: packages/std/src/result/interface.ts:142

Returns x if this result is Ok, otherwise returns the Err value of self.

Type Parameters​

Type ParameterDefault type

U

‐

F

E

Parameters​

ParameterType

x

Result<U, F>

Returns​

Result<U, InferType<E, F>>

Example​

const x = ok<number, string>(1);
const y = ok<number, string>(2);
const z = err<number, string>("failure");

expect(x.and(y)).toStrictEqual(ok(2));
expect(x.and(z)).toStrictEqual(err("failure"));
expect(z.and(x)).toStrictEqual(err("failure"));

andThen()​

andThen<U, F>(f): Result<U, InferType<E, F>>;

Defined in: packages/std/src/result/interface.ts:157

Applies f to the value if this result is Ok and returns its result, otherwise returns the Err value of self.

Type Parameters​

Type ParameterDefault type

U

‐

F

E

Parameters​

ParameterType

f

(x) => Result<U, F>

Returns​

Result<U, InferType<E, F>>

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.andThen((n) => ok(n * 2))).toStrictEqual(ok(4));
expect(y.andThen((n) => ok(n * 2))).toStrictEqual(err("failure"));

check()​

check(this): readonly [false, CheckedError<E>];

Defined in: packages/std/src/result/interface.ts:177

Inspects this result’s state, returning a tuple indicating success and either the value or error.

Parameters​

ParameterType

this

SettledResult<T, E>

Returns​

readonly [false, CheckedError<E>]

Notes​

  • Returns [true, T] if this is an Ok, or [false, CheckedError<E>] if this is an Err.
  • Never throws, providing a safe way to access the result’s state without unwrapping.

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(x.check()).toEqual([true, 42]);
expect(y.check()).toEqual([
false,
expect.objectContaining({ expected: "failure" }),
]);

clone()​

clone<U, F>(this): Result<U, F>;

Defined in: packages/std/src/result/interface.ts:199

Returns a clone of the Result.

Only available on Results with Cloneable value and error.

Type Parameters​

Type Parameter

U

F

Parameters​

ParameterType

this

Result<Cloneable<U>, Cloneable<F>>

Returns​

Result<U, F>

Example​

const x = ok(1);
const y = ok({ a: 1, clone: () => ({ a: 0 }) });

expect(x.clone()).toStrictEqual(ok(1));
expect(x.clone()).not.toBe(x); // Different reference
expect(x.clone().unwrap()).toBe(1);
expect(y.clone()).toStrictEqual(ok({ a: 0 }));

combine()​

combine<U>(...results): Result<[T, ...OkValues<U>[]], E>;

Defined in: packages/std/src/result/interface.ts:220

Combines this Result with other Result instances into a single Result containing a tuple of values.

The combine method takes an arbitrary number of Result instances, all sharing the same Err type. If all Result instances (including this one) are Ok, it returns a Result with a tuple of their Ok values in the order provided. If any Result is Err, it returns that Err. The resulting tuple includes the value of this Result as the first element, followed by the values from the provided Result instances.

Type Parameters​

Type Parameter

U extends Result<unknown, E>[]

Parameters​

ParameterType

...results

U

Returns​

Result<[T, ...OkValues<U>[]], E>

Example​

const a = ok<Promise<number>, string>(Promise.resolve(1));
const b = ok<string, string>("hi");
const c = err<Date, string>("no");
const d = a.combine(b, c); // Result<[Promise<number>, string, Date], string>

contains()​

contains(value): boolean;

Defined in: packages/std/src/result/interface.ts:238

Returns true if the result is Ok and the contained value equals value using the === operator.

Parameters​

ParameterType

value

T

Returns​

boolean

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

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

copy()​

copy(): Result<T, E>;

Defined in: packages/std/src/result/interface.ts:253

Returns a shallow copy of the Result.

Returns​

Result<T, E>

Example​

const value = { a: 1 };
const x = ok<{ a: number }, string>(value);

expect(x.copy()).toStrictEqual(ok({ a: 1 }));
expect(x.copy()).not.toBe(x); // Different result reference
expect(x.copy().unwrap()).toBe(value); // Same value reference

err()​

err(this): Option<E>;

Defined in: packages/std/src/result/interface.ts:275

Converts this Result to an Option\<E\> containing the error, if present.

Returns Some with the error value if this is an Err, or None if this is an Ok.

Parameters​

ParameterType

this

SettledResult<T, E>

Returns​

Option<E>

Notes​

Example​

const x = ok<number, string>(1);
const y = err<number, string>("failure");

expect(x.err()).toStrictEqual(none());
expect(y.err()).toStrictEqual(some("failure"));

expect()​

expect(this, msg?): T;

Defined in: packages/std/src/result/interface.ts:293

Retrieves the value if this result is an Ok, or throws a ResultError with an optional message if it’s an Err.

Parameters​

ParameterType

this

SettledResult<T, E>

msg?

string

Returns​

T

Throws​

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(x.expect("Failed!")).toBe(42);
expect(() => y.expect("Failed!")).toThrow(ResultError);

expectErr()​

expectErr(this, msg?): CheckedError<E>;

Defined in: packages/std/src/result/interface.ts:312

Retrieves the error if this result is an Err, or throws a ResultError with an optional message if it’s an Ok.

Parameters​

ParameterType

this

SettledResult<T, E>

msg?

string

Returns​

CheckedError<E>

Throws​

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(() => x.expectErr("Failed!")).toThrow(ResultError);
expect(isCheckedError(y.expectErr("Failed!"))).toBe(true);
expect(y.expectErr("Failed!").expected).toBe("failure");

flatten()​

flatten<U, F>(this): Result<U, InferType<E, F>>;

Defined in: packages/std/src/result/interface.ts:334

Flattens a nested result (Result<Result<T, E>, E>) into a single result (Result<T, E>). Peels one layer; chain flatten for deeper nesting.

Outer and inner error types are merged. An unannotated outer unknown error (from ok()) does not swallow a concrete inner error type.

Type Parameters​

Type Parameter

U

F

Parameters​

ParameterType

this

Result<Result<U, F>, E>

Returns​

Result<U, InferType<E, F>>

Example​

const x: Result<Result<Result<number, string>, string>, string> = ok(
ok(ok<number, string>(6)),
);
const y: Result<Result<number, string>, string> = x.flatten();
const z: Result<Result<number, string>, string> = err("oops");

expect(x.flatten()).toStrictEqual(ok(ok(6)));
expect(y.flatten()).toStrictEqual(ok(6));
expect(z.flatten().expected).toBe("oops");

inspect()​

inspect(f): Result<T, E>;

Defined in: packages/std/src/result/interface.ts:357

Calls f with the value if this result is an Ok, then returns a copy of this result.

Parameters​

ParameterType

f

(x) => unknown

Returns​

Result<T, E>

Notes​

  • Returns a new Result instance, not the original reference.
  • If f throws or returns a Promise that rejects, the error is ignored.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");
let sideEffect = 0;

expect(x.inspect((n) => (sideEffect = n))).toStrictEqual(ok(2));
expect(
x.inspect((_) => {
throw new Error();
}),
).toStrictEqual(ok(2));
expect(sideEffect).toBe(2);
expect(y.inspect((n) => (sideEffect = n))).toStrictEqual(err("failure"));
expect(sideEffect).toBe(2); // Unchanged

inspectErr()​

inspectErr(f): Result<T, E>;

Defined in: packages/std/src/result/interface.ts:381

Calls f with the error if this result is an Err, then returns a copy of this result.

Parameters​

ParameterType

f

(x) => unknown

Returns​

Result<T, E>

Notes​

  • Returns a new Result instance, not the original reference.
  • If f throws or returns a Promise that rejects, the error is ignored.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");
let sideEffect: CheckedError<string> | null = null;

expect(x.inspectErr((e) => (sideEffect = e))).toStrictEqual(ok(2));
expect(
x.inspectErr((_) => {
throw new Error();
}),
).toStrictEqual(ok(2));
expect(sideEffect).toBeNull();
expect(y.inspectErr((e) => (sideEffect = e))).toStrictEqual(err("failure"));
expect(
y.inspectErr((_) => {
throw new Error();
}),
).toStrictEqual(err("failure"));
expect(isCheckedError(sideEffect)).toBe(true);

isErr()​

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

Defined in: packages/std/src/result/interface.ts:396

Checks if this result is an Err, narrowing its type to Err if true.

Returns​

this is Err<T, E>

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.isErr()).toBe(false);
expect(y.isErr()).toBe(true);

isErrAnd()​

isErrAnd(f): boolean;

Defined in: packages/std/src/result/interface.ts:415

Returns true if the result is Err and f returns true for the contained error.

Parameters​

ParameterType

f

(x) => boolean

Returns​

boolean

Notes​

  • Default: If f throws, false is returned.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.isErrAnd((e) => e.expected === "failure")).toBe(false);
expect(y.isErrAnd((e) => e.expected === "failure")).toBe(true);
expect(y.isErrAnd((e) => Boolean(e.unexpected))).toBe(false);

isOk()​

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

Defined in: packages/std/src/result/interface.ts:430

Checks if this result is an Ok, narrowing its type to Ok if true.

Returns​

this is Ok<T, E>

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.isOk()).toBe(true);
expect(y.isOk()).toBe(false);

isOkAnd()​

isOkAnd(f): boolean;

Defined in: packages/std/src/result/interface.ts:449

Returns true if the result is Ok and f returns true for the contained value.

Parameters​

ParameterType

f

(x) => boolean

Returns​

boolean

Notes​

  • Default: If f throws, false is returned.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.isOkAnd((n) => n > 0)).toBe(true);
expect(x.isOkAnd((n) => n < 0)).toBe(false);
expect(y.isOkAnd((_) => true)).toBe(false);

map()​

map<U>(f): Result<U, E>;

Defined in: packages/std/src/result/interface.ts:492

Transforms this result by applying f to the value if it’s an Ok, or preserves the Err unchanged.

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

f

(x) => Awaited<U>

Returns​

Result<U, E>

Notes​

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.map((n) => n * 2)).toStrictEqual(ok(4));
expect(
x
.map(() => {
throw new Error("boom");
})
.unwrapErr().unexpected,
).toBeDefined();
expect(y.map((n) => n * 2)).toStrictEqual(err("failure"));

mapAll()​

Call Signature​

mapAll<U, F>(f): Result<U, F>;

Defined in: packages/std/src/result/interface.ts:516

Maps this result by applying a callback to its full state, executing the callback for both Ok and Err, returning a new Result.

Unlike andThen, which only invokes the callback for Ok, this method always calls f, passing the entire Result as its argument.

Type Parameters​
Type Parameter

U

F

Parameters​
ParameterType

f

(x) => Result<U, F>

Returns​

Result<U, F>

Notes​
Example​
const okRes = ok<number, string>(42);
const errRes = err<number, string>("failure");

expect(okRes.mapAll((res) => ok(res.unwrapOr(0) + 1))).toStrictEqual(ok(43));
expect(errRes.mapAll((res) => ok(res.unwrapOr(0) + 1))).toStrictEqual(ok(1));
expect(
okRes.mapAll((res) => (res.isOk() ? ok("success") : err("fail"))),
).toStrictEqual(ok("success"));
expect(
errRes
.mapAll(() => {
throw new Error("boom");
})
.unwrapErr().unexpected,
).toBeDefined();

Call Signature​

mapAll<U, F>(f): PendingResult<Awaited<U>, Awaited<F>>;

Defined in: packages/std/src/result/interface.ts:548

Maps this result by applying a callback to its full state, executing the callback for both Ok and Err, returning a new PendingResult.

Unlike andThen, which only invokes the callback for Ok, this method always calls f, passing the entire Result as its argument.

Type Parameters​
Type Parameter

U

F

Parameters​
ParameterType

f

(x) => Promise<Result<U, F>>

Returns​

PendingResult<Awaited<U>, Awaited<F>>

Notes​
Example​
const okRes = ok<number, string>(42);
const errRes = err<number, string>("failure");

const mappedOk = okRes.mapAll((res) =>
Promise.resolve(ok(res.unwrapOr(0) + 1)),
);
expect(await mappedOk).toStrictEqual(ok(43));

const mappedErr = errRes.mapAll((res) =>
Promise.resolve(ok(res.unwrapOr(0) + 1)),
);
expect(await mappedErr).toStrictEqual(ok(1));

const mappedCheck = okRes.mapAll((res) =>
Promise.resolve(res.isOk() ? ok("success") : err("fail")),
);
expect(await mappedCheck).toStrictEqual(ok("success"));

const mappedThrow = errRes.mapAll(() => Promise.reject(new Error("boom")));
expect((await mappedThrow).unwrapErr().unexpected).toBeDefined();

mapErr()​

mapErr<F>(f): Result<T, F>;

Defined in: packages/std/src/result/interface.ts:572

Transforms this result by applying f to the error if it’s an Err with an expected error, or preserves the result unchanged.

Type Parameters​

Type Parameter

F

Parameters​

ParameterType

f

(e) => Awaited<F>

Returns​

Result<T, F>

Notes​

  • If f throws, returns an Err with an UnexpectedError containing the original error.
  • If this is an Err with an UnexpectedError, f is not called, and the original error is preserved.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.mapErr((e) => e.length)).toStrictEqual(ok(2));
expect(y.mapErr((e) => e.length)).toStrictEqual(err(7));
expect(
y
.mapErr(() => {
throw new Error("boom");
})
.unwrapErr().unexpected,
).toBeDefined();

mapOr()​

mapOr<U>(
this,
def,
f): U;

Defined in: packages/std/src/result/interface.ts:590

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

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

this

SettledResult<T, E>

def

Awaited<U>

f

(x) => Awaited<U>

Returns​

U

Notes​

  • Default: If f throws, returns def.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.mapOr(0, (n) => n * 2)).toBe(4);
expect(
x.mapOr(0, () => {
throw new Error("boom");
}),
).toBe(0);
expect(y.mapOr(0, (n) => n * 2)).toBe(0);

mapOrElse()​

mapOrElse<U>(
this,
mkDef,
f): U;

Defined in: packages/std/src/result/interface.ts:619

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

Type Parameters​

Type Parameter

U

Parameters​

ParameterType

this

SettledResult<T, E>

mkDef

() => Awaited<U>

f

(x) => Awaited<U>

Returns​

U

Throws​

Notes​

  • If f throws, the error is silently ignored, and the result of mkDef is returned.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(
x.mapOrElse(
() => 0,
(n) => n * 2,
),
).toBe(4);
expect(
x.mapOrElse(
() => 1,
() => {
throw new Error("boom");
},
),
).toBe(1);
expect(() =>
y.mapOrElse(
() => {
throw new Error("boom");
},
(n) => n * 2,
),
).toThrow(ResultError);
expect(
y.mapOrElse(
() => 0,
(n) => n * 2,
),
).toBe(0);

match()​

match<U, F>(
this,
f,
g): InferType<U, F>;

Defined in: packages/std/src/result/interface.ts:648

Matches this result, returning f applied to the value if Ok, or g applied to the CheckedError if Err.

Type Parameters​

Type ParameterDefault type

U

‐

F

U

Parameters​

ParameterType

this

SettledResult<T, E>

f

(x) => Awaited<U>

g

(e) => Awaited<F>

Returns​

InferType<U, F>

Throws​

Notes​

  • If f or g return a Promise that rejects, the caller is responsible for handling the rejection.

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(
x.match(
(n) => n * 2,
() => 0,
),
).toBe(4);
expect(() =>
x.match(
(_) => {
throw new Error();
},
() => 0,
),
).toThrow(ResultError);
expect(
y.match(
(n) => n * 2,
(e) => e.expected?.length,
),
).toBe(7);
expect(() =>
y.match(
(n) => n * 2,
() => {
throw new Error();
},
),
).toThrow(ResultError);

ok()​

ok(): Option<T>;

Defined in: packages/std/src/result/interface.ts:669

Converts this result to an Option, discarding the error if present.

Maps Ok(v) to Some(v) and Err(e) to None.

Returns​

Option<T>

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.ok()).toStrictEqual(some(2));
expect(y.ok()).toStrictEqual(none());

or()​

or<F>(x): Result<T, F>;

Defined in: packages/std/src/result/interface.ts:686

Returns the current result if it is Ok, otherwise returns a copy of x.

Type Parameters​

Type Parameter

F

Parameters​

ParameterType

x

Result<T, F>

Returns​

Result<T, F>

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.or(ok(3))).toStrictEqual(ok(2));
expect(x.or(err("failure"))).toStrictEqual(ok(2));
expect(y.or(ok(3))).toStrictEqual(ok(3));
expect(y.or(err("another one"))).toStrictEqual(err("another one"));

orElse()​

orElse<F>(f): Result<T, F>;

Defined in: packages/std/src/result/interface.ts:706

Returns the current result if Ok, otherwise returns the result of f.

Type Parameters​

Type Parameter

F

Parameters​

ParameterType

f

() => Result<T, F>

Returns​

Result<T, F>

Notes​

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.orElse(() => ok(3))).toStrictEqual(ok(2));
expect(y.orElse(() => ok(3))).toStrictEqual(ok(3));
expect(
y
.orElse(() => {
throw new Error("boom");
})
.unwrapErr().unexpected,
).toBeDefined();
expect(y.orElse(() => err("another one"))).toStrictEqual(err("another one"));

tap()​

tap(f): Result<T, E>;

Defined in: packages/std/src/result/interface.ts:729

Executes f with a copy of this result, then returns a new copy unchanged.

Useful for side-effects like logging, works with both Ok and Err.

Parameters​

ParameterType

f

(x) => unknown

Returns​

Result<T, E>

Notes​

  • If f throws or rejects, the error is silently ignored.
  • If f returns a promise, the promise is not awaited before returning.

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");
let log = "";

expect(x.tap((res) => (log = res.toString()))).toStrictEqual(ok(42));
expect(log).toBe("Ok { 42 }");
expect(y.tap((res) => (log = res.toString()))).toStrictEqual(err("failure"));
expect(log).toBe("Err { 'failure' }");

toPending()​

toPending(): PendingResult<Awaited<T>, Awaited<E>>;

Defined in: packages/std/src/result/interface.ts:751

Converts this result to a PendingResult using a shallow copy of its current state.

Returns​

PendingResult<Awaited<T>, Awaited<E>>

Notes​

  • Useful for transposing a result with a PromiseLike value to a PendingResult with an Awaited value.

Example​

const value = { a: 1 };
const x = ok<{ a: number }, string>(value);
const pendingX = x.toPending();

expect(isPendingResult(pendingX)).toBe(true);
expect(await pendingX).toStrictEqual(ok({ a: 1 }));
value.a = 2;
expect(await pendingX).toStrictEqual(ok({ a: 2 }));

toPendingCloned()​

toPendingCloned(this): PendingResult<Awaited<T>, Awaited<E>>;

Defined in: packages/std/src/result/interface.ts:776

Converts this result to a PendingResult using a deep clone of its current state.

Parameters​

ParameterType

this

Result<Cloneable<T>, Cloneable<E>>

Returns​

PendingResult<Awaited<T>, Awaited<E>>

Notes​

  • Useful for transposing a result with a PromiseLike value to a PendingResult with an Awaited value, preserving independence from the original data.
  • If inner T or E is a promise-like that rejects, maps to a PendingResult that resolves to Err with UnexpectedError.

Example​

const value = { a: 1, clone: () => ({ a: 0 }) };
const x = ok(value);
const pendingX = x.toPendingCloned();

expect(isPendingResult(pendingX)).toBe(true);
expect((await pendingX).unwrap()).toStrictEqual({ a: 0 });
value.a = 42;
expect((await pendingX).unwrap()).toStrictEqual({ a: 0 });

toString()​

toString(): string;

Defined in: packages/std/src/result/interface.ts:793

Generates a string representation of this result, reflecting its current state.

Returns​

string

Example​

const x = ok<number, string>(2);
const y = err<number, string>("error");

expect(x.toString()).toBe("Ok { 2 }");
expect(y.toString()).toBe("Err { 'error' }");

transpose()​

transpose<U, F>(this): Option<Result<U, F>>;

Defined in: packages/std/src/result/interface.ts:813

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

Maps Ok(None) to None, Ok(Some(_)) to Some(Ok(_)) and Err(_) to Some(Err(_)).

Type Parameters​

Type Parameter

U

F

Parameters​

ParameterType

this

Result<Option<U>, F>

Returns​

Option<Result<U, F>>

Example​

const x = ok<Option<number>, string>(none());
const y = ok<Option<number>, string>(some(2));
const z = err<Option<number>, string>("error");

expect(x.transpose()).toStrictEqual(none());
expect(y.transpose()).toStrictEqual(some(ok(2)));
expect(z.transpose()).toStrictEqual(some(err("error")));

try()​

try(this): readonly [false, CheckedError<E>, undefined];

Defined in: packages/std/src/result/interface.ts:837

Extracts this result’s state, returning a tuple with a success flag, error, and value.

Inspired by the Try Operator proposal.

Parameters​

ParameterType

this

SettledResult<T, E>

Returns​

readonly [false, CheckedError<E>, undefined]

Notes​

  • Returns [true, undefined, T] if this is an Ok, or [false, CheckedError<E>, undefined] if this is an Err.
  • Never throws, offering a safe way to inspect the result’s state with explicit success indication.

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(x.try()).toEqual([true, undefined, 42]);
expect(y.try()).toEqual([
false,
expect.objectContaining({ expected: "failure" }),
undefined,
]);

unwrap()​

unwrap(this): T;

Defined in: packages/std/src/result/interface.ts:859

Retrieves the value if this result is an Ok, or throws a ResultError if it’s an Err.

Parameters​

ParameterType

this

SettledResult<T, E>

Returns​

T

Throws​

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(x.unwrap()).toBe(42);
expect(() => y.unwrap()).toThrow(ResultError);

unwrapErr()​

unwrapErr(this): CheckedError<E>;

Defined in: packages/std/src/result/interface.ts:877

Retrieves the CheckedError if this result is an Err, or throws a ResultError if it’s an Ok.

Parameters​

ParameterType

this

SettledResult<T, E>

Returns​

CheckedError<E>

Throws​

Example​

const x = ok<number, string>(42);
const y = err<number, string>("failure");

expect(() => x.unwrapErr()).toThrow(ResultError);
expect(y.unwrapErr().expected).toBe("failure");

unwrapOr()​

unwrapOr(this, def): T;

Defined in: packages/std/src/result/interface.ts:891

Returns the contained value if Ok, or def if Err.

Parameters​

ParameterType

this

SettledResult<T, E>

def

Awaited<T>

Returns​

T

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

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

unwrapOrDefault()​

unwrapOrDefault(): T | undefined;

Defined in: packages/std/src/result/interface.ts:905

Returns the contained value if Ok, or undefined if Err.

Returns​

T | undefined

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

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

unwrapOrElse()​

unwrapOrElse(this, mkDef): T;

Defined in: packages/std/src/result/interface.ts:925

Returns the contained value if Ok, or the result of mkDef if Err.

Parameters​

ParameterType

this

SettledResult<T, E>

mkDef

() => Awaited<T>

Returns​

T

Throws​

Example​

const x = ok<number, string>(2);
const y = err<number, string>("failure");

expect(x.unwrapOrElse(() => 0)).toBe(2);
expect(y.unwrapOrElse(() => 0)).toBe(0);
expect(() =>
y.unwrapOrElse(() => {
throw new Error("boom");
}),
).toThrow(ResultError);