Calls op if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on Result values.
const ok = Ok(25);
const sq = (x: number) => Ok(x * x);
// 25 * 25 => 625 + 5 => 630
const result = ok.andThen(sq).andThen((x) => Ok(x + 5));
expect(result.unwrap()).toEqual(630);
Returns the contained Ok
value, consuming the self
value.
Panics if the value is an Err
, with a panic message including the passed message, and the content of the Err
.
expect(Ok("ok").expect("Testing expect")).toEqual("ok");
try {
Err("fail result").expect("Testing expect")
} catch (e: unknown) {
expect((e as Error).message).toEqual("Testing expect");
}
Returns the contained Err value, consuming the self value.
Panics if the value is an Ok, with a panic message including the passed message, and the content of the Ok.
Returns true
if the result is Err
.
Returns true
if the result is Ok
.
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value,
leaving an Err
value untouched.
This function can be used to compose the results of two functions.
const x: Result<number, string> = Err("5");
expect(x.map((item) => item * 5)).toEqual(Err("5"));
const y: Result<number, string> = Ok(5);
expect(y.map((item) => item * 5)).toEqual(Ok(25));
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.
const stringify = (x: number) => `error code: ${x}`;
const x: Result<number, number> = Ok(2);
expect(x.mapErr(stringify)).toEqual(Ok(2));
const y: Result<number, number> = Err(13);
expect(y.mapErr(stringify)).toEqual(Err("error code: 13"));
Applies a function to the contained value (if Ok
), or returns the provided default (if Err
).
Arguments passed to mapOr
are eagerly evaluated;
if you are passing the result of a function call,
it is recommended to use map_or_else
, which is lazily evaluated.
const x = new Ok("foo");
expect(x.mapOr(42 as number, (v) => v.length)).toEqual(3);
const y = new Err("bar");
expect(y.mapOr(42 as number, (v) => v.length)).toEqual(42);
Maps a Result<T, E>
to U
by applying a function to a contained Ok
value,
or a fallback function to a contained Err
value.
This function can be used to unpack a successful result while handling an error.
const x: Result<string, string> = new Ok("fOo");
expect(
x.mapOrElse(
(err) => err.toLowerCase(),
(v) => v.toUpperCase(),
),
).toEqual("FOO");
const y: Result<string, string> = Err("BaR");
expect(
y.mapOrElse(
(err) => err.toLowerCase(),
(v) => v.toUpperCase(),
),
).toEqual("bar");
Pattern match to retrieve the value
return type of the Ok
branch
return type of the Err
branch
expect(Ok("ok").match({
ok: some => some.length,
err: () => "error",
})).toEqual(2);
expect(Err("error").match({
ok: _ => "ok",
err: _ => "Something bad wrong",
})).toEqual("Something bad wrong")
expect(Err({ code: 404 }).match({ err: err => err.code })).toEqual(404);
expect(Ok("nice").match({ err: _ => "not nice" })).toBeNull();
Returns a string representation of an object.
Transposes a Result of an Option into an Option of a Result.
Ok(None)
will be mapped to None. Ok(Some(_))
and Err(_)
will be mapped to Some(Ok(_))
and Some(Err(_))
.
Panics if the value is an Ok
where self is not an Option
, with a panic message provided by the Ok
's value.
const x: Result<Option<number>, string> = Ok(Some(5));
const y: Option<Result<number, string>> = Some(Ok(5));
expect(x.transpose()).toEqual(y);
Returns the contained Ok
value, consuming the self
value.
Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the Err
case explicitly, or call unwrap_or
, unwrap_or_else
, or unwrap_or_default
.
Panics if the value is an Err
, with a panic message provided by the Err
's value.
expect(Ok(5).unwrap()).toEqual(5);
expect(Ok([1, 3, 4]).unwrap()).toEqual([1, 3, 4]);
expect(
Err({
msg: "Random text",
code: 15,
}).unwrap,
).toThrow(Error);
Returns the contained Err
value, consuming the self
value.
Panics if the value is an Ok, with a custom panic message provided by the Ok's value.
expect(Ok(5).unwrapErr).toThrow(Error);
expect(
Err({
msg: "Random text",
code: 15,
}).unwrapErr(),
).toEqual({
msg: "Random text",
code: 15,
});
Returns the contained Ok
value or a provided default.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrap_or_else
, which is lazily evaluated.
expect(
Ok({
test: true,
}).unwrapOr({ test: false }),
).toEqual({
test: true,
});
expect(Err(5).unwrapOr({ test: false })).toEqual({
test: false,
});
Returns the contained Ok
value or computes it from a closure.
expect(Ok("OK").unwrapOrElse(() => "OK")).toEqual("OK");
expect(Err("Error").unwrapOrElse(() => "Else")).toEqual("Else");
Generated using TypeDoc
Error handling with the Result type.
Result<T, E>
is the type used for returning and propagating errors.Ok(T)
, representing success and containing a value, andErr(E)
, representing error and containing an error value.