export type Ok = { readonly ok: true; readonly value: ValueType; readonly error?: undefined; }; export type Err = { readonly ok: false; readonly error: ErrorType; readonly value?: undefined; }; /** * Union type representing either a successful (Ok) or failed (Err) computation result. * * @typeparam ValueType - The type of the value in a successful result * @typeparam ErrorType - The type of the error in a failed result */ export type Result = Ok | Err; /** * Construct a successful Result containing a value. * * @example * const success = ok(42) * console.log(success.value) // 42 */ export declare function ok(value: T): Ok; /** * Construct a successful Result with undefined value. * * @example * const success = ok() * console.log(success.value) // undefined */ export declare function ok(): Ok; /** * Construct an error Result with a type and payload. * * @example * err('Timeout', { ms: 1000 }) // Err<{ type: 'Timeout', ms: 1000 }> */ export declare function err>(type: K, payload: P): Err<{ type: K; } & P>; /** * Construct an error Result with only a type. * * @example * err('Timeout') // Err<{ type: 'Timeout' }> */ export declare function err(type: K): Err; /** * Construct an error Result from an arbitrary error value (object, string, etc). * Type safety and pattern matching only work if the value is an object with a `type` property. * * @example * err({ message: 'Something failed' }) * err(new Error('fail')) */ export declare function err(payload: K): Err; /** * Helper to embed a cause error payload when constructing a new Err. * * @example * const low = err('Low') * const high = err('High', cause(low)) */ export declare function cause(e: T): { cause: T["error"]; }; export declare const map: (r: Result, fn: (v: ValueType) => NewValue) => Result; export declare const mapErr: (r: Result, fn: (e: ErrorType) => NewError) => Result; export declare const flatMap: (r: Result, fn: (v: ValueType) => Result) => Result; export declare function unwrap(r: Result): ValueType; export declare function unwrap(r: PromiseLike>): Promise; export declare const orElse: (r: Result, fallback: ValueType) => ValueType; export declare function result(work: () => ValueType): Result; export declare function result(work: PromiseLike): Promise>; /** * Pattern match on a Result object (Ok/Err) or a discriminant string. * * Overloads: * - match(result, { ok, err }) * - match(type, { A: fn, B: fn }) */ export declare function match(r: Result, arms: { ok: (v: ValueType) => OnOk; err: (e: ErrorType) => OnErr; }): OnOk | OnErr; export declare function match unknown; }>(value: T, cases: Cases & Record, never>): ReturnType;