import { Option } from './option.js'; interface BaseResult extends Iterable ? U : never> { /** `true` when the result is Ok */ readonly ok: boolean; /** `true` when the result is Err */ readonly err: boolean; /** * Returns the contained `Ok` value, if exists. Throws an error if not. * @param msg the message to throw if no Ok value. */ expect(msg: string): T; /** * Returns the contained `Err` value, if exists. Throws an error if not. * @param msg the message to throw if no Err value. */ expectErr(msg: string): E; /** * Returns the contained `Ok` value. * Because this function may throw, its use is generally discouraged. * Instead, prefer to handle the `Err` case explicitly. * * Throws if the value is an `Err`, with a message provided by the `Err`'s value. */ unwrap(): T; /** * Returns the contained `Ok` value or a provided default. * * @see unwrapOr * @deprecated in favor of unwrapOr */ else(val: T2): T | T2; /** * Returns the contained `Ok` value or a provided default. * * (This is the `unwrap_or` in rust) */ unwrapOr(val: T2): T | T2; /** * Calls `mapper` if the result is `Ok`, otherwise returns the `Err` value of self. * This function can be used for control flow based on `Result` values. */ andThen(mapper: (val: T) => Ok): Result; andThen(mapper: (val: T) => Err): Result; andThen(mapper: (val: T) => Result): Result; andThen(mapper: (val: T) => Result): Result; /** * Maps a `Result` to `Result` by applying a function to a contained `Ok` value, * leaving an `Err` value untouched. * * This function can be used to compose the results of two functions. */ map(mapper: (val: T) => U): Result; /** * Maps a `Result` to `Result` by applying a function to a contained `Err` value, * leaving an `Ok` value untouched. * * This function can be used to pass through a successful result while handling an error. */ mapErr(mapper: (val: E) => F): Result; /** * Maps a `Result` to `Result` by either converting `T` to `U` using `mapper` * (in case of `Ok`) or using the `default_` value (in case of `Err`). * * If `default` is a result of a function call consider using `mapOrElse` instead, it will * only evaluate the function when needed. */ mapOr(default_: U, mapper: (val: T) => U): U; /** * Maps a `Result` to `Result` by either converting `T` to `U` using `mapper` * (in case of `Ok`) or producing a default value using the `default` function (in case of * `Err`). */ mapOrElse(default_: () => U, mapper: (val: T) => U): U; /** * Converts from `Result` to `Option`, discarding the error if any * * Similar to rust's `ok` method */ toOption(): Option; } /** * Contains the error value */ export declare class ErrImpl implements BaseResult { /** An empty Err */ static readonly EMPTY: ErrImpl; readonly ok: false; readonly err: true; readonly val: E; private readonly _stack; [Symbol.iterator](): Iterator; constructor(val: E); /** * @deprecated in favor of unwrapOr * @see unwrapOr */ else(val: T2): T2; unwrapOr(val: T2): T2; expect(msg: string): never; expectErr(_msg: string): E; unwrap(): never; map(_mapper: unknown): Err; andThen(op: unknown): Err; mapErr(mapper: (err: E) => E2): Err; mapOr(default_: U, _mapper: unknown): U; mapOrElse(default_: () => U, _mapper: unknown): U; toOption(): Option; toString(): string; get stack(): string | undefined; } export declare const Err: typeof ErrImpl & ((err: E) => Err); export declare type Err = ErrImpl; /** * Contains the success value */ export declare class OkImpl implements BaseResult { static readonly EMPTY: OkImpl; readonly ok: true; readonly err: false; readonly val: T; /** * Helper function if you know you have an Ok and T is iterable */ [Symbol.iterator](): Iterator ? U : never>; constructor(val: T); /** * @see unwrapOr * @deprecated in favor of unwrapOr */ else(_val: unknown): T; unwrapOr(_val: unknown): T; expect(_msg: string): T; expectErr(msg: string): never; unwrap(): T; map(mapper: (val: T) => T2): Ok; andThen(mapper: (val: T) => Ok): Ok; andThen(mapper: (val: T) => Err): Result; andThen(mapper: (val: T) => Result): Result; mapErr(_mapper: unknown): Ok; mapOr(_default_: U, mapper: (val: T) => U): U; mapOrElse(_default_: () => U, mapper: (val: T) => U): U; toOption(): Option; /** * Returns the contained `Ok` value, but never throws. * Unlike `unwrap()`, this method doesn't throw and is only callable on an Ok * * Therefore, it can be used instead of `unwrap()` as a maintainability safeguard * that will fail to compile if the error type of the Result is later changed to an error that can actually occur. * * (this is the `into_ok()` in rust) */ safeUnwrap(): T; toString(): string; } export declare const Ok: typeof OkImpl & ((val: T) => Ok); export declare type Ok = OkImpl; export declare type Result = Ok | Err; export declare type ResultOkType> = T extends Ok ? U : never; export declare type ResultErrType = T extends Err ? U : never; export declare type ResultOkTypes[]> = { [key in keyof T]: T[key] extends Result ? ResultOkType : never; }; export declare type ResultErrTypes[]> = { [key in keyof T]: T[key] extends Result ? ResultErrType : never; }; export declare namespace Result { /** * Parse a set of `Result`s, returning an array of all `Ok` values. * Short circuits with the first `Err` found, if any */ function all[]>(...results: T): Result, ResultErrTypes[number]>; /** * Parse a set of `Result`s, short-circuits when an input value is `Ok`. * If no `Ok` is found, returns an `Err` containing the collected error values */ function any[]>(...results: T): Result[number], ResultErrTypes>; /** * Wrap an operation that may throw an Error (`try-catch` style) into checked exception style * @param op The operation function */ function wrap(op: () => T): Result; /** * Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style * @param op The operation function */ function wrapAsync(op: () => Promise): Promise>; function isResult(val: unknown): val is Result; } export {}; //# sourceMappingURL=result.d.ts.map