type RawValue = T extends Result ? "Function return value cannot be a Result" : T; type SyncValue = T extends Promise ? "Use Res.wrapAsync when returning a Promise" : T; /** * @see Result */ interface IResult { /** * Returns `true` if this is an {@link Ok} result. */ isOk(): this is Ok; /** * Returns `true` if this is an {@link Err} result. */ isErr(): this is Err; /** * Transforms the success value if this is an {@link Ok} result, using a function * that returns another {@link Result}. * * This method is useful to _flatten_ chained results. * * If this is an {@link Err} result, its error value is preserved unchanged. */ map(fn: (v: V) => Ok): Result; map(fn: (v: V) => Err): Result; map(fn: (v: V) => Result): Result; /** * Transforms the success value if this is an {@link Ok} result, using a raw value. * * Example: * ```ts * const r = getCores().map((n) => n * 3); // Ok(n * 3) * ``` * * If this is an {@link Err} result, its error value is preserved unchanged. */ map(fn: (v: V) => RawValue): Result; /** * Transforms the success value if this is an async {@link Ok} result, using a function * that returns another {@link Result}. * * This method is useful to _flatten_ chained async results. * * If this is an async {@link Err} result, its error value is preserved unchanged. */ mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; /** * Transforms the success value if this is an async {@link Ok} result, using a raw value. * * Example: * ```ts * // getCores(): Promise * // adjustCores(n): Promise * const r = Res.from(getCores()).mapAsync((n) => adjustCores(n)); * // r: AsyncResult * ``` * * If this is an {@link Err} result, its error value is preserved unchanged. */ mapAsync(fn: (v: V) => RawValue | Promise>): AsyncResult; /** * Invokes the `fnv` function with the success value if this is an {@link Ok} result, * or the `fne` function if this is an {@link Err} result. * * The result is returned unchanged. */ tap(fnv: ((v: V) => unknown) | undefined, fne?: (e: E) => unknown): Result; /** * Transforms the error value if this is an {@link Err} result, using a function * that returns another {@link Result}. * * This method is useful for recovering from failures and _flattening_ * chains of results that operate on errors. * * If this is an {@link Ok} result, its success value is preserved unchanged. */ catch(fn: (e: E) => Ok): Result; catch(fn: (e: E) => Err): Result; catch(fn: (e: E) => Result): Result; /** * Replaces the error value with an {@link Ok} result using a raw value. * * Equivalent to writing: * ```ts * result.catch((e) => Res.ok(value)) * ``` * * If this is an {@link Ok} result, its success value is preserved unchanged. */ catch(fn: (e: E) => RawValue): Result; /** * Transforms the error value if this is an async {@link Err} result, using a function * that returns another {@link Result}. * * This method is useful for recovering from failures and _flattening_ * chains of async results that operate on errors. * * If this is an async {@link Ok} result, its success value is preserved unchanged. */ catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; /** * Replaces the error value with an {@link Ok} result using a raw value. * * Equivalent to writing: * ```ts * result.catchAsync((e) => Promise.resolve(Res.ok(value))) * ``` * * If this is an async {@link Ok} result, its success value is preserved unchanged. */ catchAsync(fn: (e: E) => RawValue | Promise>): AsyncResult; /** * Exhaustively handles both the {@link Ok} and {@link Err} result variants. * * Example: * ```ts * const r = result.match( * (v) => // Ok branch * (e) => // Err branch * ) * ``` * * @param ok The function called when the result is `Ok`. * @param err The function called when the result is `Err`. */ match(ok: (v: V) => RV, err: (e: E) => RE): RV | RE; /** * Returns the success value if this is an {@link Ok} result, or throws * if this is an {@link Err} result. * * @throws {Error} If this is an {@link Err} result, which does not have a value. */ unwrap(): V; /** * Returns the success value if this is an {@link Ok} result, or falls back * to the value produced by the given function if this is an {@link Err} result. */ unwrapOr(fn: (e: E) => RV): V | RV; } /** * Represents the result of an operation that may succeed (`Ok`) or fail (`Err`). * * @template V The success value type. * @template E The error value type. */ type Result = Ok | Err; /** * A promise that resolves to the result of an operation that may succeed (`Ok`) or fail (`Err`). * * @template V The success value type. * @template E The error value type. */ type ResultPromise = Promise>; /** * Represents a successful result containing a value of type `V`. * * @template V The success value type. */ declare class Ok implements IResult { /** * The underlying success value, also returned when calling {@link Ok.unwrap}. */ readonly value: V; constructor(value: V); isOk(): this is Ok; isErr(): this is Err; map(fn: (v: V) => Ok): Result; map(fn: (v: V) => Err): Result; map(fn: (v: V) => Result): Result; map(fn: (v: V) => RawValue): Result; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => RawValue | Promise>): AsyncResult; tap(fnv: ((v: V) => unknown) | undefined, fne?: (e: E) => unknown): Result; catch(fn: (e: E) => Ok): Result; catch(fn: (e: E) => Err): Result; catch(fn: (e: E) => Result): Result; catch(fn: (e: E) => RawValue): Result; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => RawValue | Promise>): AsyncResult; match(ok: (v: V) => RV, err: (e: E) => RE): RV | RE; unwrap(): V; unwrapOr(fn: (e: E) => RV): V | RV; } /** * Represents a failed result containing an error of type `E`. * * @template E The error value type. */ declare class Err implements IResult { /** * The underlying error value. */ readonly error: E; constructor(error: E); isOk(): this is Ok; isErr(): this is Err; map(fn: (v: V) => Ok): Result; map(fn: (v: V) => Err): Result; map(fn: (v: V) => Result): Result; map(fn: (v: V) => RawValue): Result; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => Promise>): AsyncResult; mapAsync(fn: (v: V) => RawValue | Promise>): AsyncResult; tap(fn: ((v: V) => unknown) | undefined, fne?: (e: E) => unknown): Result; catch(fn: (e: E) => Ok): Result; catch(fn: (e: E) => Err): Result; catch(fn: (e: E) => Result): Result; catch(fn: (e: E) => RawValue): Result; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => Promise>): AsyncResult; catchAsync(fn: (e: E) => RawValue | Promise>): AsyncResult; match(ok: (v: V) => RV, err: (e: E) => RE): RV | RE; unwrap(): V; unwrapOr(fn: (e: E) => RV): V | RV; } /** * Allows performing asynchronous operations on a {@link Result}. */ interface AsyncResult { /** * Returns the underlying {@link Result} object. */ get(): Promise>; /** * Transforms the success value if this is an async {@link Ok} result, using a function * that returns another {@link Result}. * * This method is useful to _flatten_ chained async results. * * If this is an async {@link Err} result, its error value is preserved unchanged. */ mapAsync(fn: (v: V) => Ok | Promise>): AsyncResult; mapAsync(fn: (v: V) => Err | Promise>): AsyncResult; mapAsync(fn: (v: V) => Result | Promise>): AsyncResult; /** * Transforms the success value if this is an async {@link Ok} result, using a raw value. * * Example: * ```ts * // getCores(): Promise * // adjustCores(n): Promise * const r = Res.from(getCores()).mapAsync((n) => adjustCores(n)); * // r: AsyncResult * ``` * * If this is an {@link Err} result, its error value is preserved unchanged. */ mapAsync(fn: (v: V) => RawValue | Promise>): AsyncResult; /** * Invokes the `fnv` function with the success value if this is an async {@link Ok} result, * or the `fne` function if this is an async {@link Err} result. * * The async result is returned unchanged. */ tapAsync(fnv: ((v: V) => unknown) | undefined, fne?: (e: E) => unknown): AsyncResult; /** * Transforms the error value if this is an async {@link Err} result, using a function * that returns another {@link Result}. * * This method is useful for recovering from failures and _flattening_ * chains of async results that operate on errors. * * If this is an async {@link Ok} result, its success value is preserved unchanged. */ catchAsync(fn: (e: E) => Ok | Promise>): AsyncResult; catchAsync(fn: (e: E) => Err | Promise>): AsyncResult; catchAsync(fn: (e: E) => Result | Promise>): AsyncResult; /** * Replaces the error value with an {@link Ok} result using a raw value. * * Equivalent to writing: * ```ts * result.catchAsync((e) => Promise.resolve(Res.ok(value))) * ``` * * If this is an async {@link Ok} result, its success value is preserved unchanged. */ catchAsync(fn: (e: E) => RawValue | Promise>): AsyncResult; /** * Exhaustively handles both the async {@link Ok} and {@link Err} result variants. * * Example: * ```ts * const r = await result.match( * (v) => // Ok branch * (e) => // Err branch * ) * ``` * * @param ok The function called when the result is `Ok`. * @param err The function called when the result is `Err`. */ matchAsync(ok: (v: V) => RV | Promise, err: (e: E) => RE | Promise): Promise; /** * Returns the success value if this is an async {@link Ok} result, or throws * if this is an async {@link Err} result. * * @throws {Error} If this is an async {@link Err} result, which does not have a value. */ unwrapAsync(): Promise; /** * Returns the success value if this is an async {@link Ok} result, or falls back * to the value produced by the given function if this is an async {@link Err} result. */ unwrapOrAsync(fn: (e: E) => RV | Promise): Promise; } /** * A factory for creating and composing {@link Result|Results}. * * @example * ```ts * const success = Res.ok(42); * const failure = Res.err(new Error("Invalid input")); * ``` */ declare const Res: { /** * Creates a successful {@link Result}. * * @example * ```ts * const result = Res.ok(successRc); * ``` * * @param value The value to wrap. * @returns An {@link Ok} result containing the provided value. */ readonly ok: (value: V) => Ok; /** * Creates a failed {@link Result}. * * @example * ```ts * const result = Res.err(new Error("Invalid input")); * ``` * * @param error The error to wrap. * @returns An {@link Err} result containing the provided error. */ readonly err: (error: E) => Err; /** * Creates an {@link AsyncResult} from a promise that resolves to a {@link Result}. * * Useful for chaining asynchronous result operations without manually awaiting * the underlying promise. * * @example * ```ts * const result = Res.async(fetchUser()); * ``` * * @param promise A promise resolving to a {@link Result}. * @returns An {@link AsyncResult} wrapping the provided promise. */ readonly async: (promise: Promise>) => AsyncResult; /** * Executes a synchronous function and wraps its return value in a {@link Result}. * * @example * ```ts * const result = Res.wrap(() => JSON.parse(jsonStr)); * ``` * * @param fn The synchronous function to execute. * @returns A {@link Result} containing either the returned value or the captured error. */ readonly wrap: (fn: () => SyncValue) => Result, unknown>; /** * Executes a synchronous or asynchronous function and wraps its return value in an {@link AsyncResult}. * * @example * ```ts * const result = Res.wrapAsync(async () => { * const response = await fetch(`/api/user/${userId}`); * return response.json(); * }); * ``` * * @param fn The synchronous or asynchronous function to execute. * @returns An {@link AsyncResult} containing either the resolved value or the captured error. */ readonly wrapAsync: (fn: () => Promise | V) => AsyncResult; }; export { Err, Ok, Res }; export type { AsyncResult, Result, ResultPromise };