Skip to content

whenIsRightElse

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

Interactive example

Syntax

Classic signature

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

Curried signature

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

Parameters

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

Return value

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

See also

Released under the MIT license.