Skip to main content

mapRight

Callable

  • mapRight<E, T>(x: E, f: (r: E extends Right<R> ? R : never) => T): E extends Right<unknown> ? Right<T> : E

  • Applies a transformation function to the Right value of an Either type, if present. If the input is a Left, it remains unchanged.

    Example

    mapRight({ right: 'bar' }, (x) => `${x}${x}`)
    // => { right: "barbar" }

    mapRight({ left: 'bar' }, (x) => `${x}${x}`)
    // => { left: "bar" }

    Type parameters

    • E: Either<unknown, unknown>

      The type of the Either.

    • T

      The type of the transformed Right value.

    Parameters

    • x: E

      The Either value to map.

    • f: (r: E extends Right<R> ? R : never) => T

      The transformation function to apply to the Right value.

    Returns E extends Right<unknown> ? Right<T> : E

    An Either object where the Right has been transformed if present, or the original Left.