@bodil/opt
    Preparing search index...

    Interface Result<A, E>

    A value which can be of either type A or type E.

    This is normally used as a return value for operations which can fail: E is short for Error. A can be thought of as the success value, and E is the error value. This is reflected in their constructors: to construct a success value, you call Ok, and to construct an error value, you call Err.

    You can also use the Result.await function to convert a Promise that could throw an exception into a Promise that won't throw but instead returns a Result containing either the error or the expected result, or the Result.try function to run a function and catch any exceptions into a Result return value.

    function processResult(result: Result<string, Error>): void {
    if (result.isOk()) {
    console.info(result.value);
    } else {
    console.error(result.value.message);
    }
    }

    processResult(Ok("Hello Joe!"));
    processResult(Err(new Error("Get Robert to fix the bug!")));
    interface Result<A, E> {
        value: A | E;
        and<B>(result: Result<B, E>): Result<B, E>;
        ap<B>(f: Result<(a: A) => B, E>): Result<B, E>;
        assertErr(): void;
        assertOk(): void;
        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 ResultErr<DOMException>;
        isErr(): this is ResultErr<E>;
        isOk(): this is ResultOk<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(): A | undefined;
        unwrapErr(): E | undefined;
        unwrapExact(): A;
    }

    Type Parameters

    • A

      The type of the success value.

    • E

      The type of the error value.

    Properties

    value: A | E

    If Result.isOk() is true, this is the contained value of type A. If Result.isErr() is true, this is the error value of type E.

    Methods

    • 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>

    • 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
      • err: (errValue: E) => F

      Returns Result<B, F>

    • If the Result is Ok, call the provided function with the contained value and return 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>

    • 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

      Returns A

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

      Parameters

      • onErr: (value: E) => void

      Returns void

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

      Parameters

      • onOk: (value: A) => void

      Returns void

    • 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
      • onErr: (value: E) => B

      Returns B

    • Convert a Result into a JSON structure for serialisation.

      Returns { 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 A | undefined

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

      Returns E | undefined

    • 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