Skip to content

rightToMaybe

rightToMaybe<T>(x): T extends Right<infer R> ? T extends Left<unknown> ? Maybe<R> : R : Nothing

Converts a Right value from an Either type to a Just type, or returns Nothing if the value is Left.

Example

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

Type Parameters

T extends Either<unknown, unknown>

The type of the Either. It determines the type encapsulated if Right.

Parameters

x

T

The Either object to examine. This function checks if it is a Right and extracts its value.

Returns

T extends Right<infer R> ? T extends Left<unknown> ? Maybe<R> : R : Nothing

A Maybe type, wrapping the Right value if present; otherwise, Nothing.