Skip to content

whenIsLeftElse

Applies one callback on Left values and a second callback on every non-Left value.

Interactive example

Syntax

Classic signature

typescript
function whenIsLeftElse<
  GenericInput extends unknown,
  GenericOutputLeft,
  GenericOutputElse
>(
  input: GenericInput,
  theFunction: (value: Unwrap<Extract<GenericInput, Left>>) => GenericOutputLeft,
  elseFunction: (value: Extract<GenericInput, Right> | Exclude<GenericInput, Right | Left>) => GenericOutputElse
): GenericOutputLeft | GenericOutputElse;

Curried signature

typescript
function whenIsLeftElse<
  GenericInput extends unknown,
  GenericOutputLeft,
  GenericOutputElse
>(
  theFunction: (value: Unwrap<Extract<GenericInput, Left>>) => GenericOutputLeft,
  elseFunction: (value: Extract<GenericInput, Right> | Exclude<GenericInput, Right | Left>) => GenericOutputElse
): (input: GenericInput) => GenericOutputLeft | GenericOutputElse;

Parameters

  • theFunction: Callback executed when input is Left (receives unwrapped payload).
  • elseFunction: Callback executed for remaining values (Right or non-Either), without unwrap.
  • input: Value to process immediately (optional in curried style).

Return value

Returns the result of theFunction for Left, otherwise the result of elseFunction.

See also

Released under the MIT license.