⚠️ Internal: This API is not publically exported by the package.

Interface BaseResult<T, E>

interface BaseResult {
    [iterator](): Iterator<T extends Iterable<U>
        ? U
        : never, any, undefined>;
    andThen<T2>(mapper): Result<T2, E>;
    andThen<E2>(mapper): Result<T, E | E2>;
    andThen<T2, E2>(mapper): Result<T2, E | E2>;
    andThen<T2, E2>(mapper): Result<T2, E | E2>;
    else<T2>(val): T | T2;
    expect(msg): T;
    expectErr(msg): E;
    isErr(): this is ErrImpl<E>;
    isOk(): this is OkImpl<T>;
    map<U>(mapper): Result<U, E>;
    mapErr<F>(mapper): Result<T, F>;
    mapOr<U>(default_, mapper): U;
    mapOrElse<U>(default_, mapper): U;
    or<E2>(other): Result<T, E2>;
    orElse<E2>(other): Result<T, E2>;
    toAsyncResult(): AsyncResult<T, E>;
    toOption(): Option<T>;
    unwrap(): T;
    unwrapErr(): E;
    unwrapOr<T2>(val): T | T2;
}

Type Parameters

  • T

  • E

Implemented by

Methods

  • Returns Iterator<T extends Iterable<U>
        ? U
        : never, any, undefined>

  • Calls mapper if the result is Ok, otherwise returns the Err value of self. This function can be used for control flow based on Result values.

    Type Parameters

    • T2

    Parameters

    • mapper: ((val) => Ok<T2>)
        • (val): Ok<T2>
        • Parameters

          • val: T

          Returns Ok<T2>

    Returns Result<T2, E>

  • Type Parameters

    • E2

    Parameters

    • mapper: ((val) => Err<E2>)
        • (val): Err<E2>
        • Parameters

          • val: T

          Returns Err<E2>

    Returns Result<T, E | E2>

  • Type Parameters

    • T2

    • E2

    Parameters

    • mapper: ((val) => Result<T2, E2>)

    Returns Result<T2, E | E2>

  • Type Parameters

    • T2

    • E2

    Parameters

    • mapper: ((val) => Result<T2, E2>)

    Returns Result<T2, E | E2>

  • Returns the contained Ok value or a provided default.

    Type Parameters

    • T2

    Parameters

    • val: T2

    Returns T | T2

    See

    unwrapOr

    Deprecated

    in favor of unwrapOr

  • Returns the contained Ok value, if exists. Throws an error if not.

    The thrown error's [cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) is set to value contained in Err`.

    If you know you're dealing with Ok and the compiler knows it too (because you tested isOk() or isErr()) you should use value instead. While Ok's expect() and value will both return the same value using value is preferable because it makes it clear that there won't be an exception thrown on access.

    Parameters

    • msg: string

      the message to throw if no Ok value.

    Returns T

  • Returns the contained Err value, if exists. Throws an error if not.

    Parameters

    • msg: string

      the message to throw if no Err value.

    Returns E

  • true when the result is Err

    Returns this is ErrImpl<E>

  • true when the result is Ok

    Returns this is OkImpl<T>

  • Maps a Result<T, E> to 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.

    Type Parameters

    • U

    Parameters

    • mapper: ((val) => U)
        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns Result<U, E>

  • Maps a Result<T, E> to 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.

    Type Parameters

    • F

    Parameters

    • mapper: ((val) => F)
        • (val): F
        • Parameters

          • val: E

          Returns F

    Returns Result<T, F>

  • Maps a Result<T, E> to Result<U, E> by either converting T to U using mapper (in case of Ok) or using the default_ value (in case of Err).

    If default is a result of a function call consider using mapOrElse instead, it will only evaluate the function when needed.

    Type Parameters

    • U

    Parameters

    • default_: U
    • mapper: ((val) => U)
        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns U

  • Maps a Result<T, E> to Result<U, E> by either converting T to U using mapper (in case of Ok) or producing a default value using the default function (in case of Err).

    Type Parameters

    • U

    Parameters

    • default_: ((error) => U)
        • (error): U
        • Parameters

          • error: E

          Returns U

    • mapper: ((val) => U)
        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns U

  • Returns Ok() if we have a value, otherwise returns other.

    other is evaluated eagerly. If other is a result of a function call try orElse() instead – it evaluates the parameter lazily.

    Type Parameters

    • E2

    Parameters

    Returns Result<T, E2>

    Example

    Ok(1).or(Ok(2)) // => Ok(1)
    Err('error here').or(Ok(2)) // => Ok(2)
  • Returns Ok() if we have a value, otherwise returns the result of calling other().

    other() is called only when needed and is passed the error value in a parameter.

    Type Parameters

    • E2

    Parameters

    • other: ((error) => Result<T, E2>)
        • (error): Result<T, E2>
        • Parameters

          • error: E

          Returns Result<T, E2>

    Returns Result<T, E2>

    Example

    Ok(1).orElse(() => Ok(2)) // => Ok(1)
    Err('error').orElse(() => Ok(2)) // => Ok(2)
  • Creates an AsyncResult based on this Result.

    Useful when you need to compose results with asynchronous code.

    Returns AsyncResult<T, E>

  • Converts from Result<T, E> to Option<T>, discarding the error if any

    Similar to rust's ok method

    Returns Option<T>

  • Returns the contained Ok value. Because this function may throw, its use is generally discouraged. Instead, prefer to handle the Err case explicitly.

    If you know you're dealing with Ok and the compiler knows it too (because you tested isOk() or isErr()) you should use value instead. While Ok's unwrap() and value will both return the same value using value is preferable because it makes it clear that there won't be an exception thrown on access.

    Throws if the value is an Err, with a message provided by the Err's value and [`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) set to the value.

    Returns T

  • Returns the contained Ok value or a provided default.

    (This is the unwrap_or in rust)

    Type Parameters

    • T2

    Parameters

    • val: T2

    Returns T | T2