Skip to content

whenRight

whenRight<E, T>(x, f): E extends Right<unknown> ? T : E

If the given Either is a Left, return it, otherwise apply the given function to the Right value and return the result.

Example

whenRight({ right: 'foo' }, (x) => `${x}${x}`)
// => "foofoo"
whenRight({ left: 'bar' }, (x) => `${x}${x}`)
// => { left: "bar" }

Type Parameters

E extends Either<unknown, unknown>

T extends Either<unknown, unknown>

The mapped type.

Parameters

x

E

The either value to map.

f

(r) => T

The map function.

Returns

E extends Right<unknown> ? T : E

The mapped value if right.