interface ErrorConfig { withStackTrace: boolean; } declare class ResultAsync implements PromiseLike> { private _promise; constructor(res: Promise>); static fromSafePromise(promise: PromiseLike): ResultAsync; static fromPromise(promise: PromiseLike, errorFn: (e: unknown) => E): ResultAsync; static fromThrowable(fn: (...args: A) => Promise, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync; static combine, ...ResultAsync[]]>(asyncResultList: T): CombineResultAsyncs; static combine[]>(asyncResultList: T): CombineResultAsyncs; static combineWithAllErrors, ...ResultAsync[]]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync; static combineWithAllErrors[]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync; map(f: (t: T) => A | Promise): ResultAsync; andThrough(f: (t: T) => Result | ResultAsync): ResultAsync; andTee(f: (t: T) => unknown): ResultAsync; orTee(f: (t: E) => unknown): ResultAsync; mapErr(f: (e: E) => U | Promise): ResultAsync; andThen>(f: (t: T) => R): ResultAsync, InferErrTypes | E>; andThen>(f: (t: T) => R): ResultAsync, InferAsyncErrTypes | E>; andThen(f: (t: T) => Result | ResultAsync): ResultAsync; orElse>(f: (e: E) => R): ResultAsync | T, InferErrTypes>; orElse>(f: (e: E) => R): ResultAsync | T, InferAsyncErrTypes>; orElse(f: (e: E) => Result | ResultAsync): ResultAsync; match(ok: (t: T) => A, _err: (e: E) => B): Promise; unwrapOr(t: A): Promise; /** * @deprecated will be removed in 9.0.0. * * You can use `safeTry` without this method. * @example * ```typescript * safeTry(async function* () { * const okValue = yield* yourResult * }) * ``` * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. */ safeUnwrap(): AsyncGenerator, T>; then(successCallback?: (res: Result) => A | PromiseLike, failureCallback?: (reason: unknown) => B | PromiseLike): PromiseLike; [Symbol.asyncIterator](): AsyncGenerator, T>; } declare function okAsync(value: T): ResultAsync; declare function okAsync(value: void): ResultAsync; declare function errAsync(err: E): ResultAsync; declare function errAsync(err: void): ResultAsync; declare const fromPromise: typeof ResultAsync.fromPromise; declare const fromSafePromise: typeof ResultAsync.fromSafePromise; declare const fromAsyncThrowable: typeof ResultAsync.fromThrowable; declare type CombineResultAsyncs[]> = IsLiteralArray extends 1 ? TraverseAsync> : ResultAsync, ExtractErrAsyncTypes[number]>; declare type CombineResultsWithAllErrorsArrayAsync[]> = IsLiteralArray extends 1 ? TraverseWithAllErrorsAsync> : ResultAsync, ExtractErrAsyncTypes[number][]>; declare type UnwrapAsync = IsLiteralArray extends 1 ? Writable extends [infer H, ...infer Rest] ? H extends PromiseLike ? HI extends Result ? [Dedup, ...UnwrapAsync] : never : never : [] : T extends Array ? A extends PromiseLike ? HI extends Result ? Ok[] : never : never : never; declare type TraverseAsync = IsLiteralArray extends 1 ? Combine extends [infer Oks, infer Errs] ? ResultAsync, MembersToUnion> : never : T extends Array ? Combine, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync, MembersToUnion> : ResultAsync, Errs> : Errs extends unknown[] ? ResultAsync> : ResultAsync : never : never; declare type TraverseWithAllErrorsAsync = TraverseAsync extends ResultAsync ? ResultAsync : never; declare type Writable = T extends ReadonlyArray ? [...T] : T; 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 type InferOkTypes = R extends Result ? T : never; declare type InferErrTypes = R extends Result ? E : never; declare type InferAsyncOkTypes = R extends ResultAsync ? T : never; declare type InferAsyncErrTypes = R extends ResultAsync ? E : never; 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>; function combine, ...Result[]]>(resultList: T): CombineResults; function combine[]>(resultList: T): CombineResults; function combineWithAllErrors, ...Result[]]>(resultList: T): CombineResultsWithAllErrorsArray; function combineWithAllErrors[]>(resultList: T): CombineResultsWithAllErrorsArray; } declare type Result = Ok | Err; declare function ok(value: T): Ok; declare function ok(value: void): Ok; declare function err(err: E): Err; declare function err(err: E): Err; declare function err(err: void): Err; /** * Evaluates the given generator to a Result returned or an Err yielded from it, * whichever comes first. * * This function is intended to emulate Rust's ? operator. * See `/tests/safeTry.test.ts` for examples. * * @param body - What is evaluated. In body, `yield* result` works as * Rust's `result?` expression. * @returns The first occurrence of either an yielded Err or a returned Result. */ declare function safeTry(body: () => Generator, Result>): Result; declare function safeTry, GeneratorReturnResult extends Result>(body: () => Generator): Result, InferErrTypes | InferErrTypes>; /** * Evaluates the given generator to a Result returned or an Err yielded from it, * whichever comes first. * * This function is intended to emulate Rust's ? operator. * See `/tests/safeTry.test.ts` for examples. * * @param body - What is evaluated. In body, `yield* result` and * `yield* resultAsync` work as Rust's `result?` expression. * @returns The first occurrence of either an yielded Err or a returned Result. */ declare function safeTry(body: () => AsyncGenerator, Result>): ResultAsync; declare function safeTry, GeneratorReturnResult extends Result>(body: () => AsyncGenerator): ResultAsync, InferErrTypes | InferErrTypes>; interface IResult { /** * Used to check if a `Result` is an `OK` * * @returns `true` if the result is an `OK` variant of Result */ isOk(): this is Ok; /** * Used to check if a `Result` is an `Err` * * @returns `true` if the result is an `Err` variant of Result */ isErr(): this is Err; /** * Maps a `Result` to `Result` * by applying a function to a contained `Ok` value, leaving an `Err` value * untouched. * * @param f The function to apply an `OK` value * @returns the result of applying `f` or an `Err` untouched */ map(f: (t: T) => A): 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. * * @param f a function to apply to the error `Err` value */ mapErr(f: (e: E) => U): Result; /** * Similar to `map` Except you must return a new `Result`. * * This is useful for when you need to do a subsequent computation using the * inner `T` value, but that computation might fail. * Additionally, `andThen` is really useful as a tool to flatten a * `Result, E1>` into a `Result` (see example below). * * @param f The function to apply to the current value */ andThen>(f: (t: T) => R): Result, InferErrTypes | E>; andThen(f: (t: T) => Result): Result; /** * This "tee"s the current value to an passed-in computation such as side * effect functions but still returns the same current value as the result. * * This is useful when you want to pass the current result to your side-track * work such as logging but want to continue main-track work after that. * This method does not care about the result of the passed in computation. * * @param f The function to apply to the current value */ andTee(f: (t: T) => unknown): Result; /** * This "tee"s the current `Err` value to an passed-in computation such as side * effect functions but still returns the same `Err` value as the result. * * This is useful when you want to pass the current `Err` value to your side-track * work such as logging but want to continue error-track work after that. * This method does not care about the result of the passed in computation. * * @param f The function to apply to the current `Err` value */ orTee(f: (t: E) => unknown): Result; /** * Similar to `andTee` except error result of the computation will be passed * to the downstream in case of an error. * * This version is useful when you want to make side-effects but in case of an * error, you want to pass the error to the downstream. * * @param f The function to apply to the current value */ andThrough>(f: (t: T) => R): Result | E>; andThrough(f: (t: T) => Result): Result; /** * Takes an `Err` value and maps it to a `Result`. * * This is useful for error recovery. * * * @param f A function to apply to an `Err` value, leaving `Ok` values * untouched. */ orElse>(f: (e: E) => R): Result | T, InferErrTypes>; orElse(f: (e: E) => Result): Result; /** * Similar to `map` Except you must return a new `Result`. * * This is useful for when you need to do a subsequent async computation using * the inner `T` value, but that computation might fail. Must return a ResultAsync * * @param f The function that returns a `ResultAsync` to apply to the current * value */ asyncAndThen(f: (t: T) => ResultAsync): ResultAsync; /** * Maps a `Result` to `ResultAsync` * by applying an async function to a contained `Ok` value, leaving an `Err` * value untouched. * * @param f An async function to apply an `OK` value */ asyncMap(f: (t: T) => Promise): ResultAsync; /** * Unwrap the `Ok` value, or return the default if there is an `Err` * * @param v the default value to return if there is an `Err` */ unwrapOr(v: A): T | A; /** * * Given 2 functions (one for the `Ok` variant and one for the `Err` variant) * execute the function that matches the `Result` variant. * * Match callbacks do not necessitate to return a `Result`, however you can * return a `Result` if you want to. * * `match` is like chaining `map` and `mapErr`, with the distinction that * with `match` both functions must have the same return type. * * @param ok * @param err */ match(ok: (t: T) => A, err: (e: E) => B): A | B; /** * @deprecated will be removed in 9.0.0. * * You can use `safeTry` without this method. * @example * ```typescript * safeTry(function* () { * const okValue = yield* yourResult * }) * ``` * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`. */ safeUnwrap(): Generator, T>; /** * **This method is unsafe, and should only be used in a test environments** * * Takes a `Result` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object. * * @param config */ _unsafeUnwrap(config?: ErrorConfig): T; /** * **This method is unsafe, and should only be used in a test environments** * * takes a `Result` and returns a `E` when the result is an `Err`, * otherwise it throws a custom object. * * @param config */ _unsafeUnwrapErr(config?: ErrorConfig): E; } declare class Ok implements IResult { 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) => R): Result, InferErrTypes | E>; andThen(f: (t: T) => Result): Result; andThrough>(f: (t: T) => R): Result | E>; andThrough(f: (t: T) => Result): Result; andTee(f: (t: T) => unknown): Result; orTee(_f: (t: E) => unknown): Result; orElse>(_f: (e: E) => R): Result | T, InferErrTypes>; orElse(_f: (e: E) => Result): Result; asyncAndThen(f: (t: T) => ResultAsync): ResultAsync; asyncAndThrough>(f: (t: T) => R): ResultAsync | E>; asyncAndThrough(f: (t: T) => ResultAsync): ResultAsync; asyncMap(f: (t: T) => Promise): ResultAsync; unwrapOr(_v: A): T | A; match(ok: (t: T) => A, _err: (e: E) => B): A | B; safeUnwrap(): Generator, T>; _unsafeUnwrap(_?: ErrorConfig): T; _unsafeUnwrapErr(config?: ErrorConfig): E; [Symbol.iterator](): Generator, T>; } declare class Err implements IResult { 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; andThrough(_f: (t: T) => Result): Result; andTee(_f: (t: T) => unknown): Result; orTee(f: (t: E) => unknown): Result; andThen>(_f: (t: T) => R): Result, InferErrTypes | E>; andThen(_f: (t: T) => Result): Result; orElse>(f: (e: E) => R): Result | T, InferErrTypes>; orElse(f: (e: E) => Result): Result; asyncAndThen(_f: (t: T) => ResultAsync): ResultAsync; asyncAndThrough(_f: (t: T) => ResultAsync): ResultAsync; asyncMap(_f: (t: T) => Promise): ResultAsync; unwrapOr(v: A): T | A; match(_ok: (t: T) => A, err: (e: E) => B): A | B; safeUnwrap(): Generator, T>; _unsafeUnwrap(config?: ErrorConfig): T; _unsafeUnwrapErr(_?: ErrorConfig): E; [Symbol.iterator](): Generator, T>; } declare const fromThrowable: typeof Result.fromThrowable; declare type Prev = [ never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, ...0[] ]; declare type CollectResults = [ Depth ] extends [never] ? [] : T extends [infer H, ...infer Rest] ? H extends Result ? CollectResults : never : Collected; declare type Transpose = A extends [infer T, ...infer Rest] ? T extends [infer L, infer R] ? Transposed extends [infer PL, infer PR] ? PL extends unknown[] ? PR extends unknown[] ? Transpose : never : never : Transpose : Transposed : Transposed; declare type Combine = Transpose, [], Depth> extends [ infer L, infer R ] ? [UnknownMembersToNever, UnknownMembersToNever] : Transpose, [], Depth> extends [] ? [[], []] : never; declare type Dedup = T extends Result ? [unknown] extends [RL] ? Err : Ok : T; declare type MemberListOf = ((T extends unknown ? (t: T) => T : never) extends infer U ? (U extends unknown ? (u: U) => unknown : never) extends (v: infer V) => unknown ? V : never : never) extends (_: unknown) => infer W ? [...MemberListOf>, W] : []; declare type EmptyArrayToNever = T extends [] ? never : NeverArrayToNever extends 1 ? T extends [never, ...infer Rest] ? [EmptyArrayToNever] extends [never] ? never : T : T : T; declare type UnknownMembersToNever = T extends [infer H, ...infer R] ? [[unknown] extends [H] ? never : H, ...UnknownMembersToNever] : T; declare type MembersToUnion = T extends unknown[] ? T[number] : never; declare type IsLiteralArray = T extends { length: infer L; } ? L extends number ? number extends L ? 0 : 1 : 0 : 0; declare type Traverse = Combine extends [infer Oks, infer Errs] ? Result, MembersToUnion> : never; declare type TraverseWithAllErrors = Traverse extends Result ? Result : never; declare type CombineResults[]> = IsLiteralArray extends 1 ? Traverse : Result, ExtractErrTypes[number]>; declare type CombineResultsWithAllErrorsArray[]> = IsLiteralArray extends 1 ? TraverseWithAllErrors : Result, ExtractErrTypes[number][]>; export { Err, Ok, Result, ResultAsync, err, errAsync, fromAsyncThrowable, fromPromise, fromSafePromise, fromThrowable, ok, okAsync, safeTry };