import type { Atom } from "./atom"; import type { Result } from "./result"; declare const __option__: unique symbol; export type Some = { type: "Some"; value: T; readonly isSome: true; readonly isNone: false; readonly [__option__]: true; }; export type None = { type: "None"; readonly isSome: false; readonly isNone: true; readonly [__option__]: true; }; export type Option = (Some | None) & OptionMethods; /** Values treated as falsy by Option */ export type NonTruthy = null | undefined | ""; /** Methods available on an Option */ export interface OptionMethods { /** Returns the same option */ to(target: "option"): Option; /** Converts `Some` to `Atom`; throws for `None` or non-string */ to(target: "atom"): Atom; /** Converts to `Result`; `None` becomes `Err("Value is None")` */ to(target: "result"): Result), string>; /** * Unwraps the option, throwing if `None`. * @throws Error with provided message or default. * @example * option(42).expect(); // 42 * option("").expect("must be present"); // throws */ expect(msg?: string): T; /** * Returns an unwrap chain that MUST be completed with `.else(...)`. * If `.else(...)` is not chained, an error is thrown ("Expected else"). * - If `Some`, `.else(...)` is required but ignored for outcome; returns the inner value. * - If `None`, `.else(...)` provides fallback; if a function, it is called with `undefined`. * - Fallback result must be truthy; otherwise, throws ("Fallback must be truthy"). */ unwrap(): { /** * Fallback value or transformer to recover from `None`. * - Function form receives `undefined` and must return a truthy value. * - Direct value must be truthy. * Returns the inner value for `Some`, or the validated fallback for `None`. */ else(fallback: T | ((value: T | undefined) => T)): T; }; /** * Transforms the inner value if `Some`, returns `None` if `None`. * - If fn returns `undefined`, returns original Option (no new allocation). * - If fn throws, returns `None`. * - Supports async functions, returning `Promise>`. * @example * option(5).andThen(x => x * 2); // Some(10) * option(null).andThen(x => x * 2); // None * option(5).andThen(x => undefined); // Some(5) - original * await option(5).andThen(async x => x); // Some(5) */ andThen(fn: (value: T) => U | Promise): Option | Promise>; } /** * Checks if a value is falsy for Option purposes. * Falsy: null, undefined, empty string, NaN, Infinity, -Infinity */ export declare const isFalsy: (value: any) => boolean; /** * Creates a new truthy option. * @param value - the value of the option * @throws Error if value is falsy * @example * const a = Some("hello"); * typeof a; // Some<"hello"> */ declare function Some(value: T): Some; /** Singleton None value */ declare const None: None; /** * Sets the _to converter function (called from to.ts to break circular dep). * @internal */ export declare function setToConverter(fn: (value: any, target: string) => any): void; /** * Creates a new option type from a value. * - Truthy values become `Some`; `null|undefined|""` become `None`. * - Provides chainable `.to()`, `.expect()`, and `.unwrap()` helpers. * @example * option("hi").expect(); // "hi" * option("").expect("cannot be empty"); // throws Error("cannot be empty") * option("state").to("atom"); // Atom<"state"> * option(null).to("result"); // Err("Value is None") */ export declare function option(value: T | NonTruthy): Option & OptionMethods; export { Some, None };