whenIsRight
Applies a function only when the input is an EitherRight. Otherwise, the original value is returned as-is.
Interactive example
Syntax
Classic signature
typescript
function whenIsRight<
const GenericInput extends unknown,
const GenericOutput
>(
input: GenericInput,
theFunction: (value: Unwrap<Extract<GenericInput, EitherRight>>) => GenericOutput
): Exclude<GenericInput, EitherRight> | GenericOutput;Curried signature
typescript
function whenIsRight<
const GenericInput extends unknown,
const GenericOutput
>(
theFunction: (value: Unwrap<Extract<GenericInput, EitherRight>>) => GenericOutput
): (input: GenericInput) => Exclude<GenericInput, EitherRight> | GenericOutput;Parameters
theFunction: Callback executed only on theRightpart.input(overload 2): Value to process immediately (otherwise the returned function awaits anEither).
Return value
- Curried overload: returns a function ready to use in a pipeline.
- Direct overload: returns either the callback result (if
Right) or the initial value.
Use cases
- Enrich a
Right(logging, transformation) without touchingLeftvalues. - Inject
whenIsRightintopipeto keep a smooth flow. - Replace repetitive
if (E.isRight(...))with a functional API.
See also
isRight– Manually test the type before acting.whenIsLeft– Error-side counterpart.whenHasInformation– Pattern matching based on literal info.
