Interface RightEither<L, R>

Implementation of the Either interface for a Right value.

interface RightEither<L, R> {
    type: symbol;
    isLeft(): this is LeftEither<L, R>;
    isRight(): this is RightEither<L, R>;
    left(): NoneOption<L>;
    leftAndThen<U>(fn: ((val: L) => Either<U, R>)): Either<U, R>;
    mapLeft<U>(_fn: ((val: L) => U)): RightEither<U, R>;
    mapRight<U>(fn: ((val: R) => U)): RightEither<L, U>;
    match<U>(fn: Match<L, R, U>): U;
    right(): SomeOption<R>;
    rightAndThen<U>(fn: ((val: R) => Either<L, U>)): Either<L, U>;
    unwrap(): L | R;
    unwrapLeft(): never;
    unwrapLeftOr(other: L): L;
    unwrapRight(): R;
    unwrapRightOr(other: R): R;
}

Type Parameters

  • L extends NonUndefined
  • R extends NonUndefined

Hierarchy (view full)

Properties

type: symbol

A symbol indicating whether the Either is Left or Right. Primarily for internal use.

Methods

  • Checks if the Either is a Left value.

    Returns this is LeftEither<L, R>

    true if the Either is Left, otherwise false.

    console.log(Left('error').isLeft()); // true
    console.log(Right(42).isLeft()); // false
  • Checks if the Either is a Right value.

    Returns this is RightEither<L, R>

    true if the Either is Right, otherwise false.

    console.log(Right(42).isRight()); // true
    console.log(Left('error').isRight()); // false
  • Converts the Either to an Option of its Left value.

    Returns NoneOption<L>

    An Option containing the Left value if present, otherwise None.

    const leftOption = Left('error').left(); // Some('error')
    const rightOption = Right(42).left(); // None
  • Transforms the Either by applying a function to its Left value if present, chaining multiple operations.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val: L) => Either<U, R>)

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

    Returns Either<U, R>

    The Either returned by the function if the original was Left, otherwise the original Right.

    const either = Left('initial error');
    const chained = either.leftAndThen((err) => Left(`Processed ${err}`));
    console.log(chained.unwrapLeft()); // "Processed initial error"
  • Maps an Either by applying a function to its Left value, leaving Right untouched.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • _fn: ((val: L) => U)
        • (val): U
        • Parameters

          • val: L

          Returns U

    Returns RightEither<U, R>

    A new Either with the Left value transformed if it was Left, otherwise the original Right.

    const either = Left('error');
    const mapped = either.mapLeft((err) => `Error: ${err}`);
    console.log(mapped.unwrapLeft()); // "Error: error"
  • Maps an Either by applying a function to its Right value, leaving Left untouched.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val: R) => U)

      A function to apply to the Right value.

        • (val): U
        • Parameters

          • val: R

          Returns U

    Returns RightEither<L, U>

    A new Either with the Right value transformed if it was Right, otherwise the original Left.

    const either = Right(42);
    const mapped = either.mapRight((val) => val.toString());
    console.log(mapped.unwrapRight()); // "42"
  • Performs a match operation on the Either, allowing for branching logic based on its state.

    Type Parameters

    • U extends void | NonUndefined

    Parameters

    • fn: Match<L, R, U>

      An object containing functions for each case: Left and Right.

    Returns U

    The result of applying the corresponding function based on the Either's state.

    const either = Left('error');
    const result = either.match({
    left: (err) => `Error: ${err}`,
    right: (val) => `Success: ${val}`,
    });
    console.log(result); // "Error: error"
  • Converts the Either to an Option of its Right value.

    Returns SomeOption<R>

    An Option containing the Right value if present, otherwise None.

    const leftOption = Left('error').right(); // None
    const rightOption = Right(42).right(); // Some(42)
  • Transforms the Either by applying a function to its Right value if present, chaining multiple operations.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val: R) => Either<L, U>)

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

    Returns Either<L, U>

    The Either returned by the function if the original was Right, otherwise the original Left.

    const either = Right(42);
    const chained = either.rightAndThen((val) => Right(val.toString()));
    console.log(chained.unwrapRight()); // "42"
  • Unwraps the Either, yielding the contained value.

    Throws an error if the Either is not the expected type (Left or Right).

    Returns L | R

    The contained value.

    Error if the operation is invalid.

  • Unwraps the Either, yielding the Left value if present.

    Throws an error if the Either is a Right.

    Returns never

    The Left value.

    Error if the Either is Right.

    console.log(Left('error').unwrapLeft()); // 'error'
    
  • Returns the Left value if present, otherwise returns the provided default value.

    Parameters

    • other: L

      The default value to return if the Either is Right.

    Returns L

    The Left value or the default.

    console.log(Left('error').unwrapLeftOr('default')); // 'error'
    console.log(Right(42).unwrapLeftOr('default')); // 'default'
  • Unwraps the Either, yielding the Right value if present.

    Throws an error if the Either is a Left.

    Returns R

    The Right value.

    Error if the Either is Left.

    console.log(Right(42).unwrapRight()); // 42
    
  • Returns the Right value if present, otherwise returns the provided default value.

    Parameters

    • other: R

      The default value to return if the Either is Left.

    Returns R

    The Right value or the default.

    console.log(Right(42).unwrapRightOr(100)); // 42
    console.log(Left('error').unwrapRightOr(100)); // 100