Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Result<T, E>

Result is an abstract class that represents either success Ok or failure Err. See the module documentation for details.

Type parameters

  • T

    The type of the success value.

  • E

    The type of the failure value.

Hierarchy

Index

Constructors

constructor

  • new Result<T, E>(): Result<T, E>
  • Type parameters

    • T

    • E

    Returns Result<T, E>

Methods

Abstract and

  • Returns res if the result is Ok, otherwise returns the Err value of this.

    Examples

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

    Type parameters

    • U

    Parameters

    Returns Result<U, E>

Abstract andThen

  • Calls f if the result is Ok, otherwise returns the Err value of this.

    This function can be used for control flow based on Result values.

    Examples

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

    Type parameters

    • U

    Parameters

    • f: (value: T) => Result<U, E>
        • Parameters

          • value: T

          Returns Result<U, E>

    Returns Result<U, E>

Abstract expect

  • expect(message: string): T
  • Returns the contained Ok value.

    Throws

    Throws if the value is an Err, with an error message including the passed message, and the content of the Err.

    Examples

    Basic usage:

    const x = new Err("emergency failure");
    x.expect("Testing expect"); // throws an error with `Testing expect: emergency failure`

    Parameters

    • message: string

    Returns T

Abstract expectErr

  • expectErr(message: string): E
  • Return the contained Err value.

    Throws

    Throws if the value is an Ok, with an error message including the passed message, and the content of the Ok.

    Examples

    Basic usage:

    const x = new Ok(10);
    x.expectErr("Testing expectErr"); // throws an error with `Testing expectErr: 10`

    Parameters

    • message: string

    Returns E

Abstract isErr

  • isErr(): this is Err<E>

Abstract isOk

  • isOk(): this is Ok<T>
  • Returns true if the result is Ok, false otherwise.

    Returns this is Ok<T>

Abstract map

  • map<U>(f: (value: T) => U): Result<U, E>
  • 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.

    Examples

    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())
    }

    Type parameters

    • U

    Parameters

    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

Abstract mapErr

  • mapErr<F>(f: (e: E) => F): Result<T, F>
  • 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.

    Examples

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

    Type parameters

    • F

    Parameters

    • f: (e: E) => F
        • (e: E): F
        • Parameters

          • e: E

          Returns F

    Returns Result<T, F>

Abstract mapOr

  • mapOr<U>(defaultValue: U, f: (value: T) => U): U
  • 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.

    Examples

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

    Type parameters

    • U

    Parameters

    • defaultValue: U
    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

Abstract mapOrElse

  • mapOrElse<U>(defaultFunc: (e: E) => U, f: (value: T) => U): U
  • 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.

    Examples

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

    Type parameters

    • U

    Parameters

    • defaultFunc: (e: E) => U
        • (e: E): U
        • Parameters

          • e: E

          Returns U

    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

Abstract or

  • Returns res if the result is Err, otherwise returns the Ok value of this.

    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.

    Examples

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

    Type parameters

    • F

    Parameters

    Returns Result<T, F>

Abstract orElse

  • Calls f if the result is Err, otherwise returns the Ok value of this.

    This function can be used for control flow based on result values.

    Examples

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

    Type parameters

    • F

    Parameters

    Returns Result<T, F>

Abstract unwrap

  • unwrap(): T
  • 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

    Throws if the value is an Err, with an error message provided by the Err's value.

    Examples

    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`

    Returns T

Abstract unwrapErr

  • unwrapErr(): E
  • Return the contained Err value.

    Throws

    Throws if the value is an Ok, with a custom message provided by the Ok's value.

    Examples

    const x = new Ok(2);
    x.unwrapErr(); // throws an error with `2`

    const x = new Err("emergency failure");
    expect(x.unwrapErr()).toBe("emergency failure");

    Returns E

Abstract unwrapOr

  • unwrapOr(defaultValue: T): T
  • 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.

    Examples

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

    Parameters

    • defaultValue: T

    Returns T

Abstract unwrapOrElse

  • unwrapOrElse(f: (err: E) => T): T
  • Returns the contained Ok value or computes it from a provided function.

    Examples

    Basic usage:

    const count = (x: string) => x.length;

    expect(new Ok(2).unwrapOrElse(count)).toBe(2);
    expect(new Err("foo").unwrapOrElse(count)).toBe(3);

    Parameters

    • f: (err: E) => T
        • (err: E): T
        • Parameters

          • err: E

          Returns T

    Returns T

Generated using TypeDoc