Class Result<T, E>

Represents a result that can either hold a value of type T or an error of type E.

Type Parameters

  • T

    The type of the value.

  • E extends Error = Error

    The type of the error.

Constructors

  • Type Parameters

    • T
    • E extends Error = Error

    Parameters

    • value: null | T
    • error: null | string | E

    Returns Result<T, E>

Properties

error: null | E
value: null | T

Methods

  • Combines the current Result with another Result. If the current Result is Ok, returns the other Result. If the current Result is Err, returns a new Result with the same error.

    Type Parameters

    • U

    Parameters

    • result: Result<U, E>

      The other Result to combine with.

    Returns Result<U, E>

    The combined Result.

    Typeparam

    U The type of the value contained in the other Result.

  • Chains a new Result by applying a function to the value of the current Result. If the current Result is an error, the error is propagated to the new Result.

    Type Parameters

    • U

      The type of the value in the new Result.

    Parameters

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

      The function to apply to the value of the current Result.

    Returns Result<U, E>

    • A new Result obtained by applying the function to the value of the current Result.
  • Throws an error with the specified message if the result is an error. Otherwise, returns the value.

    Parameters

    • message: string

      The error message to throw if the result is an error.

    Returns T

    The value if the result is not an error.

    Throws

    Error with the specified message if the result is an error.

  • Throws an error with the specified message if the result is not an error. Returns the error value if the result is an error.

    Parameters

    • message: string

      The error message to throw if the result is not an error.

    Returns E

    The error value if the result is an error.

    Throws

    Error with the specified message if the result is not an error.

  • Inspects the value of the Result using the provided predicate function. If the predicate returns true, the current Result is returned. If the predicate returns false, a new Result with the same error is returned.

    Parameters

    • predicate: ((value) => boolean)

      The predicate function to inspect the value.

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Result<T, E> | Result<null, E>

    The current Result if the predicate returns true, otherwise a new Result with the same error.

  • Inspects the error value of the Result and returns a new Result with the same value and error if the provided predicate returns true. If the predicate returns false, a new Result is returned with the same value and a null error.

    Parameters

    • predicate: ((error) => boolean)

      A function that takes the error value as a parameter and returns a boolean indicating whether the error should be inspected.

        • (error): boolean
        • Parameters

          • error: E

          Returns boolean

    Returns Result<T, Error>

    A new Result instance with the same value and error if the predicate returns true, otherwise a new Result with the same value and a null error.

  • Returns boolean

  • Checks if the result is an error and satisfies the given predicate.

    Parameters

    • predicate: ((error) => boolean)

      The predicate function to apply to the error value.

        • (error): boolean
        • Parameters

          • error: E

          Returns boolean

    Returns boolean

    true if the result is an error and the predicate returns true, otherwise false.

  • Checks if the result is considered "ok".

    Returns boolean

    True if the result is "ok", false otherwise.

  • Checks if the result is Ok and satisfies the given predicate.

    Parameters

    • predicate: ((value) => boolean)

      The predicate function to apply to the value.

        • (value): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns boolean

    true if the result is Ok and the predicate returns true, otherwise false.

  • Applies a function to the value of the Result and returns a new Result with the transformed value. If the Result is an error, it returns a new Result with the same error.

    Type Parameters

    • U

      The type of the transformed value.

    Parameters

    • fn: ((value) => U)

      The function to apply to the value.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

    • A new Result with the transformed value or the same error.
  • Maps the error value of the Result to a new value using the provided function. If the Result is an Err, a new Result with the mapped error value is returned. If the Result is an Ok, a new Result with the same value and a null error is returned.

    Type Parameters

    • F extends Error = Error

      The type of the new error value.

    Parameters

    • fn: ((error) => F)

      The function to map the error value.

        • (error): F
        • Parameters

          • error: E

          Returns F

    Returns Result<T, F>

    • A new Result with the mapped error value.
  • Maps the value of the Result to a new value using the provided function, or returns a default value if the Result is an error.

    Type Parameters

    • U

      The type of the new value.

    Parameters

    • defaultValue: U

      The default value to return if the Result is an error.

    • fn: ((value) => U)

      The function to map the value of the Result.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    • The mapped value or the default value.
  • Maps the value of the Result to a new value using the provided function, or returns a default value if the Result is an Err.

    Type Parameters

    • U

      The type of the new value.

    Parameters

    • defaultValue: (() => U)

      The function that returns the default value.

        • (): U
        • Returns U

    • fn: ((value) => U)

      The function to map the value of the Result.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    • The mapped value or the default value.
  • Returns an Option containing the value of this Result if it is Ok, otherwise returns None.

    Returns Option<T>

    An Option containing the value if it is Ok, otherwise None.

  • Returns the current result if it is successful (Ok), otherwise returns the provided result.

    Type Parameters

    • F extends Error = Error

    Parameters

    • result: Result<T, F>

      The result to return if the current result is not successful.

    Returns Result<T, F>

    The current result if it is successful, otherwise the provided result.

  • Returns the current Result if it is successful (isOk), otherwise invokes the provided function fn and returns the result of that function.

    Type Parameters

    • F extends Error = Error

      The type of the error value.

    Parameters

    Returns Result<T, F>

    The current Result if it is successful, otherwise the result of fn.

  • Unwraps the value from the Result monad. If the Result contains an error, it throws the error. Otherwise, it returns the value.

    Returns T

    The unwrapped value.

    Throws

    If the Result contains an error.

  • Unwraps the error value if the result is an error. Throws an error if the result is not an error.

    Returns E

    The error value.

    Throws

    If the result is not an error.

  • Returns the value contained in the Result if it is Ok, otherwise returns the value contained in the provided Result.

    Parameters

    • result: Result<T, E>

      The Result to use if the current Result is not Ok.

    Returns T

    The value contained in the Result if it is Ok, otherwise the value contained in the provided Result.

  • Returns the value contained in the Result if it is Ok, otherwise returns the provided default value.

    Parameters

    • defaultValue: T

      The default value to return if the Result is not Ok.

    Returns T

    The value contained in the Result if it is Ok, otherwise the provided default value.

  • Unwraps the result value if it is Ok, otherwise calls the provided function and returns its result.

    Parameters

    Returns T

    The unwrapped value if the result is Ok, otherwise the result of calling the provided function.

  • Creates a new Result instance representing an error.

    Type Parameters

    • T

      The type of the value in the Result.

    • E extends Error = Error

      The type of the error in the Result.

    Parameters

    • error: string | E

      The error object or error message.

    Returns Result<T, E>

    • A new Result instance representing an error.
  • Creates a new Result instance from a value and an error.

    Type Parameters

    • T

      The type of the value.

    • E extends Error = Error

      The type of the error.

    Parameters

    • value: T

      The value of the Result.

    • error: null | E

      The error of the Result.

    Returns Result<T, E>

    A new Result instance.

  • Matches a Result object against provided patterns and returns the corresponding result.

    Type Parameters

    • T

      The type of the success value in the Result.

    • E extends Error

      The type of the error value in the Result.

    • R

      The type of the returned value.

    Parameters

    • result: Result<T, E>

      The Result object to match against.

    • patterns: {
          Err: ((error) => R);
          Ok: ((value) => R);
      }

      The patterns to match against.

      • Err: ((error) => R)
          • (error): R
          • Parameters

            • error: E

            Returns R

      • Ok: ((value) => R)
          • (value): R
          • Parameters

            • value: T

            Returns R

    Returns R

    • The result of the matching pattern.
  • Creates a new Result instance with a successful value.

    Type Parameters

    • T

      The type of the successful value.

    • E extends Error = Error

      The type of the error value.

    Parameters

    • value: T

      The successful value.

    Returns Result<T, E>

    A new Result instance with the successful value.