Class Result<T, E>Abstract

Represents a result that can either be a success (Ok<T>) or an error (Err<E>). Provides functional methods for safe computations without exceptions.

Type Parameters

  • T

    The type of the successful value.

  • E

    The type of the error.

Hierarchy (view full)

Constructors

Methods

  • Maps the successful value to another Result<U, E>, allowing chaining.

    Type Parameters

    • U

      The transformed success type.

    Parameters

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

      The function returning a Result<U, E>.

    Returns Result<U, E>

    A new Result<U, E>.

  • Folds the result into a single value by applying one of two functions.

    Type Parameters

    • U

      The return type.

    Parameters

    • ifErr: ((error: E) => U)

      Function applied to Err<E>.

        • (error): U
        • Parameters

          • error: E

          Returns U

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

      Function applied to Ok<T>.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns U

    The computed value.

  • Maps the successful value using the given function.

    Type Parameters

    • U

      The transformed success type.

    Parameters

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

      The function to apply.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

    A new Result<U, E>.

  • Maps the error using the given function.

    Type Parameters

    • F

      The transformed error type.

    Parameters

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

      The function to apply.

        • (error): F
        • Parameters

          • error: E

          Returns F

    Returns Result<T, F>

    A new Result<T, F>.

  • Returns an alternative Result<T, E> if this is Err<E>.

    Parameters

    • alternative: Result<T, E>

      The alternative Result<T, E>.

    Returns Result<T, E>

    The original Result<T, E> if Ok<T>, otherwise alternative.

  • Retrieves the success value if Ok<T>, otherwise returns a default.

    Parameters

    • defaultValue: T

      The default value if Err<E>.

    Returns T

    The contained value or defaultValue.

  • Retrieves the success value if Ok<T>, otherwise throws an error.

    Returns T

    The contained value if Ok<T>.

    Throws an error if Err<E>.