import { Failure, Result, Success } from "./result.js"; /** * Discriminated union representing a value that may or may not be present. * * @template T Type of the contained value */ export type Maybe = Result; /** * Represents a present value. */ export type Some = Success; /** * Represents an absent value. */ export type None = Failure; /** * Creates a Some instance containing a value. * * @example * const maybeNumber = Some(42); // Maybe */ export declare const Some: (value: T) => Maybe; /** * Singleton instance representing absence of a value. * * @example * const maybeEmpty = None; // Maybe */ export declare const None: Maybe; export declare namespace Maybe { /** * Type guard for instances of Some. */ const isSome: (m: Maybe) => m is Some; /** * Type guard for instances of None. */ const isNone: (m: Maybe) => m is None; /** * Type guard that checks if a value is a valid Maybe. * * @param value Unknown value to check * @returns true if the value is either Some or None */ const is: (value: unknown) => value is Maybe; /** * Transforms the value inside a Some, leaving None unchanged. * * @example * const numMaybe = Some(42); * const strMaybe = Maybe.map(numMaybe, n => n.toString()); // Some("42") */ const map: (m: Maybe, fn: (v: T) => U) => Maybe; /** * Chains Maybe-producing operations. * * @example * const maybeLength = Maybe.flatMap(Some("hello"), s => Some(s.length)); // Some(5) */ const flatMap: (m: Maybe, fn: (v: T) => Maybe) => Maybe; /** * Converts a nullable value to a Maybe. * * @returns Some(value) if not null/undefined, otherwise None. * * @example * const maybeName: Maybe = Maybe.fromNullable(getNameThatMightReturnNull()); */ const fromNullable: (value: T | null | undefined) => Maybe>; /** * Converts a function's return value to a Maybe. * - Returns the existing Maybe if the function returns one * - Converts null/undefined returns to None * - Wraps other returns in Some * * @param fn Function to execute * @returns Maybe-wrapped result * * @example * const maybeData: Maybe = Maybe.from(() => JSON.parse(localStorage.data)); */ const from: (fn: () => T) => Maybe ? Ok : T extends Failure ? never : T>; const orElse: (m: Maybe, or: () => T) => T; }