Err is a type that represents an error result.

Type Parameters

  • E

Implements

Constructors

  • Creates an Err.

    Type Parameters

    • E

    Parameters

    • error: E

      The error to be wrapped.

    Returns Err<E>

    const errorResult = new Err('Something went wrong');
    

Methods

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

    Type Parameters

    • U
    • E2

    Parameters

    • _: Result<U, E2>

      The Result to be ignored as this Result is an Err.

    Returns Result<never, E>

    This Result as it is an Err.

    const errorResult = new Err('Something went wrong');
    const anotherResult = new Ok(42);
    const combined = errorResult.and(anotherResult); // combined is errorResult
  • 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

    Parameters

    • _: ((value: never) => Result<U, E2>)

      The function to be ignored as this Result is an Err.

    Returns Result<never, E>

    This Result as it is an Err.

    const errorResult = new Err('Something went wrong');
    const newResult = errorResult.andThen(value => new Ok(value)); // newResult is errorResult
  • Returns true if this Result is an Err, false otherwise.

    Returns this is Err<E>

    true if this Result is an Err.

    const errorResult = new Err('Something went wrong');
    console.log(errorResult.isError()); // true
  • Returns true if this Result is an Err, false otherwise.

    Returns this is Ok<unknown>

    true if this Result is an Err.

    const errorResult = new Err('Something went wrong');
    console.log(errorResult.isOk()); // false
  • 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

    • _: ((value: never) => U)

      The function to be ignored as this Result is an Err.

        • (value): U
        • Parameters

          • value: never

          Returns U

    Returns Result<U, E>

    This Result as it is an Err.

    const errorResult = new Err('Something went wrong');
    const mappedResult = errorResult.map(value => value.toString()); // mappedResult is errorResult
  • Returns a new Result that is the result of applying the given function to the error of this Result. If this Result is an Err, the function is applied to the error.

    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<never, E2>

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

    const errorResult = new Err('Something went wrong');
    const newErrorResult = errorResult.mapError(error => 'New error'); // newErrorResult is Err('New error')
  • Throws an error as this Result is an Err.

    Returns never

    Always throws an error as this Result is an Err.

    const errorResult = new Err('Something went wrong');
    errorResult.unwrap(); // throws Error
  • Returns the error of this Result.

    Returns E

    The error of this Result.

    const errorResult = new Err('Something went wrong');
    console.log(errorResult.unwrapError()); // 'Something went wrong'
  • Returns the default value as this Result is an Err.

    Type Parameters

    • T

    Parameters

    • defaultValue: T

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

    Returns T

    The default value as this Result is an Err.

    const errorResult = new Err('Something went wrong');
    console.log(errorResult.unwrapOr(42)); // 42