Resultant
@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 |
---|
|
|
Methods
and()
and<U>(x): Result<U, E>;
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 Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
Returns
Result
<U
, E
>
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): Result<U, E>;
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 Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Result
<U
, E
>
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
Parameter | Type |
---|---|
|
|
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 |
---|
|
|
Parameters
Parameter | Type |
---|---|
|
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, unknown>[]], 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 |
---|
|
Parameters
Parameter | Type |
---|---|
... |
|
Returns
Result
<[T
, ...OkValues<U, unknown>[]
], 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>
copy()
copy(): Result<T, E>;
Defined in: packages/std/src/result/interface.ts:237
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:259
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
Parameter | Type |
---|---|
|
|
Returns
Option
<E
>
Notes
- Extracts the error from CheckedError if it’s an ExpectedError; returns None for UnexpectedError.
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:277
Retrieves the value if this result is an Ok, or throws a ResultError with an optional message if it’s an Err.
Parameters
Parameter | Type |
---|---|
|
|
|
|
Returns
T
Throws
- ResultError if this result is an Err
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:296
Retrieves the error if this result is an Err, or throws a ResultError with an optional message if it’s an Ok.
Parameters
Parameter | Type |
---|---|
|
|
|
|
Returns
CheckedError
<E
>
Throws
- ResultError if this result is an Ok
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, F>;
Defined in: packages/std/src/result/interface.ts:313
Flattens a nested result (Result<Result<T, E>, E>
) into a single result
(Result<T, E>
).
Type Parameters
Type Parameter |
---|
|
|
Parameters
Parameter | Type |
---|---|
|
Returns
Result
<U
, F
>
Example
const x: Result<Result<Result<number, string>, string>, string> = ok(ok(ok(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:336
Calls f
with the value if this result is an Ok, then returns
a copy of this result.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Result
<T
, E
>
Notes
- Returns a new Result instance, not the original reference.
- If
f
throws or returns aPromise
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:360
Calls f
with the error if this result is an Err, then returns
a copy of this result.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Result
<T
, E
>
Notes
- Returns a new Result instance, not the original reference.
- If
f
throws or returns aPromise
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(0);
expect(y.inspect((n) => (sideEffect = n))).toStrictEqual(err("failure"));
expect(
y.inspect((_) => {
throw new Error();
}),
).toStrictEqual(err("failure"));
expect(sideEffect).toBe(2);
isErr()
isErr(): this is Err<T, E>;
Defined in: packages/std/src/result/interface.ts:375
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): this is Resultant<T, E> & { error: CheckedError<E> } & boolean;
Defined in: packages/std/src/result/interface.ts:394
Returns true
if the result is Err and f
returns true
for the contained error.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
this is Resultant<T, E> & { error: CheckedError<E> } & 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:409
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): this is Resultant<T, E> & { value: T } & boolean;
Defined in: packages/std/src/result/interface.ts:428
Returns true
if the result is Ok and f
returns true
for the contained value.
Parameters
Parameter | Type |
---|---|
| ( |
Returns
this is Resultant<T, E> & { value: T } & 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);
iter()
iter(): IterableIterator<T, T, void>;
Defined in: packages/std/src/result/interface.ts:455
Returns an iterator over this result’s value, yielding it if Ok or nothing if Err.
Returns
IterableIterator
<T
, T
, void
>
Notes
- Yields exactly one item for Ok, or zero items for Err.
- Compatible with
for...of
loops and spread operators. - Ignores the error value in Err cases, focusing only on the success case.
Example
const x = ok<number, string>(42);
const y = err<number, string>("failure");
const iterX = x.iter();
expect(iterX.next()).toEqual({ value: 42, done: false });
expect(iterX.next()).toEqual({ done: true });
const iterY = y.iter();
expect(iterY.next()).toEqual({ done: true });
expect([...x.iter()]).toEqual([42]);
expect([...y.iter()]).toEqual([]);
map()
map<U>(f): Result<U, E>;
Defined in: packages/std/src/result/interface.ts:475
Transforms this result by applying f
to the value if it’s an Ok,
or preserves the Err unchanged.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Result
<U
, E
>
Notes
- If
f
throws, returns an Err with an UnexpectedError containing the original error.
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:499
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 |
---|
|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
Result
<U
, F
>
Notes
- If
f
throws an Err with an UnexpectedError is returned.
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:531
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 |
---|
|
|
Parameters
Parameter | Type |
---|---|
| ( |
Returns
PendingResult
<Awaited
<U
>, Awaited
<F
>>
Notes
- If
f
returns aPromise
that rejects, the resulting PendingResult resolves to an Err with an UnexpectedError.
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:555
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 |
---|
|
Parameters
Parameter | Type |
---|---|
| ( |
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:573
Returns f
applied to the value if Ok, otherwise returns def
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
|
|
| ( |
Returns
U
Notes
- Default: If
f
throws, returnsdef
.
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:602
Returns f
applied to the contained value if Ok, otherwise
returns the result of mkDef
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
| () => |
| ( |
Returns
U
Throws
- ResultError if
mkDef
is called and throws an exception, with the original error set as ResultError.reason.
Notes
- If
f
throws, the error is silently ignored, and the result ofmkDef
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): U | F;
Defined in: packages/std/src/result/interface.ts:631
Matches this result, returning f
applied to the value if Ok,
or g
applied to the CheckedError if Err.
Type Parameters
Type Parameter | Default type |
---|---|
| ‐ |
|
|
Parameters
Parameter | Type |
---|---|
|
|
| ( |
| ( |
Returns
U
| F
Throws
- ResultError if
f
org
throws an exception, with the original error set as ResultError.reason.
Notes
- If
f
org
return aPromise
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:652
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:668
Returns the current result if it is Ok, otherwise returns x
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
|
|
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:688
Returns the current result if Ok, otherwise returns the result of f
.
Type Parameters
Type Parameter |
---|
|
Parameters
Parameter | Type |
---|---|
| () => |
Returns
Result
<T
, F
>
Notes
- If
f
throws, returns an Err with an UnexpectedError containing the original error.
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:711
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
Parameter | Type |
---|---|
| ( |
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:733
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 anAwaited
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:758
Converts this result to a PendingResult using a deep clone of its current state.
Parameters
Parameter | Type |
---|---|
|
Returns
PendingResult
<Awaited
<T
>, Awaited
<E
>>
Notes
- Useful for transposing a result with a
PromiseLike
value to a PendingResult with anAwaited
value, preserving independence from the original data. - If inner
T
orE
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:775
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:795
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 |
---|
|
|
Parameters
Parameter | Type |
---|---|
|
Returns
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:819
Extracts this result’s state, returning a tuple with a success flag, error, and value.
Inspired by the Try Operator proposal.
Parameters
Parameter | Type |
---|---|
|
|
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:841
Retrieves the value if this result is an Ok, or throws a ResultError if it’s an Err.
Parameters
Parameter | Type |
---|---|
|
|
Returns
T
Throws
- ResultError if this result is an Err
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:859
Retrieves the CheckedError if this result is an Err, or throws a ResultError if it’s an Ok.
Parameters
Parameter | Type |
---|---|
|
|
Returns
CheckedError
<E
>
Throws
- ResultError if this result is an Ok
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:873
Returns the contained value if Ok, or def
if Err.
Parameters
Parameter | Type |
---|---|
|
|
|
|
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);
unwrapOrElse()
unwrapOrElse(this, mkDef): T;
Defined in: packages/std/src/result/interface.ts:893
Returns the contained value if Ok, or the result of mkDef
if Err.
Parameters
Parameter | Type |
---|---|
|
|
| () => |
Returns
T
Throws
- ResultError if
mkDef
throws, with the original error set as ResultError.reason.
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);