resultar 2.0.0 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,624 @@
1
- import { S as unit, _ as err, a as fromPromise, b as safeTry, c as okAsync, d as DisposableResult, f as ErrResult, g as StrictResult, h as ResultOperations, i as errAsync, l as tryCatchAsync, m as Result, n as ResultAsync, o as fromSafePromise, p as OkResult, r as StrictResultAsync, s as fromThrowableAsync, t as DisposableResultAsync, u as unitAsync, v as fromThrowable, x as tryCatch, y as ok } from "./result-async-CxGBicja.js";
2
-
1
+ //#region src/error.d.ts
2
+ interface ErrorConfig {
3
+ withStackTrace: boolean;
4
+ }
5
+ //#endregion
6
+ //#region src/pipe.d.ts
7
+ type PipeFn<Input, Output> = (input: Input) => Output;
8
+ declare abstract class Pipeable {
9
+ pipe<A>(ab: PipeFn<this, A>): A;
10
+ pipe<A, B>(ab: PipeFn<this, A>, bc: PipeFn<A, B>): B;
11
+ pipe<A, B, C>(ab: PipeFn<this, A>, bc: PipeFn<A, B>, cd: PipeFn<B, C>): C;
12
+ }
13
+ //#endregion
14
+ //#region src/utils.d.ts
15
+ type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never };
16
+ type ExtractOkAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never };
17
+ type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never };
18
+ type ExtractErrAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never };
19
+ type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never;
20
+ type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never;
21
+ type InferAsyncOkTypes<R> = R extends ResultAsync<infer T, unknown> ? T : never;
22
+ type InferAsyncErrTypes<R> = R extends ResultAsync<unknown, infer E> ? E : never;
23
+ //#endregion
24
+ //#region src/tagged-types.d.ts
25
+ type TaggedValue = {
26
+ readonly _tag: string;
27
+ };
28
+ type TaggedErrorValue = Error & TaggedValue;
29
+ type TagsOf<E> = Extract<E, TaggedValue>["_tag"];
30
+ type ErrorTagsOf<E> = Extract<E, TaggedErrorValue>["_tag"];
31
+ type ErrorForTag$1<E, Tag extends string> = Extract<E, {
32
+ readonly _tag: Tag;
33
+ }>;
34
+ type ExcludeTag<E, Tag extends string> = Exclude<E, {
35
+ readonly _tag: Tag;
36
+ }>;
37
+ type UntaggedError$1<E> = Exclude<Extract<E, Error>, TaggedErrorValue>;
38
+ type CatchTagHandlerResult<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
39
+ type MatchTagHandlerResult<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
40
+ type CatchTagHandlers<E, Handlers, R> = { readonly [Tag in keyof Handlers]: Tag extends TagsOf<E> ? (error: ErrorForTag$1<E, Tag & string>) => R : never };
41
+ type ResultCatchTagHandlers<E, Handlers> = CatchTagHandlers<E, Handlers, Result<unknown, unknown>>;
42
+ type ResultAsyncCatchTagHandlers<E, Handlers> = CatchTagHandlers<E, Handlers, Result<unknown, unknown> | ResultAsync<unknown, unknown>>;
43
+ type MatchTagHandlers<E, Handlers> = { readonly [Tag in ErrorTagsOf<E>]: (error: ErrorForTag$1<E, Tag>) => unknown } & ([UntaggedError$1<E>] extends [never] ? {
44
+ readonly Error?: (error: Error) => unknown;
45
+ } : {
46
+ readonly Error: (error: UntaggedError$1<E>) => unknown;
47
+ }) & { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf<E> | "Error" ? Handlers[Tag] : never };
48
+ type PartialMatchTagHandlers<E, Handlers> = { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf<E> ? (error: ErrorForTag$1<E, Tag & string>) => unknown : Tag extends "Error" ? (error: Extract<E, Error>) => unknown : never };
49
+ //#endregion
50
+ //#region src/result.d.ts
51
+ type ResultFinalizer<T, E> = (value: T | undefined, error: E | undefined) => void;
52
+ type MatchOkHandler<T, A> = (value: T) => A;
53
+ type MatchErrorHandler<E, B> = (error: E) => B;
54
+ type ResultCandidate = () => Result<unknown, unknown>;
55
+ type ResultLoopCollected<R extends Result<unknown, unknown>> = Result<readonly InferOkTypes<R>[], InferErrTypes<R>>;
56
+ type ResultLoopDiscarded<R extends Result<unknown, unknown>> = Result<void, InferErrTypes<R>>;
57
+ type ResultIterated<State, R extends Result<State, unknown>> = Result<State, InferErrTypes<R>>;
58
+ type ResultForEachCollected<R extends Result<unknown, unknown>> = Result<readonly InferOkTypes<R>[], InferErrTypes<R>>;
59
+ type ResultForEachDiscarded<R extends Result<unknown, unknown>> = Result<void, InferErrTypes<R>>;
60
+ type ResultValidatedAll<R extends Result<unknown, unknown>> = Result<readonly InferOkTypes<R>[], InferErrTypes<R>[]>;
61
+ type ResultZipped<Left extends Result<unknown, unknown>, Right extends Result<unknown, unknown>> = Result<[InferOkTypes<Left>, InferOkTypes<Right>], InferErrTypes<Left> | InferErrTypes<Right>>;
62
+ type ResultBooleanCondition = boolean | (() => boolean);
63
+ type ResultIf<ConditionErr, OnTrue extends Result<unknown, unknown>, OnFalse extends Result<unknown, unknown>> = Result<InferOkTypes<OnTrue> | InferOkTypes<OnFalse>, ConditionErr | InferErrTypes<OnTrue> | InferErrTypes<OnFalse>>;
64
+ type ResultWhen<R extends Result<unknown, unknown>> = Result<InferOkTypes<R> | undefined, InferErrTypes<R>>;
65
+ type ResultWhenWithCondition<Condition extends Result<boolean, unknown>, R extends Result<unknown, unknown>> = Result<InferOkTypes<R> | undefined, InferErrTypes<Condition> | InferErrTypes<R>>;
66
+ type WidenLiteral$1<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T;
67
+ interface MatchHandlers$1<T, E, A, B = A> {
68
+ readonly error: MatchErrorHandler<E, B>;
69
+ readonly ok: MatchOkHandler<T, A>;
70
+ }
71
+ type FirstSuccessOf<Candidates extends Iterable<ResultCandidate>> = Result<InferOkTypes<ReturnType<IterableElement$1<Candidates>>>, InferErrTypes<ReturnType<IterableElement$1<Candidates>>>>;
72
+ type IterableElement$1<T> = T extends Iterable<infer Element> ? Element : never;
73
+ interface ResultLoopOptions<State, BodyState extends State, R extends Result<unknown, unknown>> {
74
+ readonly body: (state: BodyState) => R;
75
+ readonly discard?: false;
76
+ readonly step: (state: BodyState) => State;
77
+ readonly while: (state: State) => state is BodyState;
78
+ }
79
+ interface ResultLoopBooleanOptions<State, R extends Result<unknown, unknown>> {
80
+ readonly body: (state: State) => R;
81
+ readonly discard?: false;
82
+ readonly step: (state: State) => State;
83
+ readonly while: (state: State) => boolean;
84
+ }
85
+ interface ResultLoopDiscardOptions<State, BodyState extends State, R extends Result<unknown, unknown>> {
86
+ readonly body: (state: BodyState) => R;
87
+ readonly discard: true;
88
+ readonly step: (state: BodyState) => State;
89
+ readonly while: (state: State) => state is BodyState;
90
+ }
91
+ interface ResultLoopBooleanDiscardOptions<State, R extends Result<unknown, unknown>> {
92
+ readonly body: (state: State) => R;
93
+ readonly discard: true;
94
+ readonly step: (state: State) => State;
95
+ readonly while: (state: State) => boolean;
96
+ }
97
+ interface ResultIterateOptions<State, BodyState extends State, R extends Result<State, unknown>> {
98
+ readonly body: (state: BodyState) => R;
99
+ readonly while: (state: State) => state is BodyState;
100
+ }
101
+ interface ResultIterateBooleanOptions<State, R extends Result<State, unknown>> {
102
+ readonly body: (state: State) => R;
103
+ readonly while: (state: State) => boolean;
104
+ }
105
+ interface ResultForEachOptions {
106
+ readonly discard?: false;
107
+ }
108
+ interface ResultForEachDiscardOptions {
109
+ readonly discard: true;
110
+ }
111
+ interface ResultIfOptions<OnTrue extends Result<unknown, unknown>, OnFalse extends Result<unknown, unknown>> {
112
+ readonly onFalse: () => OnFalse;
113
+ readonly onTrue: () => OnTrue;
114
+ }
115
+ /**
116
+ * A Result interface that is used to define the methods that are available on a
117
+ * `Result` object.
118
+ *
119
+ * The immutability of the `Result` object is maintained by the interface.
120
+ */
121
+ interface Resultable<T, E> {
122
+ get value(): T;
123
+ get error(): E;
124
+ /**
125
+ * Used to check if a `Result` is an `OK`
126
+ *
127
+ * @returns `true` if the result is an `OK` variant of Result
128
+ */
129
+ isOk(): boolean;
130
+ /**
131
+ * Used to check if a `Result` is an `Err`
132
+ *
133
+ * @returns `true` if the result is an `Err` variant of Result
134
+ */
135
+ isErr(): boolean;
136
+ /**
137
+ * Unwrap the `Ok` value, or return the default if there is an `Err`
138
+ *
139
+ * @param v the default value to return if there is an `Err`
140
+ */
141
+ unwrapOr<A>(defaultValue: A): T | A;
142
+ /**
143
+ * **This method is unsafe, and should only be used in a test environments**
144
+ *
145
+ * Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
146
+ *
147
+ * @param config
148
+ */
149
+ _unsafeUnwrap(config?: ErrorConfig): T;
150
+ /**
151
+ * **This method is unsafe, and should only be used in a test environments**
152
+ *
153
+ * takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
154
+ * otherwise it throws a custom object.
155
+ *
156
+ * @param config
157
+ */
158
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
159
+ }
160
+ interface ResultOperations<T, E> {
161
+ isOk(): this is OkResult<T, E>;
162
+ isErr(): this is ErrResult<T, E>;
163
+ unwrapOr<A>(defaultValue: A): T | A;
164
+ _unsafeUnwrap(config?: ErrorConfig): T;
165
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
166
+ map<X>(f: (t: T) => X): Result<X, E>;
167
+ as<X>(value: X): Result<X, E>;
168
+ filterOrElse<U extends T, F>(predicate: (value: T) => value is U, onFalse: (value: T) => F): Result<U, E | F>;
169
+ filterOrElse<F>(predicate: (value: T) => boolean, onFalse: (value: T) => F): Result<T, E | F>;
170
+ mapErr<X>(fn: (t: E) => X): Result<T, X>;
171
+ andThen<X, Y>(f: (t: T) => Result<X, Y>): Result<X, E | Y>;
172
+ if(fCondition: (t: T) => boolean): {
173
+ true: <X1, Y1>(fTrue: (t: T) => Result<X1, Y1>) => {
174
+ false: <X2, Y2>(fFalse: (t: T) => Result<X2, Y2>) => Result<X1 | X2, Y1 | Y2 | E>;
175
+ };
176
+ };
177
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): Result<InferOkTypes<R> | T, InferErrTypes<R>>;
178
+ catchTag<const Tag extends TagsOf<E>, R extends Result<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag$1<E, Tag>) => R): Result<T | InferOkTypes<R>, ExcludeTag<E, Tag> | InferErrTypes<R>>;
179
+ catchTags<const Handlers extends object>(handlers: Handlers & ResultCatchTagHandlers<E, Handlers>): Result<T | InferOkTypes<CatchTagHandlerResult<Handlers>>, ExcludeTag<E, keyof Handlers & string> | InferErrTypes<CatchTagHandlerResult<Handlers>>>;
180
+ asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
181
+ asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
182
+ match<A, B = A>(handlers: MatchHandlers$1<T, E, A, B>): A | B;
183
+ match<A, B = A>(fnOk: MatchOkHandler<T, A>, fnErr: MatchErrorHandler<E, B>): A | B;
184
+ matchTags<A, const Handlers extends object>(fnOk: (t: T) => A, handlers: Handlers & MatchTagHandlers<E, Handlers>): A | MatchTagHandlerResult<Handlers>;
185
+ matchTagsPartial<A, B, const Handlers extends object>(fnOk: (t: T) => A, handlers: Handlers & PartialMatchTagHandlers<E, Handlers>, fallback: (error: E) => B): A | MatchTagHandlerResult<Handlers> | B;
186
+ pipe<A>(ab: PipeFn<Result<T, E>, A>): A;
187
+ pipe<A, B>(ab: PipeFn<Result<T, E>, A>, bc: PipeFn<A, B>): B;
188
+ pipe<A, B, C>(ab: PipeFn<Result<T, E>, A>, bc: PipeFn<A, B>, cd: PipeFn<B, C>): C;
189
+ log(fn: (t?: T, e?: E) => void): this;
190
+ tap(fn: (t: T) => void): this;
191
+ tapError(fn: (e: E) => void): this;
192
+ toDisposable(f: ResultFinalizer<T, E>): DisposableResult<T, E>;
193
+ unwrapOrThrow(): T;
194
+ [Symbol.iterator](): Generator<Result<never, E>, T>;
195
+ safeUnwrap(): Generator<Result<never, E>, T>;
196
+ }
197
+ interface OkResult<T, E> extends ResultOperations<T, E> {
198
+ readonly value: T;
199
+ }
200
+ interface ErrResult<T, E> extends ResultOperations<T, E> {
201
+ readonly error: E;
202
+ }
203
+ type Result<T, E> = OkResult<T, E> | ErrResult<T, E>;
204
+ type StrictResult<T, E extends Error = Error> = Result<T, E>;
205
+ interface TryResultOptions<T, E = unknown> {
206
+ readonly try: () => Exclude<T, Promise<unknown>>;
207
+ readonly catch?: (e: unknown) => E;
208
+ }
209
+ interface SafeTryOptions<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>, CatchErr = never> {
210
+ readonly try: () => Generator<YieldErr, GeneratorReturnResult>;
211
+ readonly catch?: (e: unknown) => CatchErr;
212
+ }
213
+ interface SafeTryAsyncOptions<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>, CatchErr = never> {
214
+ readonly try: () => AsyncGenerator<YieldErr, GeneratorReturnResult>;
215
+ readonly catch?: (e: unknown) => CatchErr;
216
+ }
217
+ interface ResultStatic {
218
+ [Symbol.hasInstance](value: unknown): boolean;
219
+ /**
220
+ * Compatibility alias. Prefer the top-level `tryResult` helper in new code.
221
+ */
222
+ tryCatch<T, E>(this: void, fn: () => Exclude<T, Promise<unknown>>, errorFn?: (e: unknown) => E): Result<T, E>;
223
+ fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(this: void, fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
224
+ ok<T, E = never>(this: void, value: T): OkResult<T, E>;
225
+ ok<E = never>(this: void, value: void): OkResult<void, E>;
226
+ unit<E = never>(this: void): Result<undefined, E>;
227
+ err<T = never, E extends string = string>(this: void, err: E): ErrResult<T, E>;
228
+ err<T = never, E = unknown>(this: void, err: E): ErrResult<T, E>;
229
+ err<T = never>(this: void, err: void): ErrResult<T, void>;
230
+ combine<T extends readonly [Result<unknown, unknown>, ...Result<unknown, unknown>[]]>(this: void, resultList: T): CombineResults<T>;
231
+ combine<T extends readonly Result<unknown, unknown>[]>(this: void, resultList: T): CombineResults<T>;
232
+ combineWithAllErrors<T extends readonly [Result<unknown, unknown>, ...Result<unknown, unknown>[]]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
233
+ combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
234
+ validateAll<T extends readonly [Result<unknown, unknown>, ...Result<unknown, unknown>[]]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
235
+ validateAll<T extends readonly Result<unknown, unknown>[]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
236
+ validateAll<Item, R extends Result<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R): ResultValidatedAll<R>;
237
+ zip<Left extends Result<unknown, unknown>, Right extends Result<unknown, unknown>>(this: void, left: Left, right: Right): ResultZipped<Left, Right>;
238
+ firstSuccessOf<Candidates extends Iterable<ResultCandidate>>(this: void, candidates: Candidates): FirstSuccessOf<Candidates>;
239
+ if<OnTrue extends Result<unknown, unknown>, OnFalse extends Result<unknown, unknown>>(this: void, condition: ResultBooleanCondition, options: ResultIfOptions<OnTrue, OnFalse>): ResultIf<never, OnTrue, OnFalse>;
240
+ if<Condition extends Result<boolean, unknown>, OnTrue extends Result<unknown, unknown>, OnFalse extends Result<unknown, unknown>>(this: void, condition: Condition, options: ResultIfOptions<OnTrue, OnFalse>): ResultIf<InferErrTypes<Condition>, OnTrue, OnFalse>;
241
+ when<R extends Result<unknown, unknown>>(this: void, condition: ResultBooleanCondition, body: () => R): ResultWhen<R>;
242
+ whenResult<Condition extends Result<boolean, unknown>, R extends Result<unknown, unknown>>(this: void, condition: Condition, body: () => R): ResultWhenWithCondition<Condition, R>;
243
+ unless<R extends Result<unknown, unknown>>(this: void, condition: ResultBooleanCondition, body: () => R): ResultWhen<R>;
244
+ unlessResult<Condition extends Result<boolean, unknown>, R extends Result<unknown, unknown>>(this: void, condition: Condition, body: () => R): ResultWhenWithCondition<Condition, R>;
245
+ loop<State, BodyState extends State, R extends Result<unknown, unknown>>(this: void, initial: State, options: ResultLoopOptions<State, BodyState, R>): ResultLoopCollected<R>;
246
+ loop<State, R extends Result<unknown, unknown>>(this: void, initial: State, options: ResultLoopBooleanOptions<State, R>): ResultLoopCollected<R>;
247
+ loop<State, BodyState extends State, R extends Result<unknown, unknown>>(this: void, initial: State, options: ResultLoopDiscardOptions<State, BodyState, R>): ResultLoopDiscarded<R>;
248
+ loop<State, R extends Result<unknown, unknown>>(this: void, initial: State, options: ResultLoopBooleanDiscardOptions<State, R>): ResultLoopDiscarded<R>;
249
+ iterate<State, BodyState extends WidenLiteral$1<State>, R extends Result<WidenLiteral$1<State>, unknown>>(this: void, initial: State, options: ResultIterateOptions<WidenLiteral$1<State>, BodyState, R>): ResultIterated<WidenLiteral$1<State>, R>;
250
+ iterate<State, R extends Result<WidenLiteral$1<State>, unknown>>(this: void, initial: State, options: ResultIterateBooleanOptions<WidenLiteral$1<State>, R>): ResultIterated<WidenLiteral$1<State>, R>;
251
+ forEach<Item, R extends Result<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R, options?: ResultForEachOptions): ResultForEachCollected<R>;
252
+ forEach<Item, R extends Result<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R, options: ResultForEachDiscardOptions): ResultForEachDiscarded<R>;
253
+ }
254
+ declare const Result: ResultStatic;
255
+ /**
256
+ * A `Disposable` is an object that has a `dispose` method that can be used to
257
+ * clean up resources.
258
+ */
259
+ declare class DisposableResult<T, E> implements Resultable<T, E>, Disposable {
260
+ private disposed;
261
+ private readonly finalizer;
262
+ readonly result: Result<T, E>;
263
+ constructor(result: Result<T, E>, finalizer: ResultFinalizer<T, E>);
264
+ get value(): T;
265
+ get error(): E;
266
+ _unsafeUnwrap(config?: ErrorConfig): T;
267
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
268
+ isOk(): boolean;
269
+ isErr(): boolean;
270
+ unwrapOr<A>(defaultValue: A): T | A;
271
+ [Symbol.dispose](): void;
272
+ }
273
+ /**
274
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
275
+ * whichever comes first.
276
+ *
277
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
278
+ * Rust's ? operator.
279
+ * See `/tests/safeTry.test.ts` for examples.
280
+ *
281
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
282
+ * Rust's `result?` expression.
283
+ * @returns The first occurence of either an yielded Err or a returned Result.
284
+ */
285
+ declare function safeTry<T, E, CatchErr = E>(input: SafeTryOptions<Result<never, E>, Result<T, E>, CatchErr> & {
286
+ readonly catch: (e: unknown) => CatchErr;
287
+ }): Result<T, E | CatchErr>;
288
+ declare function safeTry<T, E>(input: (() => Generator<Result<never, E>, Result<T, E>>) | SafeTryOptions<Result<never, E>, Result<T, E>>): Result<T, E>;
289
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>, CatchErr>(input: SafeTryOptions<YieldErr, GeneratorReturnResult, CatchErr> & {
290
+ readonly catch: (e: unknown) => CatchErr;
291
+ }): Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult> | CatchErr>;
292
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(input: (() => Generator<YieldErr, GeneratorReturnResult>) | SafeTryOptions<YieldErr, GeneratorReturnResult>): Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
293
+ /**
294
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
295
+ * whichever comes first.
296
+ *
297
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
298
+ * Rust's ? operator.
299
+ * See `/tests/safeTry.test.ts` for examples.
300
+ *
301
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()`,
302
+ * `yield* result`, and `yield* resultAsync` work as Rust's `result?` expression.
303
+ * @returns The first occurence of either an yielded Err or a returned Result.
304
+ */
305
+ declare function safeTry<T, E, CatchErr = E>(input: SafeTryAsyncOptions<Result<never, E>, Result<T, E>, CatchErr> & {
306
+ readonly catch: (e: unknown) => CatchErr;
307
+ }): ResultAsync<T, E | CatchErr>;
308
+ declare function safeTry<T, E>(input: (() => AsyncGenerator<Result<never, E>, Result<T, E>>) | SafeTryAsyncOptions<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
309
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>, CatchErr>(input: SafeTryAsyncOptions<YieldErr, GeneratorReturnResult, CatchErr> & {
310
+ readonly catch: (e: unknown) => CatchErr;
311
+ }): ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult> | CatchErr>;
312
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(input: (() => AsyncGenerator<YieldErr, GeneratorReturnResult>) | SafeTryAsyncOptions<YieldErr, GeneratorReturnResult>): ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
313
+ declare function ok<T, E = never>(value: T): OkResult<T, E>;
314
+ declare function ok<E = never>(value: void): OkResult<void, E>;
315
+ declare function err<T = never, E extends string = string>(err: E): ErrResult<T, E>;
316
+ declare function err<T = never, E = unknown>(err: E): ErrResult<T, E>;
317
+ declare function err<T = never>(err: void): ErrResult<T, void>;
318
+ declare const fromThrowable: typeof Result.fromThrowable;
319
+ declare function tryResult<T, E>(fn: () => Exclude<T, Promise<unknown>>, errorFn: (e: unknown) => E): Result<T, E>;
320
+ declare function tryResult<T, E>(options: TryResultOptions<T, E> & {
321
+ readonly catch: (e: unknown) => E;
322
+ }): Result<T, E>;
323
+ declare function tryResult<T>(input: (() => Exclude<T, Promise<unknown>>) | TryResultOptions<T>): Result<T, unknown>;
324
+ declare function unit<E = never>(): OkResult<undefined, E>;
325
+ /** Compatibility alias. Prefer `tryResult` in new code. */
326
+ declare const tryCatch: typeof tryResult;
327
+ 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[]];
328
+ type CollectResults<T, Collected extends unknown[] = [], Depth extends number = 50> = [Depth] extends [never] ? [] : T extends [infer H, ...infer Rest] ? H extends Result<infer L, infer R> ? CollectResults<Rest, [...Collected, [L, R]], Prev[Depth]> : never : Collected;
329
+ type Transpose<A, Transposed extends unknown[][] = [], Depth extends number = 10> = 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<Rest, [[...PL, L], [...PR, R]], Prev[Depth]> : never : never : Transpose<Rest, [[L], [R]], Prev[Depth]> : Transposed : Transposed;
330
+ type Combine<T, Depth extends number = 5> = Transpose<CollectResults<T>, [], Depth> extends [infer L, infer R] ? [UnknownMembersToNever<L>, UnknownMembersToNever<R>] : Transpose<CollectResults<T>, [], Depth> extends [] ? [[], []] : never;
331
+ type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
332
+ type MemberListOf<T> = ((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<Exclude<T, W>>, W] : [];
333
+ type EmptyArrayToNever<T, NeverArrayToNever extends number = 0> = T extends [] ? never : NeverArrayToNever extends 1 ? T extends [never, ...infer Rest] ? [EmptyArrayToNever<Rest>] extends [never] ? never : T : T : T;
334
+ type UnknownMembersToNever<T> = T extends [infer H, ...infer R] ? [[unknown] extends [H] ? never : H, ...UnknownMembersToNever<R>] : T;
335
+ type MembersToUnion<T> = T extends unknown[] ? T[number] : never;
336
+ type IsLiteralArray<T> = T extends {
337
+ length: infer L;
338
+ } ? L extends number ? number extends L ? 0 : 1 : 0 : 0;
339
+ type Traverse<T, Depth extends number = 5> = Combine<T, Depth> extends [infer Oks, infer Errs] ? Result<EmptyArrayToNever<Oks, 1>, MembersToUnion<Errs>> : never;
340
+ type TraverseWithAllErrors<T, Depth extends number = 5> = Traverse<T, Depth> extends Result<infer Oks, infer Errs> ? Result<Oks, Errs[]> : never;
341
+ type CombineResults<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? Traverse<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>;
342
+ type CombineResultsWithAllErrorsArray<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrors<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>;
343
+ //#endregion
344
+ //#region src/result-async.d.ts
345
+ type ResultAsyncFinalizer<T, E> = (value: T | undefined, error: E | undefined) => void | Promise<void>;
346
+ interface TryResultAsyncOptions<T, E = unknown> {
347
+ readonly try: () => Promise<T>;
348
+ readonly catch?: (e: unknown) => E;
349
+ }
350
+ type HandlerOk<R> = InferOkTypes<R> | InferAsyncOkTypes<R>;
351
+ type HandlerErr<R> = InferErrTypes<R> | InferAsyncErrTypes<R>;
352
+ type IterableElement<T> = T extends Iterable<infer Element> ? Element : never;
353
+ type ResultAsyncCandidate = () => ResultAsync<unknown, unknown>;
354
+ type FirstSuccessOfAsync<Candidates extends Iterable<ResultAsyncCandidate>> = ResultAsync<InferAsyncOkTypes<ReturnType<IterableElement<Candidates>>>, InferAsyncErrTypes<ReturnType<IterableElement<Candidates>>>>;
355
+ type ResultAsyncLoopCollected<R extends ResultAsync<unknown, unknown>> = ResultAsync<readonly InferAsyncOkTypes<R>[], InferAsyncErrTypes<R>>;
356
+ type ResultAsyncLoopDiscarded<R extends ResultAsync<unknown, unknown>> = ResultAsync<void, InferAsyncErrTypes<R>>;
357
+ type ResultAsyncIterated<State, R extends ResultAsync<State, unknown>> = ResultAsync<State, InferAsyncErrTypes<R>>;
358
+ type ResultAsyncForEachCollected<R extends ResultAsync<unknown, unknown>> = ResultAsync<readonly InferAsyncOkTypes<R>[], InferAsyncErrTypes<R>>;
359
+ type ResultAsyncForEachDiscarded<R extends ResultAsync<unknown, unknown>> = ResultAsync<void, InferAsyncErrTypes<R>>;
360
+ type ResultAsyncValidatedAll<R extends ResultAsync<unknown, unknown>> = ResultAsync<readonly InferAsyncOkTypes<R>[], InferAsyncErrTypes<R>[]>;
361
+ type ResultAsyncZipped<Left extends ResultAsync<unknown, unknown>, Right extends ResultAsync<unknown, unknown>> = ResultAsync<[InferAsyncOkTypes<Left>, InferAsyncOkTypes<Right>], InferAsyncErrTypes<Left> | InferAsyncErrTypes<Right>>;
362
+ type ResultAsyncBooleanCondition = boolean | (() => boolean);
363
+ type ResultAsyncConditional = Result<unknown, unknown> | ResultAsync<unknown, unknown>;
364
+ type ResultAsyncConditionResult = Result<boolean, unknown> | ResultAsync<boolean, unknown>;
365
+ type ResultAsyncIf<ConditionErr, OnTrue extends ResultAsyncConditional, OnFalse extends ResultAsyncConditional> = ResultAsync<HandlerOk<OnTrue> | HandlerOk<OnFalse>, ConditionErr | HandlerErr<OnTrue> | HandlerErr<OnFalse>>;
366
+ type ResultAsyncWhen<R extends ResultAsyncConditional> = ResultAsync<HandlerOk<R> | undefined, HandlerErr<R>>;
367
+ type ResultAsyncWhenWithCondition<Condition extends ResultAsyncConditionResult, R extends ResultAsyncConditional> = ResultAsync<HandlerOk<R> | undefined, HandlerErr<Condition> | HandlerErr<R>>;
368
+ type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T;
369
+ interface ResultAsyncLoopOptions<State, BodyState extends State, R extends ResultAsync<unknown, unknown>> {
370
+ readonly body: (state: BodyState) => R;
371
+ readonly discard?: false;
372
+ readonly step: (state: BodyState) => State;
373
+ readonly while: (state: State) => state is BodyState;
374
+ }
375
+ interface ResultAsyncLoopBooleanOptions<State, R extends ResultAsync<unknown, unknown>> {
376
+ readonly body: (state: State) => R;
377
+ readonly discard?: false;
378
+ readonly step: (state: State) => State;
379
+ readonly while: (state: State) => boolean;
380
+ }
381
+ interface ResultAsyncLoopDiscardOptions<State, BodyState extends State, R extends ResultAsync<unknown, unknown>> {
382
+ readonly body: (state: BodyState) => R;
383
+ readonly discard: true;
384
+ readonly step: (state: BodyState) => State;
385
+ readonly while: (state: State) => state is BodyState;
386
+ }
387
+ interface ResultAsyncLoopBooleanDiscardOptions<State, R extends ResultAsync<unknown, unknown>> {
388
+ readonly body: (state: State) => R;
389
+ readonly discard: true;
390
+ readonly step: (state: State) => State;
391
+ readonly while: (state: State) => boolean;
392
+ }
393
+ interface ResultAsyncIterateOptions<State, BodyState extends State, R extends ResultAsync<State, unknown>> {
394
+ readonly body: (state: BodyState) => R;
395
+ readonly while: (state: State) => state is BodyState;
396
+ }
397
+ interface ResultAsyncIterateBooleanOptions<State, R extends ResultAsync<State, unknown>> {
398
+ readonly body: (state: State) => R;
399
+ readonly while: (state: State) => boolean;
400
+ }
401
+ interface ResultAsyncForEachOptions {
402
+ readonly discard?: false;
403
+ }
404
+ interface ResultAsyncForEachDiscardOptions {
405
+ readonly discard: true;
406
+ }
407
+ interface ResultAsyncIfOptions<OnTrue extends ResultAsyncConditional, OnFalse extends ResultAsyncConditional> {
408
+ readonly onFalse: () => OnFalse;
409
+ readonly onTrue: () => OnTrue;
410
+ }
411
+ declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>>, AsyncDisposable {
412
+ private readonly innerPromise;
413
+ private readonly finalizer;
414
+ private disposed;
415
+ constructor(res: Promise<Result<T, E>>, finalizer: ResultAsyncFinalizer<T, E>);
416
+ then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
417
+ [Symbol.asyncDispose](): Promise<void>;
418
+ }
419
+ /**
420
+ * Represents an asynchronous Result type that wraps a Promise of a Result<T, E>.
421
+ * This class provides a way to handle asynchronous operations that may succeed with a value of type T
422
+ * or fail with an error of type E.
423
+ *
424
+ * @template T - The type of the success value
425
+ * @template E - The type of the error value
426
+ *
427
+ * @implements {PromiseLike<Result<T, E>>}
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * // Create a successful async result
432
+ * const okAsync = ResultAsync.okAsync(42);
433
+ *
434
+ * // Create a failed async result
435
+ * const errAsync = ResultAsync.errAsync(new Error("Something went wrong"));
436
+ *
437
+ * // Transform a Promise into a ResultAsync
438
+ * const resultFromPromise = ResultAsync.fromPromise(
439
+ * fetch("https://api.example.com/data"),
440
+ * (error) => new Error(`API call failed: ${error}`)
441
+ * );
442
+ * ```
443
+ *
444
+ * @remarks
445
+ * ResultAsync implements the PromiseLike interface, allowing it to be used with async/await syntax.
446
+ * It provides various utility methods for transforming and combining results, similar to the Result type,
447
+ * but operating in an asynchronous context.
448
+ *
449
+ * The class includes methods for:
450
+ * - Creating ResultAsync instances (okAsync, errAsync, fromPromise)
451
+ * - Transforming values (map, mapErr)
452
+ * - Chaining operations (andThen, orElse)
453
+ * - Error handling (tapError)
454
+ * - Conditional branching (if)
455
+ * - Combining multiple ResultAsync instances (combine, combineWithAllErrors)
456
+ */
457
+ declare class ResultAsync<T, E> extends Pipeable implements PromiseLike<Result<T, E>> {
458
+ /**
459
+ * Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).
460
+ *
461
+ * @param {T} value - The value to be wrapped in a Result.ok.
462
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given value and error type E.
463
+ */
464
+ static okAsync<T, E = never>(this: void, value: T): ResultAsync<T, E>;
465
+ static okAsync<E = never>(this: void, value: void): ResultAsync<void, E>;
466
+ /**
467
+ * Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.
468
+ *
469
+ * @return {ResultAsync<undefined, E>} A ResultAsync instance with undefined as the value type and E as the error type.
470
+ */
471
+ static unitAsync<E = never>(this: void): ResultAsync<undefined, E>;
472
+ /**
473
+ * Returns a ResultAsync instance that is immediately resolved with a Result.err(error).
474
+ *
475
+ * @param {E} error - The error to be wrapped in a Result.err.
476
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given error and value type T.
477
+ */
478
+ static errAsync<T = never, E = unknown>(this: void, err: E): ResultAsync<T, E>;
479
+ static errAsync<T = never>(this: void, err: void): ResultAsync<T, void>;
480
+ /**
481
+ * Creates a ResultAsync from a Promise, catching any errors that occur during its execution.
482
+ *
483
+ * Compatibility alias. Prefer the top-level `tryResultAsync` helper in new code.
484
+ *
485
+ * @param fn - The Promise or a function returning a Promise to be wrapped in a ResultAsync.
486
+ * @param errorFn - Optional function to transform the caught error into a specific error type.
487
+ * If not provided, the original error will be used.
488
+ * @returns A ResultAsync that will resolve to Ok with the promise's value if successful,
489
+ * or Err with either the transformed error (if errorFn is provided) or the original error.
490
+ *
491
+ * New code should use the top-level `tryResultAsync` helper instead.
492
+ */
493
+ static tryCatch<T>(this: void, fn: Promise<T> | (() => Promise<T>)): ResultAsync<T, unknown>;
494
+ static tryCatch<T, E>(this: void, fn: Promise<T> | (() => Promise<T>), errorFn: (e: unknown) => E): ResultAsync<T, E>;
495
+ /**
496
+ * Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
497
+ * based on the provided promise.
498
+ *
499
+ * @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
500
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
501
+ */
502
+ static fromSafePromise<T, E = never>(this: void, promise: PromiseLike<T>): ResultAsync<T, E>;
503
+ /**
504
+ * Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
505
+ * based on the provided promise.
506
+ *
507
+ * @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
508
+ * @param {(e: unknown) => E} errorFn - A function that transforms the error from the promise into the error type E.
509
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
510
+ */
511
+ static fromPromise<T, E>(this: void, promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
512
+ static combine<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(this: void, asyncResultList: T): CombineResultAsyncs<T>;
513
+ static combine<T extends readonly ResultAsync<unknown, unknown>[]>(this: void, asyncResultList: T): CombineResultAsyncs<T>;
514
+ static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
515
+ static combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
516
+ static validateAll<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
517
+ static validateAll<T extends readonly ResultAsync<unknown, unknown>[]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
518
+ static validateAll<Item, R extends ResultAsync<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R): ResultAsyncValidatedAll<R>;
519
+ static zip<Left extends ResultAsync<unknown, unknown>, Right extends ResultAsync<unknown, unknown>>(this: void, left: Left, right: Right): ResultAsyncZipped<Left, Right>;
520
+ static firstSuccessOf<Candidates extends Iterable<ResultAsyncCandidate>>(this: void, candidates: Candidates): FirstSuccessOfAsync<Candidates>;
521
+ static if<OnTrue extends ResultAsyncConditional, OnFalse extends ResultAsyncConditional>(this: void, condition: ResultAsyncBooleanCondition, options: ResultAsyncIfOptions<OnTrue, OnFalse>): ResultAsyncIf<never, OnTrue, OnFalse>;
522
+ static if<Condition extends ResultAsyncConditionResult, OnTrue extends ResultAsyncConditional, OnFalse extends ResultAsyncConditional>(this: void, condition: Condition, options: ResultAsyncIfOptions<OnTrue, OnFalse>): ResultAsyncIf<HandlerErr<Condition>, OnTrue, OnFalse>;
523
+ static when<R extends ResultAsyncConditional>(this: void, condition: ResultAsyncBooleanCondition, body: () => R): ResultAsyncWhen<R>;
524
+ static whenResult<Condition extends ResultAsyncConditionResult, R extends ResultAsyncConditional>(this: void, condition: Condition, body: () => R): ResultAsyncWhenWithCondition<Condition, R>;
525
+ static unless<R extends ResultAsyncConditional>(this: void, condition: ResultAsyncBooleanCondition, body: () => R): ResultAsyncWhen<R>;
526
+ static unlessResult<Condition extends ResultAsyncConditionResult, R extends ResultAsyncConditional>(this: void, condition: Condition, body: () => R): ResultAsyncWhenWithCondition<Condition, R>;
527
+ static loop<State, BodyState extends State, R extends ResultAsync<unknown, unknown>>(this: void, initial: State, options: ResultAsyncLoopOptions<State, BodyState, R>): ResultAsyncLoopCollected<R>;
528
+ static loop<State, R extends ResultAsync<unknown, unknown>>(this: void, initial: State, options: ResultAsyncLoopBooleanOptions<State, R>): ResultAsyncLoopCollected<R>;
529
+ static loop<State, BodyState extends State, R extends ResultAsync<unknown, unknown>>(this: void, initial: State, options: ResultAsyncLoopDiscardOptions<State, BodyState, R>): ResultAsyncLoopDiscarded<R>;
530
+ static loop<State, R extends ResultAsync<unknown, unknown>>(this: void, initial: State, options: ResultAsyncLoopBooleanDiscardOptions<State, R>): ResultAsyncLoopDiscarded<R>;
531
+ static iterate<State, BodyState extends WidenLiteral<State>, R extends ResultAsync<WidenLiteral<State>, unknown>>(this: void, initial: State, options: ResultAsyncIterateOptions<WidenLiteral<State>, BodyState, R>): ResultAsyncIterated<WidenLiteral<State>, R>;
532
+ static iterate<State, R extends ResultAsync<WidenLiteral<State>, unknown>>(this: void, initial: State, options: ResultAsyncIterateBooleanOptions<WidenLiteral<State>, R>): ResultAsyncIterated<WidenLiteral<State>, R>;
533
+ static forEach<Item, R extends ResultAsync<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R, options?: ResultAsyncForEachOptions): ResultAsyncForEachCollected<R>;
534
+ static forEach<Item, R extends ResultAsync<unknown, unknown>>(this: void, items: Iterable<Item>, f: (value: Item, index: number) => R, options: ResultAsyncForEachDiscardOptions): ResultAsyncForEachDiscarded<R>;
535
+ /**
536
+ * Wraps a async function with a try catch, creating a new function with the same
537
+ * arguments but returning `Ok` if successful, `Err` if the function throws
538
+ *
539
+ * @param fn function to wrap with ok on success or err on failure
540
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
541
+ * @returns a new function that returns a `ResultAsync`
542
+ */
543
+ static fromThrowable<A extends readonly unknown[], T, E>(this: void, fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
544
+ private readonly innerPromise;
545
+ constructor(res: Promise<Result<T, E>>);
546
+ then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
547
+ mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
548
+ map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
549
+ as<X>(value: X): ResultAsync<X, E>;
550
+ filterOrElse<U extends T, F>(predicate: (value: T) => value is U, onFalse: (value: T) => F | Promise<F>): ResultAsync<U, E | F>;
551
+ filterOrElse<F>(predicate: (value: T) => boolean | Promise<boolean>, onFalse: (value: T) => F | Promise<F>): ResultAsync<T, E | F>;
552
+ andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
553
+ andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
554
+ andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
555
+ if(fCondition: (t: T) => boolean): {
556
+ true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
557
+ false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
558
+ };
559
+ };
560
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
561
+ orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
562
+ orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
563
+ catchTag<const Tag extends TagsOf<E>, R extends Result<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag$1<E, Tag>) => R): ResultAsync<T | InferOkTypes<R>, ExcludeTag<E, Tag> | InferErrTypes<R>>;
564
+ catchTag<const Tag extends TagsOf<E>, R extends ResultAsync<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag$1<E, Tag>) => R): ResultAsync<T | InferAsyncOkTypes<R>, ExcludeTag<E, Tag> | InferAsyncErrTypes<R>>;
565
+ catchTag<U, F, const Tag extends TagsOf<E>>(tag: Tag, f: (error: ErrorForTag$1<E, Tag>) => Result<U, F> | ResultAsync<U, F>): ResultAsync<T | U, ExcludeTag<E, Tag> | F>;
566
+ catchTags<const Handlers extends object>(handlers: Handlers & ResultAsyncCatchTagHandlers<E, Handlers>): ResultAsync<T | HandlerOk<CatchTagHandlerResult<Handlers>>, ExcludeTag<E, keyof Handlers & string> | HandlerErr<CatchTagHandlerResult<Handlers>>>;
567
+ match<A, B = A>(handlers: MatchHandlers$1<T, E, A, B>): Promise<A | B>;
568
+ match<A, B = A>(ok: (t: T) => A, fnErr: (e: E) => B): Promise<A | B>;
569
+ matchTags<A, const Handlers extends object>(ok: (t: T) => A, handlers: Handlers & MatchTagHandlers<E, Handlers>): Promise<A | MatchTagHandlerResult<Handlers>>;
570
+ matchTagsPartial<A, B, const Handlers extends object>(ok: (t: T) => A, handlers: Handlers & PartialMatchTagHandlers<E, Handlers>, fallback: (error: E) => B): Promise<A | MatchTagHandlerResult<Handlers> | B>;
571
+ unwrapOr<A>(t: A): Promise<T | A>;
572
+ unwrapOrThrow(): Promise<T>;
573
+ /**
574
+ * Runs a side effect for either variant and preserves the original async result.
575
+ *
576
+ * For `Ok`, the callback receives `(value, undefined)`.
577
+ * For `Err`, the callback receives `(undefined, error)`.
578
+ * Callback errors and rejected callback promises are intentionally ignored.
579
+ *
580
+ * Use `map`, `mapErr`, `andThen`, or `orElse` when the callback should
581
+ * transform the result.
582
+ */
583
+ log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
584
+ /**
585
+ * Runs a side effect only for the `Ok` variant and preserves the original async result.
586
+ *
587
+ * Callback errors and rejected callback promises are intentionally ignored.
588
+ * Use `map` or `andThen` when the callback should transform the value.
589
+ */
590
+ tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
591
+ /**
592
+ * Runs a side effect only for the `Err` variant and preserves the original async result.
593
+ *
594
+ * Callback errors and rejected callback promises are intentionally ignored.
595
+ * Use `mapErr` or `orElse` when the callback should transform the error.
596
+ */
597
+ tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
598
+ toAsyncDisposable(f: ResultAsyncFinalizer<T, E>): DisposableResultAsync<T, E>;
599
+ [Symbol.asyncIterator](): AsyncGenerator<Result<never, E>, T>;
600
+ }
601
+ type StrictResultAsync<T, E extends Error = Error> = ResultAsync<T, E>;
602
+ declare const okAsync: typeof ResultAsync.okAsync;
603
+ declare const errAsync: typeof ResultAsync.errAsync;
604
+ declare const fromPromise: typeof ResultAsync.fromPromise;
605
+ declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
606
+ declare const unitAsync: typeof ResultAsync.unitAsync;
607
+ declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
608
+ declare function tryResultAsync<T, E>(fn: Promise<T> | (() => Promise<T>), errorFn: (e: unknown) => E): ResultAsync<T, E>;
609
+ declare function tryResultAsync<T, E>(options: TryResultAsyncOptions<T, E> & {
610
+ readonly catch: (e: unknown) => E;
611
+ }): ResultAsync<T, E>;
612
+ declare function tryResultAsync<T>(input: Promise<T> | (() => Promise<T>) | TryResultAsyncOptions<T>): ResultAsync<T, unknown>;
613
+ /** Compatibility alias. Prefer `tryResultAsync` in new code. */
614
+ declare const tryCatchAsync: typeof tryResultAsync;
615
+ type CombineResultAsyncs<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
616
+ type CombineResultsWithAllErrorsArrayAsync<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>;
617
+ type UnwrapAsync<T> = IsLiteralArray<T> extends 1 ? Writable<T> extends [infer H, ...infer Rest] ? H extends PromiseLike<infer HI> ? HI extends Result<unknown, unknown> ? [Dedup<HI>, ...UnwrapAsync<Rest>] : never : never : [] : T extends (infer A)[] ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Result<L, R>[] : never : never : never;
618
+ type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>> : never : T extends (infer I)[] ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Oks[number][]>, MembersToUnion<Errs[number][]>> : ResultAsync<EmptyArrayToNever<Oks[number][]>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Errs[number][]>> : ResultAsync<Oks, Errs> : never : never;
619
+ type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = TraverseAsync<T, Depth> extends ResultAsync<infer Oks, infer Errs> ? ResultAsync<Oks, Errs[]> : never;
620
+ type Writable<T> = T extends readonly unknown[] ? [...T] : T;
621
+ //#endregion
3
622
  //#region src/tagged-error.d.ts
4
623
  type Alpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "_";
5
624
  type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
@@ -73,5 +692,5 @@ declare function createTaggedError<const Tag extends string, const MessageTempla
73
692
  declare const matchError: <ErrorType extends Error, ReturnType>(error: ErrorType, handlers: MatchHandlers<ErrorType, ReturnType>) => ReturnType;
74
693
  declare const matchErrorPartial: <ErrorType extends Error, ReturnType>(error: ErrorType, handlers: PartialMatchHandlers<ErrorType, ReturnType>, fallback: (error: ErrorType) => ReturnType) => ReturnType;
75
694
  //#endregion
76
- export { DisposableResult, DisposableResultAsync, type ErrResult, type OkResult, Result, ResultAsync, type ResultOperations, type StrictResult, type StrictResultAsync, type TaggedEnum, type TaggedErrorClass, type TaggedErrorInstance, type TaggedErrorOptions, createTaggedError, err, errAsync, findCause, fromPromise, fromSafePromise, fromThrowable, fromThrowableAsync, isError, matchError, matchErrorPartial, ok, okAsync, safeTry, tryCatch, tryCatchAsync, unit, unitAsync };
695
+ export { DisposableResult, DisposableResultAsync, type ErrResult, type OkResult, Result, ResultAsync, type ResultOperations, type SafeTryAsyncOptions, type SafeTryOptions, type StrictResult, type StrictResultAsync, type TaggedEnum, type TaggedErrorClass, type TaggedErrorInstance, type TaggedErrorOptions, type TryResultAsyncOptions, type TryResultOptions, createTaggedError, tryResult as default, tryResult as try, tryResult, err, errAsync, findCause, fromPromise, fromSafePromise, fromThrowable, fromThrowableAsync, isError, matchError, matchErrorPartial, ok, okAsync, safeTry, tryResultAsync as tryAsync, tryResultAsync, tryCatch, tryCatchAsync, unit, unitAsync };
77
696
  //# sourceMappingURL=index.d.ts.map