Class Either<L, R>

The Either<R,L> type represents 2 possible values of 2 possible types.

A common use case it to use this to represent the outcome of a function that could be an error or a value: const result: Either<ErrorType, ValueType>. Convention has ErrorType as the left-value and ValueType as the right-value.

The type will only ever be single-valued, but it will always be single-valued

Type Parameters

  • L
  • R

Constructors

Properties

left: Optional<L>
right: Optional<R>

Methods

  • Applies a function to the internal value. Since this returns void and takes functions that return void, this relies entirely on side-effects..

    Parameters

    • lFunc: ((val) => void)
        • (val): void
        • Parameters

          • val: L

          Returns void

    • rFunc: ((val) => void)
        • (val): void
        • Parameters

          • val: R

          Returns void

    Returns void

  • Gets the left-value of the object.

    This will result in an error, if the object is not left-valued

    Returns L

    Throws

  • Gets the right-value of the object.

    This will result in an error, if the object is not right-valued

    Returns R

    Throws

  • Runs the provided function if the left value is populated

    Parameters

    • lFunc: ((val) => void)
        • (val): void
        • Parameters

          • val: L

          Returns void

    Returns void

  • Runs the provided function if the right value is populated

    Parameters

    • rFunc: ((val) => void)
        • (val): void
        • Parameters

          • val: R

          Returns void

    Returns void

  • Returns whether or not the value is left-valued

    Returns boolean

  • Returns whether or not the value is right-valued

    Returns boolean

  • Maps the Either<L,R> object to a simple object of type T.

    Each of the mapping functions must return the same type, so that this Either object can be mapped.

    Only one of the functions will be called, so no side-effects should be relied upon.

    Type Parameters

    • O

    Parameters

    Returns O

  • Type Parameters

    • T

    Parameters

    • lFunc: ((val) => T)
        • (val): T
        • Parameters

          • val: L

          Returns T

    • rFunc: ((val) => T)
        • (val): T
        • Parameters

          • val: R

          Returns T

    Returns T

  • Map the left-value of one type to a different value of another type.

    If this object is right-valued, then this doesn't change that, but it does change the overall type of the Either object

    Type Parameters

    • O

    Parameters

    Returns Either<O, R>

  • Type Parameters

    • T

    Parameters

    • lFunc: ((val) => T)
        • (val): T
        • Parameters

          • val: L

          Returns T

    Returns Either<T, R>

  • Map the right-value of one type to a different value of another type.

    If this object is leftt-valued, then this doesn't change that, but it does change the overall type of the Either object

    Type Parameters

    • O

    Parameters

    Returns Either<L, O>

  • Type Parameters

    • T

    Parameters

    • rFunc: ((val) => T)
        • (val): T
        • Parameters

          • val: R

          Returns T

    Returns Either<L, T>

  • Much like {@link mapLeft}, this converts an Either from one type to another.

    The difference here is that the mapping function returns an Either of its own, rather than just a left-value. This allows you to chain a series of Eithers together, only operating if you are getting left-valued Eithers

    Type Parameters

    • T

    Parameters

    Returns Either<T, R>

  • Type Parameters

    • T

    Parameters

    Returns Promise<Either<T, R>>

  • Much like {@link mapRight}, this converts an Either from one type to another.

    The difference here is that the mapping function returns an Either of its own, rather than just a right-value. This allows you to chain a series of Eithers together, only operating if you are getting right-valued Eithers

    Type Parameters

    • T

    Parameters

    Returns Promise<Either<L, T>>

  • Type Parameters

    • T

    Parameters

    Returns Either<L, T>