resultar 1.1.3 → 1.1.5

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.
@@ -1,92 +1,3 @@
1
- type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
2
- type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
3
- 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 Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
4
- 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 Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
5
- type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
6
- type Writable<T> = T extends readonly unknown[] ? [...T] : T;
7
- declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
8
- private readonly innerPromise;
9
- constructor(res: Promise<Result<T, E>>);
10
- then<A, B>(// eslint-disable-line unicorn/no-thenable
11
- successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
12
- }
13
- declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
14
- static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
15
- static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
16
- static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
17
- static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
18
- static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
19
- static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
20
- static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
21
- static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
22
- /**
23
- * Wraps a async function with a try catch, creating a new function with the same
24
- * arguments but returning `Ok` if successful, `Err` if the function throws
25
- *
26
- * @param fn function to wrap with ok on success or err on failure
27
- * @param errorFn when an error is thrown, this will wrap the error result if provided
28
- * @returns a new function that returns a `ResultAsync`
29
- */
30
- static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
31
- private readonly innerPromise;
32
- constructor(res: Promise<Result<T, E>>);
33
- then<A, B>(// eslint-disable-line unicorn/no-thenable
34
- successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
35
- mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
36
- map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
37
- andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
38
- andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
39
- andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
40
- if(fCondition: (t: T) => boolean): {
41
- true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
42
- false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
43
- };
44
- };
45
- orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
46
- orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
47
- orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
48
- match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
49
- unwrapOr<A>(t: A): Promise<T | A>;
50
- /**
51
- * Performs a side effect for the `Ok` variant of `ResultAsync`.
52
- * This function can be used for control flow based on result values.
53
- * @param f The function to call if the result is `Ok`
54
- * @returns `self` if the result is `Err`, otherwise the result of `f`
55
- */
56
- tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
57
- /**
58
- * Performs a side effect for the `Err` variant of `ResultAsync`.
59
- * This function can be used for control flow based on result values.
60
- * @param f The function to call if the result is `Err`
61
- * @returns `self` if the result is `Ok`, otherwise the result of `f`
62
- * @example
63
- */
64
- tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
65
- finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
66
- /**
67
- * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
68
- */
69
- safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
70
- }
71
- declare const okAsync: typeof ResultAsync.okAsync;
72
- declare const errAsync: typeof ResultAsync.errAsync;
73
- declare const fromPromise: typeof ResultAsync.fromPromise;
74
- declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
75
- declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
76
- /**
77
- * Evaluates the given generator to a Result returned or an Err yielded from it,
78
- * whichever comes first.
79
- *
80
- * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
81
- * Rust's ? operator.
82
- * See `/tests/safeTry.test.ts` for examples.
83
- *
84
- * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
85
- * Rust's `result?` expression.
86
- * @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
87
- */
88
- declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
89
-
90
1
  type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
91
2
  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] : [];
92
3
  type ExtractOkTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
@@ -194,6 +105,102 @@ declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: Reado
194
105
  */
195
106
  declare const combineResultListWithAllErrors: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E[]>;
196
107
 
108
+ type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
109
+ type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
110
+ 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 Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
111
+ 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 Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
112
+ type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
113
+ type Writable<T> = T extends readonly unknown[] ? [...T] : T;
114
+ declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
115
+ private readonly innerPromise;
116
+ constructor(res: Promise<Result<T, E>>);
117
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
118
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
119
+ }
120
+ declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
121
+ static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
122
+ static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
123
+ static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
124
+ static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
125
+ static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
126
+ static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
127
+ static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
128
+ static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
129
+ /**
130
+ * Wraps a async function with a try catch, creating a new function with the same
131
+ * arguments but returning `Ok` if successful, `Err` if the function throws
132
+ *
133
+ * @param fn function to wrap with ok on success or err on failure
134
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
135
+ * @returns a new function that returns a `ResultAsync`
136
+ */
137
+ static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
138
+ private readonly innerPromise;
139
+ constructor(res: Promise<Result<T, E>>);
140
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
141
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
142
+ mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
143
+ map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
144
+ andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
145
+ andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
146
+ andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
147
+ if(fCondition: (t: T) => boolean): {
148
+ true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
149
+ false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
150
+ };
151
+ };
152
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
153
+ orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
154
+ orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
155
+ match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
156
+ unwrapOr<A>(t: A): Promise<T | A>;
157
+ /**
158
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
159
+ * This function can be used for control flow based on result values.
160
+ * @param f The function to call if the result is `Ok`
161
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
162
+ */
163
+ log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
164
+ /**
165
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
166
+ * This function can be used for control flow based on result values.
167
+ * @param f The function to call if the result is `Ok`
168
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
169
+ */
170
+ tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
171
+ /**
172
+ * Performs a side effect for the `Err` variant of `ResultAsync`.
173
+ * This function can be used for control flow based on result values.
174
+ * @param f The function to call if the result is `Err`
175
+ * @returns `self` if the result is `Ok`, otherwise the result of `f`
176
+ * @example
177
+ */
178
+ tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
179
+ finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
180
+ /**
181
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
182
+ */
183
+ safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
184
+ }
185
+ declare const okAsync: typeof ResultAsync.okAsync;
186
+ declare const errAsync: typeof ResultAsync.errAsync;
187
+ declare const fromPromise: typeof ResultAsync.fromPromise;
188
+ declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
189
+ declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
190
+ /**
191
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
192
+ * whichever comes first.
193
+ *
194
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
195
+ * Rust's ? operator.
196
+ * See `/tests/safeTry.test.ts` for examples.
197
+ *
198
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
199
+ * Rust's `result?` expression.
200
+ * @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
201
+ */
202
+ declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
203
+
197
204
  /**
198
205
  * A Result interface that is used to define the methods that are available on a
199
206
  * `Result` object.
@@ -316,7 +323,6 @@ declare class Result<T, E> implements Resultable<T, E> {
316
323
  *
317
324
  * This is useful for error recovery.
318
325
  *
319
- *
320
326
  * @param f A function to apply to an `Err` value, leaving `Ok` values
321
327
  * untouched.
322
328
  */
@@ -346,7 +352,6 @@ declare class Result<T, E> implements Resultable<T, E> {
346
352
  */
347
353
  unwrapOr<A>(defaultValue: A): T | A;
348
354
  /**
349
- *
350
355
  * Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
351
356
  * execute the function that matches the `Result` variant.
352
357
  *
@@ -360,20 +365,27 @@ declare class Result<T, E> implements Resultable<T, E> {
360
365
  * @param err
361
366
  */
362
367
  match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
368
+ /**
369
+ * Performs a side effect no matter what the `Result` variant is.
370
+ *
371
+ * @param fn The function to apply an `OK` value
372
+ * @returns the result of applying `f` or an `Err` untouched
373
+ */
374
+ log(fn: (t?: T, e?: E) => void): this;
363
375
  /**
364
376
  * Performs a side effect for the `Ok` variant of `Result`.
365
377
  *
366
378
  * @param fn The function to apply an `OK` value
367
379
  * @returns the result of applying `f` or an `Err` untouched
368
380
  */
369
- tap(fn: (t: T) => void): Result<T, E>;
381
+ tap(fn: (t: T) => void): this;
370
382
  /**
371
383
  * Performs a side effect for the `Err` variant of `Result`.
372
384
  *
373
385
  * @param fn The function to apply an `Err` value
374
386
  * @returns the result of applying `f` or an `Ok` untouched
375
387
  */
376
- tapError(fn: (e: E) => void): Result<T, E>;
388
+ tapError(fn: (e: E) => void): this;
377
389
  /**
378
390
  * This method is used to clean up and release any resources that the `Result`
379
391
  * @param f The function that will be called to clean up the resources
@@ -461,4 +473,4 @@ interface ResultarError<T, E> {
461
473
  }
462
474
  declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
463
475
 
464
- export { ok as A, err as B, type CombineResultAsyncs as C, DisposableResultAsync as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type CombineResultsWithAllErrorsArrayAsync as a, fromSafePromise as b, fromThrowableAsync as c, type Dedup as d, errAsync as e, fromPromise as f, type ExtractOkAsyncTypes as g, type ExtractErrTypes as h, type ExtractErrAsyncTypes as i, type InferErrTypes as j, type InferAsyncOkTypes as k, type InferAsyncErrTypes as l, type IsLiteralArray as m, type EmptyArrayToNever as n, okAsync as o, type MembersToUnion as p, type Combine as q, type CombineResults as r, safeTryAsync as s, type CombineResultsWithAllErrorsArray as t, combineResultAsyncList as u, combineResultList as v, combineResultAsyncListWithAllErrors as w, combineResultListWithAllErrors as x, Result as y, DisposableResult as z };
476
+ export { ok as A, err as B, type Combine as C, type Dedup as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type ExtractOkAsyncTypes as a, type ExtractErrTypes as b, type ExtractErrAsyncTypes as c, type InferErrTypes as d, type InferAsyncOkTypes as e, type InferAsyncErrTypes as f, type IsLiteralArray as g, type EmptyArrayToNever as h, type MembersToUnion as i, type CombineResults as j, type CombineResultsWithAllErrorsArray as k, combineResultAsyncList as l, combineResultList as m, combineResultAsyncListWithAllErrors as n, combineResultListWithAllErrors as o, type CombineResultAsyncs as p, type CombineResultsWithAllErrorsArrayAsync as q, DisposableResultAsync as r, okAsync as s, errAsync as t, fromPromise as u, fromSafePromise as v, fromThrowableAsync as w, safeTryAsync as x, Result as y, DisposableResult as z };
@@ -1,92 +1,3 @@
1
- type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
2
- type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
3
- 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 Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
4
- 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 Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
5
- type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
6
- type Writable<T> = T extends readonly unknown[] ? [...T] : T;
7
- declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
8
- private readonly innerPromise;
9
- constructor(res: Promise<Result<T, E>>);
10
- then<A, B>(// eslint-disable-line unicorn/no-thenable
11
- successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
12
- }
13
- declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
14
- static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
15
- static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
16
- static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
17
- static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
18
- static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
19
- static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
20
- static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
21
- static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
22
- /**
23
- * Wraps a async function with a try catch, creating a new function with the same
24
- * arguments but returning `Ok` if successful, `Err` if the function throws
25
- *
26
- * @param fn function to wrap with ok on success or err on failure
27
- * @param errorFn when an error is thrown, this will wrap the error result if provided
28
- * @returns a new function that returns a `ResultAsync`
29
- */
30
- static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
31
- private readonly innerPromise;
32
- constructor(res: Promise<Result<T, E>>);
33
- then<A, B>(// eslint-disable-line unicorn/no-thenable
34
- successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
35
- mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
36
- map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
37
- andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
38
- andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
39
- andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
40
- if(fCondition: (t: T) => boolean): {
41
- true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
42
- false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
43
- };
44
- };
45
- orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
46
- orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
47
- orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
48
- match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
49
- unwrapOr<A>(t: A): Promise<T | A>;
50
- /**
51
- * Performs a side effect for the `Ok` variant of `ResultAsync`.
52
- * This function can be used for control flow based on result values.
53
- * @param f The function to call if the result is `Ok`
54
- * @returns `self` if the result is `Err`, otherwise the result of `f`
55
- */
56
- tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
57
- /**
58
- * Performs a side effect for the `Err` variant of `ResultAsync`.
59
- * This function can be used for control flow based on result values.
60
- * @param f The function to call if the result is `Err`
61
- * @returns `self` if the result is `Ok`, otherwise the result of `f`
62
- * @example
63
- */
64
- tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
65
- finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
66
- /**
67
- * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
68
- */
69
- safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
70
- }
71
- declare const okAsync: typeof ResultAsync.okAsync;
72
- declare const errAsync: typeof ResultAsync.errAsync;
73
- declare const fromPromise: typeof ResultAsync.fromPromise;
74
- declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
75
- declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
76
- /**
77
- * Evaluates the given generator to a Result returned or an Err yielded from it,
78
- * whichever comes first.
79
- *
80
- * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
81
- * Rust's ? operator.
82
- * See `/tests/safeTry.test.ts` for examples.
83
- *
84
- * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
85
- * Rust's `result?` expression.
86
- * @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
87
- */
88
- declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
89
-
90
1
  type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
91
2
  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] : [];
92
3
  type ExtractOkTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
@@ -194,6 +105,102 @@ declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: Reado
194
105
  */
195
106
  declare const combineResultListWithAllErrors: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E[]>;
196
107
 
108
+ type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
109
+ type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
110
+ 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 Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
111
+ 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 Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
112
+ type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
113
+ type Writable<T> = T extends readonly unknown[] ? [...T] : T;
114
+ declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
115
+ private readonly innerPromise;
116
+ constructor(res: Promise<Result<T, E>>);
117
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
118
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
119
+ }
120
+ declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
121
+ static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
122
+ static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
123
+ static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
124
+ static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
125
+ static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
126
+ static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
127
+ static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
128
+ static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
129
+ /**
130
+ * Wraps a async function with a try catch, creating a new function with the same
131
+ * arguments but returning `Ok` if successful, `Err` if the function throws
132
+ *
133
+ * @param fn function to wrap with ok on success or err on failure
134
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
135
+ * @returns a new function that returns a `ResultAsync`
136
+ */
137
+ static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
138
+ private readonly innerPromise;
139
+ constructor(res: Promise<Result<T, E>>);
140
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
141
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
142
+ mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
143
+ map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
144
+ andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
145
+ andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
146
+ andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
147
+ if(fCondition: (t: T) => boolean): {
148
+ true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
149
+ false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
150
+ };
151
+ };
152
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
153
+ orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
154
+ orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
155
+ match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
156
+ unwrapOr<A>(t: A): Promise<T | A>;
157
+ /**
158
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
159
+ * This function can be used for control flow based on result values.
160
+ * @param f The function to call if the result is `Ok`
161
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
162
+ */
163
+ log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
164
+ /**
165
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
166
+ * This function can be used for control flow based on result values.
167
+ * @param f The function to call if the result is `Ok`
168
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
169
+ */
170
+ tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
171
+ /**
172
+ * Performs a side effect for the `Err` variant of `ResultAsync`.
173
+ * This function can be used for control flow based on result values.
174
+ * @param f The function to call if the result is `Err`
175
+ * @returns `self` if the result is `Ok`, otherwise the result of `f`
176
+ * @example
177
+ */
178
+ tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
179
+ finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
180
+ /**
181
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
182
+ */
183
+ safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
184
+ }
185
+ declare const okAsync: typeof ResultAsync.okAsync;
186
+ declare const errAsync: typeof ResultAsync.errAsync;
187
+ declare const fromPromise: typeof ResultAsync.fromPromise;
188
+ declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
189
+ declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
190
+ /**
191
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
192
+ * whichever comes first.
193
+ *
194
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
195
+ * Rust's ? operator.
196
+ * See `/tests/safeTry.test.ts` for examples.
197
+ *
198
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
199
+ * Rust's `result?` expression.
200
+ * @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
201
+ */
202
+ declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
203
+
197
204
  /**
198
205
  * A Result interface that is used to define the methods that are available on a
199
206
  * `Result` object.
@@ -316,7 +323,6 @@ declare class Result<T, E> implements Resultable<T, E> {
316
323
  *
317
324
  * This is useful for error recovery.
318
325
  *
319
- *
320
326
  * @param f A function to apply to an `Err` value, leaving `Ok` values
321
327
  * untouched.
322
328
  */
@@ -346,7 +352,6 @@ declare class Result<T, E> implements Resultable<T, E> {
346
352
  */
347
353
  unwrapOr<A>(defaultValue: A): T | A;
348
354
  /**
349
- *
350
355
  * Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
351
356
  * execute the function that matches the `Result` variant.
352
357
  *
@@ -360,20 +365,27 @@ declare class Result<T, E> implements Resultable<T, E> {
360
365
  * @param err
361
366
  */
362
367
  match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
368
+ /**
369
+ * Performs a side effect no matter what the `Result` variant is.
370
+ *
371
+ * @param fn The function to apply an `OK` value
372
+ * @returns the result of applying `f` or an `Err` untouched
373
+ */
374
+ log(fn: (t?: T, e?: E) => void): this;
363
375
  /**
364
376
  * Performs a side effect for the `Ok` variant of `Result`.
365
377
  *
366
378
  * @param fn The function to apply an `OK` value
367
379
  * @returns the result of applying `f` or an `Err` untouched
368
380
  */
369
- tap(fn: (t: T) => void): Result<T, E>;
381
+ tap(fn: (t: T) => void): this;
370
382
  /**
371
383
  * Performs a side effect for the `Err` variant of `Result`.
372
384
  *
373
385
  * @param fn The function to apply an `Err` value
374
386
  * @returns the result of applying `f` or an `Ok` untouched
375
387
  */
376
- tapError(fn: (e: E) => void): Result<T, E>;
388
+ tapError(fn: (e: E) => void): this;
377
389
  /**
378
390
  * This method is used to clean up and release any resources that the `Result`
379
391
  * @param f The function that will be called to clean up the resources
@@ -461,4 +473,4 @@ interface ResultarError<T, E> {
461
473
  }
462
474
  declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
463
475
 
464
- export { ok as A, err as B, type CombineResultAsyncs as C, DisposableResultAsync as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type CombineResultsWithAllErrorsArrayAsync as a, fromSafePromise as b, fromThrowableAsync as c, type Dedup as d, errAsync as e, fromPromise as f, type ExtractOkAsyncTypes as g, type ExtractErrTypes as h, type ExtractErrAsyncTypes as i, type InferErrTypes as j, type InferAsyncOkTypes as k, type InferAsyncErrTypes as l, type IsLiteralArray as m, type EmptyArrayToNever as n, okAsync as o, type MembersToUnion as p, type Combine as q, type CombineResults as r, safeTryAsync as s, type CombineResultsWithAllErrorsArray as t, combineResultAsyncList as u, combineResultList as v, combineResultAsyncListWithAllErrors as w, combineResultListWithAllErrors as x, Result as y, DisposableResult as z };
476
+ export { ok as A, err as B, type Combine as C, type Dedup as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type ExtractOkAsyncTypes as a, type ExtractErrTypes as b, type ExtractErrAsyncTypes as c, type InferErrTypes as d, type InferAsyncOkTypes as e, type InferAsyncErrTypes as f, type IsLiteralArray as g, type EmptyArrayToNever as h, type MembersToUnion as i, type CombineResults as j, type CombineResultsWithAllErrorsArray as k, combineResultAsyncList as l, combineResultList as m, combineResultAsyncListWithAllErrors as n, combineResultListWithAllErrors as o, type CombineResultAsyncs as p, type CombineResultsWithAllErrorsArrayAsync as q, DisposableResultAsync as r, okAsync as s, errAsync as t, fromPromise as u, fromSafePromise as v, fromThrowableAsync as w, safeTryAsync as x, Result as y, DisposableResult as z };