/** * Represents the result of an operation that can succeed (`Ok`) or fail (`Err`). * Inspired by Rust's Result type — makes errors explicit in the type system. * * @example * ```ts * function divide(a: number, b: number): Result { * if (b === 0) return Err('Division by zero'); * return Ok(a / b); * } * * const result = divide(10, 2); * result.match({ * ok: (value) => console.log(`Result: ${value}`), * err: (error) => console.error(`Error: ${error}`), * }); * ``` */ export type Result = OkResult | ErrResult; declare class OkResult { readonly value: T; readonly _tag: "Ok"; constructor(value: T); isOk(): this is OkResult; isErr(): this is ErrResult; /** Transform the success value. */ map(fn: (value: T) => U): Result; /** Transform the error value (no-op for Ok). */ mapErr(_fn: (error: E) => F): Result; /** Chain operations that return a Result (flatMap). */ andThen(fn: (value: T) => Result): Result; /** Return this Result if Ok, otherwise return the other. */ orElse(_fn: (error: E) => Result): Result; /** Get the value, or throw if Err. */ unwrap(): T; /** Get the value, or return a default. */ unwrapOr(_defaultValue: T): T; /** Get the value, or compute a default from the error. */ unwrapOrElse(_fn: (error: E) => T): T; /** Get the error, or throw if Ok. */ unwrapErr(): E; /** Pattern match on the Result. */ match(handlers: { ok: (value: T) => U; err: (error: E) => U; }): U; /** Convert to an Option, discarding the error. */ ok(): T | undefined; /** Convert to an Option of the error (returns undefined for Ok). */ err(): E | undefined; /** Apply a function to the value if Ok, used for side effects. */ tap(fn: (value: T) => void): Result; /** Apply a function to the error if Err (no-op for Ok). */ tapErr(_fn: (error: E) => void): Result; /** Convert to a JSON-friendly representation. */ toJSON(): { tag: 'Ok'; value: T; }; toString(): string; } declare class ErrResult { readonly error: E; readonly _tag: "Err"; constructor(error: E); isOk(): this is OkResult; isErr(): this is ErrResult; map(_fn: (value: T) => U): Result; mapErr(fn: (error: E) => F): Result; andThen(_fn: (value: T) => Result): Result; orElse(fn: (error: E) => Result): Result; unwrap(): T; unwrapOr(defaultValue: T): T; unwrapOrElse(fn: (error: E) => T): T; unwrapErr(): E; match(handlers: { ok: (value: T) => U; err: (error: E) => U; }): U; ok(): T | undefined; err(): E | undefined; tap(_fn: (value: T) => void): Result; tapErr(fn: (error: E) => void): Result; toJSON(): { tag: 'Err'; error: E; }; toString(): string; } /** * Create a successful Result. */ export declare function Ok(value: T): Result; /** * Create a failed Result. */ export declare function Err(error: E): Result; /** * Result namespace with static utility methods. */ export declare const ResultUtils: { /** * Wrap a function that may throw into a Result. * * @example * ```ts * const result = Result.from(() => JSON.parse(input)); * ``` */ from(fn: () => T): Result; /** * Wrap a Promise into a ResultAsync. */ fromPromise(promise: Promise): Promise>; /** * Collect an array of Results into a Result of an array. * Returns the first Err encountered, or Ok with all values. */ all(results: Result[]): Result; /** * Check if a value is a Result. */ isResult(value: unknown): value is Result; }; export {}; //# sourceMappingURL=result.d.ts.map