Interface IResult<T, E>

Result is a type that represents a result of an operation that may or may not be successful.

interface IResult<T, E> {
    and<U, E2>(res: Result<U, E2>): Result<U, E | E2>;
    andThen<U, E2>(fn: ((value: T) => Result<U, E2>)): Result<U, E | E2>;
    isError(): this is Err<E>;
    isOk(): this is Ok<T>;
    map<U>(fn: ((value: T) => U)): Result<U, E>;
    mapError<E2>(fn: ((error: E) => E2)): Result<T, E2>;
    unwrap(): T;
    unwrapError(): E;
    unwrapOr(defaultValue: T): T;
}

Type Parameters

  • T = unknown
  • E = Error

Implemented by

Methods

  • Returns a Result. If this Result is an Ok, the function returns passed Result. If this Result is an Err, the function returns this Result.

    Type Parameters

    • U
    • E2 = Error

    Parameters

    • res: Result<U, E2>

      The Result to be returned if this Result is an Err.

    Returns Result<U, E | E2>

    A Result that is the result of applying the given function to the value of this Result.

  • Returns a new Result that is the result of applying the given function to the value of this Result. If this Result is an Err, the function is not applied and the error is returned.

    Type Parameters

    • U
    • E2 = Error

    Parameters

    • fn: ((value: T) => Result<U, E2>)

      The function to be applied to the value of this Result.

    Returns Result<U, E | E2>

    A Result that is the result of applying the given function to the value of this Result.

  • Returns true if this Result is an Err, false otherwise.

    Returns this is Err<E>

    true if this Result is an Err, false otherwise.

  • Returns true if this Result is an Ok, false otherwise.

    Returns this is Ok<T>

    true if this Result is an Ok, false otherwise.

  • Returns a new Result that is the result of applying the given function to the value of this Result. If this Result is an Err, the function is not applied and the error is returned.

    Type Parameters

    • U

    Parameters

    • fn: ((value: T) => U)

      The function to be applied to the value of this Result.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

    A Result that is the result of applying the given function to the value of this Result.

  • Returns a new Result that is the result of applying the given function to the error of this Result. If this Result is an Ok, the function is not applied and the value is returned.

    Type Parameters

    • E2

    Parameters

    • fn: ((error: E) => E2)

      The function to be applied to the error of this Result.

        • (error): E2
        • Parameters

          • error: E

          Returns E2

    Returns Result<T, E2>

    A Result that is the result of applying the given function to the error of this Result.

  • Returns the value of this Result.

    Returns T

    The value of this Result.

    If this Result is an Err.

  • Returns the error of this Result.

    Returns E

    The error of this Result.

    If this Result is an Ok.

  • Returns the value of this Result. If this Result is an Err, the defaultValue is returned.

    Parameters

    • defaultValue: T

      The value to be returned if this Result is an Err.

    Returns T

    The value of this Result.