export type Left = Readonly<{ isRight: false; left: E; }>; export type Right = Readonly<{ isRight: true; right: A; }>; /** * A value of `Either` type `Left` or type `Right`; a disjoint union. * * A common use case is as an alternative to `Option` where `Left` contains useful * information. Convention dictates that `Left` is used for failure and `Right` for success. * To help remember, the success case is "right"; it's the result you want. * * @example * * * ```ts * import { strictEqual } from "node:assert/strict"; * * import { either as E, pipe } from "@clipboard-health/util-ts"; * * function double(n: number): number { * return n * 2; * } * * function inverse(n: number): E.Either { * return n === 0 ? E.left("Division by zero") : E.right(1 / n); * } * * const result = pipe( * E.right(5), * E.map(double), * E.flatMap(inverse), * E.match( * (error) => `Error: ${error}`, * (r) => `Result is ${r}`, * ), * ); * * strictEqual(result, "Result is 0.1"); * ``` * * */ export type Either = Left | Right; /** * Constructs an `Either` holding a `Left` value, usually representing a failure. * * @param value - The value to wrap in a `Left` * @returns A `Left` containing the value */ export declare function left(value: E): Either; /** * Constructs an `Either` holding a `Right`, representing a success. * * @param value - The value to wrap in a `Right` * @returns A `Right` containing the value */ export declare function right(value: A): Either; /** * Type guard that checks if an either is `Left`. * * @param either - The `Either` to check * @returns `true` if the `Either` is `Left`, `false` if it is `Right` */ export declare function isLeft(either: Either): either is Left; /** * Type guard that checks if an either is `Right`. * * @param either - The `Either` to check * @returns `true` if the `Either` is `Right`, `false` if it is `Left` */ export declare function isRight(either: Either): either is Right; /** * Transforms the value inside an `Either` using the provided function. If the `Either` is * `Right(value)`, returns `Right(f(value))`. If the `Either` is `Left(value)`, returns * `Left(value)`. * * @param f - The function to apply to the `Right` value * @returns A function that transforms `Either` to `Either` */ export declare function map(f: (right: A) => B): (either: Either) => Either; /** * Transforms the value inside an `Either` using the provided function. If the `Either` is * `Left(value)`, returns `Left(f(value))`. If the `Either` is `Right(value)`, returns * `Right(value)`. */ export declare function mapLeft(f: (left: E) => G): (either: Either) => Either; /** * Chains `Either` operations that return `Either`s. Unlike `map` which wraps the result in a new * `Either`, `flatMap` prevents nested `Either`s like `Right(Right(value))`. * * @param f - A function that returns an `Either` * @returns The `Either` returned by the function if the input is `Right`, `Left` otherwise */ export declare function flatMap(f: (right: A) => Either): (either: Either) => Either; /** * Safely extracts the value from an `Either` with a fallback. Use this function when you need to * convert an `Either` to an `A`, providing a default value for the `Left` case. * * @param onLeft - The value to return if the `Either` is `Left` * @returns The contained value if `Right`, the result of `onLeft` if `Left` */ export declare function getOrElse(onLeft: (left: E) => A): (either: Either) => A; /** * Pattern matches on an `Either`, handling both `Right` and `Left` cases. * * @param onLeft - Function to handle the `Left` case * @param onRight - Function to handle the `Right` case * @returns The result of either `onLeft` or `onRight` based on the `Either` state */ export declare function match(onLeft: (left: E) => B, onRight: (right: A) => C): (either: Either) => B | C;