Interface IResult<A, E>

Methods available on Result objects.

interface IResult<A, E> {
    and<B>(result: Result<B, E>): Result<B, E>;
    ap<B>(f: Result<((a: A) => B), E>): Result<B, E>;
    assertErr(): asserts this is Err<E>;
    assertOk(): asserts this is Ok<A>;
    bichain<B, F>(ok: ((okValue: A) => Result<B, F>), err: ((errValue: E) => Result<B, F>)): Result<B, F>;
    bimap<B, F>(ok: ((okValue: A) => B), err: ((errValue: E) => F)): Result<B, F>;
    chain<B>(f: ((value: A) => Result<B, E>)): Result<B, E>;
    chainErr<F>(f: ((value: E) => Result<A, F>)): Result<A, F>;
    err(): Option<E>;
    getOr(defaultValue: A): A;
    getOrElse(f: (() => A)): A;
    ifErr(onErr: ((value: E) => void)): void;
    ifOk(onOk: ((value: A) => void)): void;
    isAborted(): this is Err<DOMException>;
    isErr(): this is Err<E>;
    isOk(): this is Ok<A>;
    map<B>(f: ((value: A) => B)): Result<B, E>;
    mapErr<F>(f: ((value: E) => F)): Result<A, F>;
    match<B>(onOk: ((value: A) => B), onErr: ((value: E) => B)): B;
    ok(): Option<A>;
    or<F>(result: Result<A, F>): Result<A, F>;
    toJSON(): {
        result: boolean;
        value: A | E;
    };
    unwrap(): undefined | A;
    unwrapErr(): undefined | E;
    unwrapExact(): A;
}

Type Parameters

  • A

    The type of the success value.

  • E

    The type of the error value.

Methods

  • If the Result is Ok, return the provided Result of B, otherwise return the original result (which would be an Err).

    Type Parameters

    • B

    Parameters

    Returns Result<B, E>

  • Given another Result containing a function from A to B:

    If both Results are Ok, call the function with the success value of this Result and return a Result<B, E> containing what the function returns.

    If the result in f is Err, return that instead.

    If the result in f is Ok but the original result is Err, return the original error Result unchanged.

    Type Parameters

    • B

    Parameters

    Returns Result<B, E>

  • Assert that the Result is Err.

    Throws a TypeError if it isn't.

    Returns asserts this is Err<E>

  • Assert that the Result is Ok.

    Throws a TypeError if it isn't.

    Returns asserts this is Ok<A>

  • Transform the contained value using one of the provided functions ok and err, according to whether the Result is Ok or Err.

    Type Parameters

    • B
    • F

    Parameters

    • ok: ((okValue: A) => B)
        • (okValue): B
        • Parameters

          • okValue: A

          Returns B

    • err: ((errValue: E) => F)
        • (errValue): F
        • Parameters

          • errValue: E

          Returns F

    Returns Result<B, F>

  • If the Result is Ok, call the provided function with the contained value and return a new Result containing the result of the function, which must be another Result. If the Result is Err, the Err is returned unchanged and the function is never called.

    This is the monadic bind function, for those who celebrate.

    Type Parameters

    • B

    Parameters

    Returns Result<B, E>

  • If the Result is Err, call the provided function and return its result, which must be another Result. If the Result is Ok, the Ok is returned unchanged and the function is never called.

    Type Parameters

    • F

    Parameters

    Returns Result<A, F>

  • Converts a Result into an Option of the error value, discarding any success value.

    Returns Option<E>

  • Return the value contained in the Result if it's Ok, or return defaultValue otherwise.

    Parameters

    • defaultValue: A

    Returns A

  • Return the value contained in the Result if it's Ok, or call the provided function and return its result otherwise.

    Parameters

    • f: (() => A)
        • (): A
        • Returns A

    Returns A

  • Call the provided function with the contained value if the Result is Err.

    Parameters

    • onErr: ((value: E) => void)
        • (value): void
        • Parameters

          • value: E

          Returns void

    Returns void

  • Call the provided function with the contained value if the Result is Ok.

    Parameters

    • onOk: ((value: A) => void)
        • (value): void
        • Parameters

          • value: A

          Returns void

    Returns void

  • Test if the Result is Err.

    Returns this is Err<E>

  • Test if the Result is Ok.

    Returns this is Ok<A>

  • If the Result is Ok, transform its contained value using the provided function.

    Type Parameters

    • B

    Parameters

    • f: ((value: A) => B)
        • (value): B
        • Parameters

          • value: A

          Returns B

    Returns Result<B, E>

  • If the Result is Err, transform its contained value using the provided function.

    Type Parameters

    • F

    Parameters

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

          • value: E

          Returns F

    Returns Result<A, F>

  • The match function takes two callbacks, one for each possible state of the Result, and calls the one that matches the actual state.

    Type Parameters

    • B

    Parameters

    • onOk: ((value: A) => B)
        • (value): B
        • Parameters

          • value: A

          Returns B

    • onErr: ((value: E) => B)
        • (value): B
        • Parameters

          • value: E

          Returns B

    Returns B

  • Converts a Result into an Option of the success value, discarding any error value.

    Returns Option<A>

  • Convert a Result into a JSON structure for serialisation.

    Returns {
        result: boolean;
        value: A | E;
    }

    • result: boolean
    • value: A | E
  • Converts a Result into an optional value of A, discarding any error value and returning undefined in place of the error.

    Returns undefined | A

  • Converts a Result into an optional value of E, discarding any success value and returning undefined in place of the A value.

    Returns undefined | E

  • Converts a Result into a value of A if it contains one. If it contains a value of E, throw that as an exception.

    Returns A