Class Either<Left, Right>

An Either is a datastructure that represents a value that can be of type 'Left' or 'Right'. It is used to represent the result of a computation that can fail. By convention, the 'Left' type is used to represent the failure and the 'Right' type is used to represent the success. An Either supports values that are null or undefined. It's up the consumer to decide if these values are valid or not. When an Either is created, an ActiveSide is set. This ActiveSide is used to determine if the Either is a 'Left' or a 'Right', not the value itself.

Example

const value = 5;
const result = value > 0 ? Either.right(value) : Either.left('Value must be positive');

Type Parameters

  • Left
  • Right

Constructors

Properties

activeSide: EitherSide
left?: Left
right?: Right

Methods

  • Alias for 'getRight'. Get the value of the Either. If the Either is a 'Left', it will throw an error. If the Either is a 'Right', it will return the value.

    Returns Right

    The value of the Either.

    Throws

    An error if the Either is a 'Left'.

  • Get the 'Left' value of the Either.

    Returns Left

    The 'Left' value of the Either.

    Throws

    An error if the Either is a 'Right'.

  • Get the 'Right' value of the Either.

    Returns Right

    The 'Right' value of the Either.

    Throws

    An error if the Either is a 'Left'.

  • Check if the Either is a 'Left'.

    Returns boolean

    true if the Either is a 'Left', false otherwise.

  • Check if the Either is a 'Right'.

    Returns boolean

    true if the Either is a 'Right', false otherwise.

  • Alias for 'mapRight'. Maps the 'Right' value of the Either to a new value. If the Either is a 'Left', it will return a new 'Left' Either with the same value. If the Either is a 'Right', it will return a new 'Right' Either with the new value.

    Type Parameters

    • NewRight

    Parameters

    • transformer: ((right) => NewRight)

      A function that takes the 'Right' value and returns a new value.

    Returns Either<Left, NewRight>

    A new Either with the mapped 'Right' value if it was present, or the same 'Left' value if it was present.

  • Maps the 'Left' value of the Either to a new value. If the Either is a 'Right', it will return a new 'Right' Either with the same value. If the Either is a 'Left', it will return a new 'Left' Either with the new value.

    Type Parameters

    • NewLeft

    Parameters

    • transformer: ((left) => NewLeft)

      A function that takes the 'Left' value and returns a new value.

    Returns Either<NewLeft, Right>

    A new Either with the mapped 'Left' value if it was present, or the same 'Right' value if it was present.

  • Maps the 'Right' value of the Either to a new value. If the Either is a 'Left', it will return a new 'Left' Either with the same value. If the Either is a 'Right', it will return a new 'Right' Either with the new value.

    Type Parameters

    • NewRight

    Parameters

    • transformer: ((right) => NewRight)

      A function that takes the 'Right' value and returns a new value.

    Returns Either<Left, NewRight>

    A new Either with the mapped 'Right' value if it was present, or the same 'Left' value if it was present.

  • Swap the Either. If the Either is a 'Left', it will become a 'Right' with the same value. If the Either is a 'Right', it will become a 'Left' with the same value.

    Returns Either<Right, Left>

    A new Either with the same value but on the opposite side.

Generated using TypeDoc