Options
All
  • Public
  • Public/Protected
  • All
Menu

A type alias for a successful Result.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • new Ok<T>(value: T): Ok<T>

Methods

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

    • E

    Parameters

    Returns Result<U, E>

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

    • E

    Parameters

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

          • value: T

          Returns Result<U, E>

    Returns Result<U, E>

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

expectErr

  • expectErr<E>(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`

    Type parameters

    • E

    Parameters

    • message: string

    Returns E

isErr

  • isErr(): this is Err<never>

isOk

  • isOk(): this is Ok<T>

map

  • map<U, E>(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

    • E

    Parameters

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

          • value: T

          Returns U

    Returns Result<U, E>

mapErr

  • mapErr<F, E>(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

    • E

    Parameters

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

          • e: E

          Returns F

    Returns Result<T, F>

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

mapOrElse

  • mapOrElse<U, E>(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

    • E

    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

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>

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

    • E

    Parameters

    Returns Result<T, F>

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

unwrapErr

  • unwrapErr<E>(): 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");

    Type parameters

    • E

    Returns E

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

unwrapOrElse

  • unwrapOrElse<E>(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);

    Type parameters

    • E

    Parameters

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

          • err: E

          Returns T

    Returns T

Generated using TypeDoc