/** * adapted from https://github.com/gcanti/fp-ts */ /** * @tsplus type Either */ export type Either = Left | Right export interface EitherF extends HKT { readonly type: Either } export interface EitherFixedLeftF extends HKT { readonly type: HKT.Kind } /** * @tsplus type Either.Ops */ export interface EitherOps { $: EitherAspects } export const Either: EitherOps = { $: {} } /** * @tsplus type Either.Aspects */ export interface EitherAspects {} export declare namespace Either { export type HKT = EitherF export type FixedLeftHKT = EitherFixedLeftF } /** * @tsplus type Either.Left */ export interface Left { readonly _tag: "Left" readonly left: E } /** * @tsplus type Either.Right */ export interface Right { readonly _tag: "Right" readonly right: A } /** * @tsplus unify Either * @tsplus unify Either.Left * @tsplus unify Either.Right */ export function unifyEither>( self: X ): Either< X extends Left ? EX : never, X extends Right ? AX : never > { return self } /** * Returns `true` if the `Either` is an instance of `Left`, `false` otherwise. * * @tsplus fluent Either isLeft */ export function isLeft(ma: Either): ma is Left { return ma._tag === "Left" } /** * Returns `true` if the `Either` is an instance of `Right`, `false` otherwise. * * @tsplus fluent Either isRight */ export function isRight(ma: Either): ma is Right { return ma._tag === "Right" } /** * @tsplus getter Either left */ export function getLeft(self: Either): Maybe { return self._tag === "Left" ? Maybe.some(self.left) : Maybe.none } /** * @tsplus static Either.Ops isEither */ export function isEither(u: unknown): u is Either { return ( typeof u === "object" && u != null && "_tag" in u && (u["_tag"] === "Left" || u["_tag"] === "Right") ) } /** * @tsplus getter Either right */ export function getRight(self: Either): Maybe { return self._tag === "Right" ? Maybe.some(self.right) : Maybe.none } /** * Constructs a new `Either` holding a `Right` value. This usually represents a * successful value due to the right bias of this structure. * * @tsplus static Either.Ops right * @tsplus static Either.Ops __call */ export function right(a: A): Either { return { _tag: "Right", right: a } } /** * Constructs a new `Either` holding a `Right` value. This usually represents a * successful value due to the right bias of this structure. * * @tsplus static Either.Ops rightW */ export function rightW(a: A): Either { return { _tag: "Right", right: a } } /** * Constructs a new `Either` holding a `Left` value. This usually represents a * failure, due to the right-bias of this structure. * * @tsplus static Either.Ops left */ export function left(e: E): Either { return { _tag: "Left", left: e } } /** * Constructs a new `Either` holding a `Left` value. This usually represents a * failure, due to the right-bias of this structure. * * @tsplus static Either.Ops leftW */ export function leftW(e: E): Either { return { _tag: "Left", left: e } } /** * Widen left side `Either[E, A] => Either[E | E1, A]`. * * @tsplus getter Either widenE * @tsplus macro identity */ export function widenE_(self: Either): Either { return self } /** * Widen left side `Either[E, A] => Either[E | E1, A]`. * * @tsplus static Either.Aspects widenE */ export function widenE() { return ( /** * @tsplus macro identity */ (self: Either): Either => self ) } /** * Widen left side `Either[E, A] => Either[E | E1, A]`. * * @tsplus getter Either widenA * @tsplus macro identity */ export function widenA_(self: Either): Either { return self } /** * Widen right side `Either[E, A] => Either[E, A | A1]`. * * @tsplus static Either.Aspects widenA */ export function widenA() { return ( /** * @tsplus macro identity */ (self: Either): Either => self ) } /** * @tsplus operator Either == */ export function equals(a: Either, b: Either) { return Equals.equals(a, b) }