Represents a failed operation with an error message.

Type Parameters

  • T

    The type that would have been returned if successful

Constructors

Properties

Methods

Constructors

  • Creates a new Err instance containing an error message.

    Type Parameters

    • T

    Parameters

    • error: string

      The error message describing what went wrong

    Returns Err<T>

Properties

error: string

The error message describing what went wrong

Methods

  • Type guard to check if this Result is an Ok. Always returns false for Err instances.

    Returns this is Ok<T>

    false for Err instances

    const result: Result<number> = err("Invalid input");
    if (!result.isOk()) {
    // TypeScript knows result.error exists here
    console.log(result.error);
    }
  • Returns a new Err with the same error message but a different type parameter. Since this represents an error, the transform function is never called.

    Type Parameters

    • O

      The new type parameter

    Parameters

    • _fn: (value: T) => O

      The function that would have transformed the value (ignored)

    Returns Result<O>

    A new Err with the same error message

    const result: Result<number> = err("Invalid input");
    const mapped: Result<string> = result.map(x => x.toString());
    // mapped = Err("Invalid input")