interface ErrorConfig { withStackTrace: boolean; } declare namespace Result { /** * Wraps a function with a try catch, creating a new function with the same * arguments but returning `Ok` if successful, `Err` if the function throws * * @param fn function to wrap with ok on success or err on failure * @param errorFn when an error is thrown, this will wrap the error result if provided */ function fromThrowable any, E>(fn: Fn, errorFn?: (e: unknown) => E): (...args: Parameters) => Result, E>; } declare type Result = Ok | Err; declare const ok: (value: T) => Ok; declare const err: (err: E) => Err; declare class Ok { readonly value: T; constructor(value: T); isOk(): this is Ok; isErr(): this is Err; map(f: (t: T) => A): Result; mapErr(_f: (e: E) => U): Result; andThen(f: (t: T) => Result): Result; /** * Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. */ orElse(_f: (e: E) => Result): Result; asyncAndThen(f: (t: T) => ResultAsync): ResultAsync; asyncMap(f: (t: T) => Promise): ResultAsync; unwrapOr(_v: T): T; match(ok: (t: T) => A, _err: (e: E) => A): A; _unsafeUnwrap(_?: ErrorConfig): T; _unsafeUnwrapErr(config?: ErrorConfig): E; } declare class Err { readonly error: E; constructor(error: E); isOk(): this is Ok; isErr(): this is Err; map(_f: (t: T) => A): Result; mapErr(f: (e: E) => U): Result; andThen(_f: (t: T) => Result): Result; /** * Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. */ orElse(f: (e: E) => Result): Result; asyncAndThen(_f: (t: T) => ResultAsync): ResultAsync; asyncMap(_f: (t: T) => Promise): ResultAsync; unwrapOr(v: T): T; match(_ok: (t: T) => A, err: (e: E) => A): A; _unsafeUnwrap(config?: ErrorConfig): T; _unsafeUnwrapErr(_?: ErrorConfig): E; } declare class ResultAsync implements PromiseLike> { private _promise; constructor(res: Promise>); static fromPromise(promise: Promise, errorFn?: (e: unknown) => E): ResultAsync; map(f: (t: T) => A | Promise): ResultAsync; mapErr(f: (e: E) => U | Promise): ResultAsync; andThen(f: (t: T) => Result | ResultAsync): ResultAsync; orElse(f: (e: E) => Result | ResultAsync): ResultAsync; match(ok: (t: T) => A, _err: (e: E) => A): Promise; unwrapOr(t: T): Promise; then(successCallback?: (res: Result) => A | PromiseLike, failureCallback?: (reason: unknown) => B | PromiseLike): PromiseLike; } declare const okAsync: (value: T) => ResultAsync; declare const errAsync: (err: E) => ResultAsync; declare type ExtractOkTypes[]> = { [idx in keyof T]: T[idx] extends Result ? U : never; }; declare type ExtractOkAsyncTypes[]> = { [idx in keyof T]: T[idx] extends ResultAsync ? U : never; }; declare type ExtractErrTypes[]> = { [idx in keyof T]: T[idx] extends Result ? E : never; }; declare type ExtractErrAsyncTypes[]> = { [idx in keyof T]: T[idx] extends ResultAsync ? E : never; }; declare function combine[]>(resultList: T): Result, ExtractErrTypes[number]>; declare function combine[]>(asyncResultList: T): Result, ExtractErrAsyncTypes[number]>; export { Err, Ok, Result, ResultAsync, combine, err, errAsync, ok, okAsync };