The type of the success value.
The type of the failure value.
Basic usage:
const x = new Ok(2);
const y = new Err("late error");
expect(x.and(y)).toEqual(new Err("late error"));
const x = new Err("early error");
const y = new Ok("foo");
expect(x.and(y)).toEqual(new Err("early error"));
const x = new Err("not a 2");
const y = new Err("late error");
expect(x.and(y)).toEqual(new Err("not a 2"));
const x = new Ok(2);
const y = new Ok("different result type");
expect(x.and(y)).toEqual(new Ok("different result type"));
This function can be used for control flow based on Result
values.
Basic usage:
const sq = (x: number) => new Ok(x * x);
const err = (x: number) => new Err(x);
expect(new Ok(2).andThen(sq).andThen(sq)).toEqual(new Ok(16));
expect(new Ok(2).andThen(sq).andThen(err)).toEqual(new Err(4));
expect(new Ok(2).andThen(err).andThen(sq)).toEqual(new Err(2));
expect(new Err(3).andThen(sq).andThen(sq)).toEqual(new Err(3));
Returns the contained Ok value.
Return the contained Err value.
Maps a Result<T, E>
to a 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.
Print the number on each line of a string multiplied by two.
const lines = "1\n2\n3\n4\n";
const parse = (str: string): Result<number, typeof NaN> => {
const num = parseInt(str, 10);
return isNaN(num) ? new Err(num) : new Ok(num);
};
for (const num of lines.split("\n")) {
const result = parse(num).map(n => n * 2);
if (result.isOk()) console.log(result.unwrap())
}
Maps a Result<T, E>
to a 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.
Basic usage:
const stringify = (x: number) => `error code: ${x}`;
const x = new Ok(2);
expect(x.mapErr(stringify)).toEqual(new Ok(2));
const y = new Err(13);
expect(y.mapErr(stringify)).toEqual(new Err("error code: 13"));
Returns the provided default value if the result is Err, otherwise
applies the function f
to the Ok value and returns the result.
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.
const x = new Ok("foo");
expect(x.mapOr(42, str => str.length)).toBe(3);
const y = new Err("bar");
expect(y.mapOr(42, str => str.length)).toBe(42);
Maps a Result<T, E>
to U
by applying fallback function defaultFunc
to
a contained Err value, or function f
to a contained Ok value.
This function can be used to unpack a successful result while handling an error.
Basic usage:
const k = 21;
const x = new Ok("foo");
expect(x.mapOrElse(e => k * 2, str => str.length)).toBe(3);
const y = new Err("bar");
expect(y.mapOrElse(e => k * 2, str => str.length)).toBe(42);
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.
Basic usage:
const x = new Ok(2);
const y = new Err("late error");
expect(x.or(y)).toEqual(new Ok(2));
const x = new Err("early error");
const y = new Ok(2);
expect(x.or(y)).toEqual(new Ok(2));
const x = new Err("not a 2");
const y = new Err("late error");
expect(x.or(y)).toEqual(new Err("late error"));
const x = new Ok(2);
const y = new Ok("100");
expect(x.or(y)).toEqual(new Ok(2));
This function can be used for control flow based on result values.
Basic usage:
const sq = (x: number) => new Ok(x * x);
const err = (x: number) => new Err(x);
expect(new Ok(2).orElse(sq).orElse(sq)).toEqual(new Ok(2));
expect(new Ok(2).orElse(err).orElse(sq)).toEqual(new Ok(2));
expect(new Err(3).orElse(sq).orElse(err)).toEqual(new Ok(9));
expect(new Err(3).orElse(err).orElse(err)).toEqual(new Err(3));
Returns the contained Ok value.
Because this function may throw, its use is generally discouraged. Instead, handle the Err case explicitly, or call unwrapOr or unwrapOrElse.
Throws if the value is an Err, with an error message provided by the Err's value.
Basic usage:
const x = new Ok(2);
expect(x.unwrap()).toBe(2);
const x = new Err("emergency failure");
x.unwrap(); // throws an Error with `emergency failure`
Return the contained Err value.
Return the contained Ok value or a 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.
Basic usage:
const defaultValue = 2;
const x = new Ok(9);
expect(x.unwrapOr(defaultValue)).toBe(9);
const x = new Err("error");
expect(x.unwrapOr(defaultValue)).toBe(defaultValue);
Generated using TypeDoc
Result is an abstract class that represents either success Ok or failure Err. See the module documentation for details.