Skip to content

eitherToError

eitherToError<E>(x): E extends Right<infer R> ? E extends Left<unknown> ? R : R : never

Returns right when x is a Right type, otherwise throw left.

Pseudocode

if (isLeft(x)) {
throw x.left
}
return x.right

Example

eitherToError({ right: 'foo' })
// => 'foo'
eitherToError({ left: new MyError('my-message') })
// => throw new MyError('my-message')

Type Parameters

E extends Either<unknown, unknown>

Parameters

x

E

The Either type.

Returns

E extends Right<infer R> ? E extends Left<unknown> ? R : R : never

x.right when the Either is Right.

Throws

x.left when the Either is Left.

See

Factorial - Wikipedia