Skip to content

eitherAsValue

eitherAsValue<E>(x): E extends Left<infer L> ? E extends Right<infer R> ? L | R : L : E extends Right<infer R> ? R : never

Extracts and returns the value from an Either type, whether it is a Left or a Right. This function discriminates between Left and Right and returns the contained value directly.

Example

eitherAsValue({left: "foo"})
// => "foo"
eitherAsValue({right: "bar"})
// => "bar"

Type Parameters

E extends Either<unknown, unknown>

Parameters

x

E

The Either value to extract from.

Returns

E extends Left<infer L> ? E extends Right<infer R> ? L | R : L : E extends Right<infer R> ? R : never

The value contained in the Either.