whenIsLeft
Executes a function only when the input is an EitherLeft. Otherwise, the original value is returned as-is.
Interactive example
Syntax
Classic signature
typescript
function whenIsLeft<
const GenericInput extends unknown,
const GenericOutput extends AnyValue | EscapeVoid
>(
input: GenericInput,
theFunction: (value: Unwrap<Extract<GenericInput, EitherLeft>>) => GenericOutput
): Exclude<GenericInput, EitherLeft> | GenericOutput;Curried signature
typescript
function whenIsLeft<
const GenericInput extends unknown,
const GenericOutput extends AnyValue | EscapeVoid
>(
theFunction: (value: Unwrap<Extract<GenericInput, EitherLeft>>) => GenericOutput
): (input: GenericInput) => Exclude<GenericInput, EitherLeft> | GenericOutput;Parameters
theFunction: Callback triggered only on theLeftpart.input(classic overload): Value to process immediately. Without this argument, the function returns a curried version suited for pipelines.
Return value
- Classic signature: returns either the transformed value (if
Left) or the initial value. - Curried signature: returns a function ready to use in
pipe,map, etc.
Use cases
- Log or transform an error without touching the
Right. - Convert a
Leftinto another type (e.g.,EitherLeft->string). - Simplify repetitive
if (E.isLeft(...))blocks in a functional style.
See also
whenIsRight– Success-side counterpart.isLeft– Manual type guard.whenHasInformation– Pattern matching on literal information.
