resultar 1.4.4 → 1.4.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.
package/dist/utils.d.cts CHANGED
@@ -1 +1,612 @@
1
- export { c as ExtractErrAsyncTypes, b as ExtractErrTypes, a as ExtractOkAsyncTypes, E as ExtractOkTypes, f as InferAsyncErrTypes, e as InferAsyncOkTypes, d as InferErrTypes, I as InferOkTypes, h as combineResultAsyncList, j as combineResultAsyncListWithAllErrors, g as combineResultList, i as combineResultListWithAllErrors } from './index-lTU9Lum3.cjs';
1
+ type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = {
2
+ [idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never;
3
+ };
4
+ type ExtractOkAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = {
5
+ [idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never;
6
+ };
7
+ type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = {
8
+ [idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never;
9
+ };
10
+ type ExtractErrAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = {
11
+ [idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never;
12
+ };
13
+ type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never;
14
+ type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never;
15
+ type InferAsyncOkTypes<R> = R extends ResultAsync<infer T, unknown> ? T : never;
16
+ type InferAsyncErrTypes<R> = R extends ResultAsync<unknown, infer E> ? E : never;
17
+ /**
18
+ * Short circuits on the FIRST Err value that we find
19
+ */
20
+ declare const combineResultList: <T, E>(resultList: readonly Result<T, E>[]) => Result<readonly T[], E>;
21
+ declare const combineResultAsyncList: <T, E>(asyncResultList: readonly ResultAsync<T, E>[]) => ResultAsync<readonly T[], E>;
22
+ /**
23
+ * Give a list of all the errors we find
24
+ */
25
+ declare const combineResultListWithAllErrors: <T, E>(resultList: readonly Result<T, E>[]) => Result<readonly T[], E[]>;
26
+ declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: readonly ResultAsync<T, E>[]) => ResultAsync<readonly T[], E[]>;
27
+
28
+ declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
29
+ private readonly innerPromise;
30
+ constructor(res: Promise<Result<T, E>>);
31
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
32
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
33
+ }
34
+ /**
35
+ * Represents an asynchronous Result type that wraps a Promise of a Result<T, E>.
36
+ * This class provides a way to handle asynchronous operations that may succeed with a value of type T
37
+ * or fail with an error of type E.
38
+ *
39
+ * @template T - The type of the success value
40
+ * @template E - The type of the error value
41
+ *
42
+ * @implements {PromiseLike<Result<T, E>>}
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // Create a successful async result
47
+ * const okAsync = ResultAsync.okAsync(42);
48
+ *
49
+ * // Create a failed async result
50
+ * const errAsync = ResultAsync.errAsync(new Error("Something went wrong"));
51
+ *
52
+ * // Transform a Promise into a ResultAsync
53
+ * const resultFromPromise = ResultAsync.fromPromise(
54
+ * fetch("https://api.example.com/data"),
55
+ * (error) => new Error(`API call failed: ${error}`)
56
+ * );
57
+ * ```
58
+ *
59
+ * @remarks
60
+ * ResultAsync implements the PromiseLike interface, allowing it to be used with async/await syntax.
61
+ * It provides various utility methods for transforming and combining results, similar to the Result type,
62
+ * but operating in an asynchronous context.
63
+ *
64
+ * The class includes methods for:
65
+ * - Creating ResultAsync instances (okAsync, errAsync, fromPromise)
66
+ * - Transforming values (map, mapErr)
67
+ * - Chaining operations (andThen, orElse)
68
+ * - Error handling (tapError)
69
+ * - Conditional branching (if)
70
+ * - Combining multiple ResultAsync instances (combine, combineWithAllErrors)
71
+ */
72
+ declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
73
+ /**
74
+ * Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).
75
+ *
76
+ * @param {T} value - The value to be wrapped in a Result.ok.
77
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given value and error type E.
78
+ */
79
+ static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
80
+ static okAsync<T extends void = void, E = never>(value: void): ResultAsync<void, E>;
81
+ /**
82
+ * Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.
83
+ *
84
+ * @return {ResultAsync<undefined, E>} A ResultAsync instance with undefined as the value type and E as the error type.
85
+ */
86
+ static unitAsync<E = never>(): ResultAsync<undefined, E>;
87
+ /**
88
+ * Returns a ResultAsync instance that is immediately resolved with a Result.err(error).
89
+ *
90
+ * @param {E} error - The error to be wrapped in a Result.err.
91
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given error and value type T.
92
+ */
93
+ static errAsync<T = never, E = unknown>(err: E): ResultAsync<T, E>;
94
+ static errAsync<T = never, E extends void = void>(err: void): ResultAsync<T, void>;
95
+ /**
96
+ * Creates a ResultAsync from a Promise, catching any errors that occur during its execution.
97
+ *
98
+ * @param fn - The Promise or a function returning a Promise to be wrapped in a ResultAsync.
99
+ * @param errorFn - Optional function to transform the caught error into a specific error type.
100
+ * If not provided, the original error will be used.
101
+ * @returns A ResultAsync that will resolve to Ok with the promise's value if successful,
102
+ * or Err with either the transformed error (if errorFn is provided) or the original error.
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * // Without error transformer
107
+ * const result = ResultAsync.tryCatch(Promise.resolve(42));
108
+ *
109
+ * // With error transformer
110
+ * const result = ResultAsync.tryCatch(
111
+ * fetch('https://api.example.com'),
112
+ * (error) => new CustomError('API request failed')
113
+ * );
114
+ * ```
115
+ */
116
+ static tryCatch<T, E>(fn: Promise<T> | (() => Promise<T>), errorFn?: (e: unknown) => E): ResultAsync<T, E>;
117
+ /**
118
+ * Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
119
+ * based on the provided promise.
120
+ *
121
+ * @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
122
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
123
+ */
124
+ static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
125
+ /**
126
+ * Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
127
+ * based on the provided promise.
128
+ *
129
+ * @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
130
+ * @param {(e: unknown) => E} errorFn - A function that transforms the error from the promise into the error type E.
131
+ * @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
132
+ */
133
+ static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
134
+ static combine<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(asyncResultList: T): CombineResultAsyncs<T>;
135
+ static combine<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): CombineResultAsyncs<T>;
136
+ static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
137
+ static combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
138
+ /**
139
+ * Wraps a async function with a try catch, creating a new function with the same
140
+ * arguments but returning `Ok` if successful, `Err` if the function throws
141
+ *
142
+ * @param fn function to wrap with ok on success or err on failure
143
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
144
+ * @returns a new function that returns a `ResultAsync`
145
+ */
146
+ static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
147
+ private readonly innerPromise;
148
+ constructor(res: Promise<Result<T, E>>);
149
+ then<A, B>(// eslint-disable-line unicorn/no-thenable
150
+ successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
151
+ mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
152
+ map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
153
+ andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
154
+ andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
155
+ andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
156
+ if(fCondition: (t: T) => boolean): {
157
+ true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
158
+ false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
159
+ };
160
+ };
161
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
162
+ orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
163
+ orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
164
+ match<A, B = A>(ok: (t: T) => A, fnErr: (e: E) => B): Promise<A | B>;
165
+ unwrapOr<A>(t: A): Promise<T | A>;
166
+ unwrapOrThrow(): Promise<T>;
167
+ /**
168
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
169
+ * This function can be used for control flow based on result values.
170
+ * @param f The function to call if the result is `Ok`
171
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
172
+ */
173
+ log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
174
+ /**
175
+ * Performs a side effect for the `Ok` variant of `ResultAsync`.
176
+ * This function can be used for control flow based on result values.
177
+ * @param f The function to call if the result is `Ok`
178
+ * @returns `self` if the result is `Err`, otherwise the result of `f`
179
+ */
180
+ tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
181
+ /**
182
+ * Performs a side effect for the `Err` variant of `ResultAsync`.
183
+ * This function can be used for control flow based on result values.
184
+ * @param f The function to call if the result is `Err`
185
+ * @returns `self` if the result is `Ok`, otherwise the result of `f`
186
+ * @example
187
+ */
188
+ tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
189
+ finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
190
+ [Symbol.asyncIterator](): AsyncGenerator<Result<never, E>, T>;
191
+ /**
192
+ * @deprecated will be removed in 2.0.0.
193
+ *
194
+ * You can use `safeTry` without this method.
195
+ * @example
196
+ * ```typescript
197
+ * safeTry(async function* () {
198
+ * const okValue = yield* yourResult
199
+ * })
200
+ * ```
201
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
202
+ */
203
+ safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
204
+ }
205
+ /**
206
+ * @deprecated Use `safeTry` instead.
207
+ * @see `safeTry`
208
+ *
209
+ * This will be removed in the next major version.
210
+ */
211
+ declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
212
+ declare const okAsync: typeof ResultAsync.okAsync;
213
+ declare const errAsync: typeof ResultAsync.errAsync;
214
+ declare const fromPromise: typeof ResultAsync.fromPromise;
215
+ declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
216
+ declare const unitAsync: typeof ResultAsync.unitAsync;
217
+ declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
218
+ declare const tryCatchAsync: typeof ResultAsync.tryCatch;
219
+ type CombineResultAsyncs<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
220
+ type CombineResultsWithAllErrorsArrayAsync<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>;
221
+ 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;
222
+ 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;
223
+ type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = TraverseAsync<T, Depth> extends ResultAsync<infer Oks, infer Errs> ? ResultAsync<Oks, Errs[]> : never;
224
+ type Writable<T> = T extends readonly unknown[] ? [...T] : T;
225
+
226
+ /**
227
+ * A Result interface that is used to define the methods that are available on a
228
+ * `Result` object.
229
+ *
230
+ * The immutability of the `Result` object is maintained by the interface.
231
+ */
232
+ interface Resultable<T, E> {
233
+ get value(): T;
234
+ get error(): E;
235
+ /**
236
+ * Used to check if a `Result` is an `OK`
237
+ *
238
+ * @returns `true` if the result is an `OK` variant of Result
239
+ */
240
+ isOk(): boolean;
241
+ /**
242
+ * Used to check if a `Result` is an `Err`
243
+ *
244
+ * @returns `true` if the result is an `Err` variant of Result
245
+ */
246
+ isErr(): boolean;
247
+ /**
248
+ * Unwrap the `Ok` value, or return the default if there is an `Err`
249
+ *
250
+ * @param v the default value to return if there is an `Err`
251
+ */
252
+ unwrapOr<A>(defaultValue: A): T | A;
253
+ /**
254
+ * **This method is unsafe, and should only be used in a test environments**
255
+ *
256
+ * Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
257
+ *
258
+ * @param config
259
+ */
260
+ _unsafeUnwrap(config?: ErrorConfig): T;
261
+ /**
262
+ * **This method is unsafe, and should only be used in a test environments**
263
+ *
264
+ * takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
265
+ * otherwise it throws a custom object.
266
+ *
267
+ * @param config
268
+ */
269
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
270
+ }
271
+ /**
272
+ * A `Result` is a type that represents either success (`Ok`) or failure (`Err`).
273
+ * It is often used to replace exceptions or `null` returns.
274
+ * `Result` is often used to handle errors in a functional way.
275
+ * The `Result` type is often used to handle errors in a functional way.
276
+ */
277
+ declare class Result<T, E> implements Resultable<T, E> {
278
+ private readonly state;
279
+ /**
280
+ * Creates a `Result` by running a function that might throw.
281
+ * If the function throws, the error will be caught and returned as an `Err`.
282
+ * If the function succeeds, the value will be wrapped in an `Ok`.
283
+ *
284
+ * @param fn - The function to execute, which might throw an error
285
+ * @param errorFn - Optional function to transform the thrown error before wrapping in `Err`
286
+ * @returns A `Result` containing either the function's return value or the caught error
287
+ */
288
+ static tryCatch<T, E>(fn: () => Exclude<T, Promise<unknown>>, errorFn?: (e: unknown) => E): Result<T, E>;
289
+ /**
290
+ * Wraps a function with a try catch, creating a new function with the same
291
+ * arguments but returning `Ok` if successful, `Err` if the function throws
292
+ *
293
+ * @param fn function to wrap with ok on success or err on failure
294
+ * @param errorFn when an error is thrown, this will wrap the error result if provided
295
+ */
296
+ static fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
297
+ /**
298
+ * Creates a new `Result` instance representing a successful operation.
299
+ *
300
+ * @param {T} value - The value to be wrapped in the `Result` instance.
301
+ * @return {Result<T, E>} A new `Result` instance with the provided value.
302
+ */
303
+ static ok<T, E = never>(value: T): Result<T, E>;
304
+ static ok<T extends void = void, E = never>(value: void): Result<void, E>;
305
+ /**
306
+ * Creates a new `Result` instance representing a successful operation with an undefined value.
307
+ *
308
+ * @return {Result<undefined, E>} A new `Result` instance with an undefined value.
309
+ */
310
+ static unit<E = never>(): Result<undefined, E>;
311
+ static err<T = never, E extends string = string>(err: E): Result<T, E>;
312
+ static err<T = never, E = unknown>(err: E): Result<T, E>;
313
+ static err<T = never, E extends void = void>(err: void): Result<T, void>;
314
+ static combine<T extends readonly [
315
+ Result<unknown, unknown>,
316
+ ...Result<unknown, unknown>[]
317
+ ]>(resultList: T): CombineResults<T>;
318
+ static combine<T extends readonly Result<unknown, unknown>[]>(resultList: T): CombineResults<T>;
319
+ static combineWithAllErrors<T extends readonly [
320
+ Result<unknown, unknown>,
321
+ ...Result<unknown, unknown>[]
322
+ ]>(resultList: T): CombineResultsWithAllErrorsArray<T>;
323
+ static combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(resultList: T): CombineResultsWithAllErrorsArray<T>;
324
+ readonly value: T;
325
+ readonly error: E;
326
+ private constructor();
327
+ /**
328
+ * Used to check if a `Result` is an `OK`
329
+ *
330
+ * @returns `true` if the result is an `OK` variant of Result
331
+ */
332
+ isOk(): boolean;
333
+ /**
334
+ * Used to check if a `Result` is an `Err`
335
+ *
336
+ * @returns `true` if the result is an `Err` variant of Result
337
+ */
338
+ isErr(): boolean;
339
+ /**
340
+ * Maps a `Result<T, E>` to `Result<U, E>`
341
+ * by applying a function to a contained `Ok` value, leaving an `Err` value
342
+ * untouched.
343
+ *
344
+ * @param f The function to apply an `OK` value
345
+ * @returns the result of applying `f` or an `Err` untouched
346
+ */
347
+ map<X>(f: (t: T) => X): Result<X, E>;
348
+ /**
349
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
350
+ * contained `Err` value, leaving an `Ok` value untouched.
351
+ *
352
+ * This function can be used to pass through a successful result while
353
+ * handling an error.
354
+ *
355
+ * @param f a function to apply to the error `Err` value
356
+ */
357
+ mapErr<X>(fn: (t: E) => X): Result<T, X>;
358
+ /**
359
+ * Similar to `map` Except you must return a new `Result`.
360
+ *
361
+ * This is useful for when you need to do a subsequent computation using the
362
+ * inner `T` value, but that computation might fail.
363
+ * Additionally, `andThen` is really useful as a tool to flatten a
364
+ * `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
365
+ *
366
+ * @param f The function to apply to the current value
367
+ */
368
+ andThen<X, Y>(f: (t: T) => Result<X, Y>): Result<X, E | Y>;
369
+ if(fCondition: (t: T) => boolean): {
370
+ true: <X1, Y1>(fTrue: (t: T) => Result<X1, Y1>) => {
371
+ false: <X2, Y2>(fFalse: (t: T) => Result<X2, Y2>) => Result<X1 | X2, Y1 | Y2 | E>;
372
+ };
373
+ };
374
+ /**
375
+ * Takes an `Err` value and maps it to a `Result<T, SomeNewType>`.
376
+ *
377
+ * This is useful for error recovery.
378
+ *
379
+ * @param f A function to apply to an `Err` value, leaving `Ok` values
380
+ * untouched.
381
+ */
382
+ orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): Result<InferOkTypes<R> | T, InferErrTypes<R>>;
383
+ /**
384
+ * Similar to `map` Except you must return a new `Result`.
385
+ *
386
+ * This is useful for when you need to do a subsequent async computation using
387
+ * the inner `T` value, but that computation might fail. Must return a ResultAsync
388
+ *
389
+ * @param f The function that returns a `ResultAsync` to apply to the current
390
+ * value
391
+ */
392
+ asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
393
+ /**
394
+ * Maps a `Result<T, E>` to `ResultAsync<U, E>`
395
+ * by applying an async function to a contained `Ok` value, leaving an `Err`
396
+ * value untouched.
397
+ *
398
+ * @param f An async function to apply an `OK` value
399
+ */
400
+ asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
401
+ /**
402
+ * Unwrap the `Ok` value, or return the default if there is an `Err`
403
+ *
404
+ * @param v the default value to return if there is an `Err`
405
+ */
406
+ unwrapOr<A>(defaultValue: A): T | A;
407
+ /**
408
+ * Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
409
+ * execute the function that matches the `Result` variant.
410
+ *
411
+ * Match callbacks do not necessitate to return a `Result`, however you can
412
+ * return a `Result` if you want to.
413
+ *
414
+ * `match` is like chaining `map` and `mapErr`, with the distinction that
415
+ * with `match` both functions must have the same return type.
416
+ *
417
+ * @param ok
418
+ * @param err
419
+ */
420
+ match<A, B = A>(fnOk: (t: T) => A, fnErr: (e: E) => B): A | B;
421
+ /**
422
+ * Performs a side effect for the `Ok` or `Err` variant of `Result`.
423
+ *
424
+ * @param fn The function to apply an `OK` value
425
+ * @returns the result of applying `f` or an `Err` untouched
426
+ */
427
+ log(fn: (t?: T, e?: E) => void): this;
428
+ /**
429
+ * Performs a side effect for the `Ok` variant of `Result`.
430
+ *
431
+ * @param fn The function to apply an `OK` value
432
+ * @returns the result of applying `f` or an `Err` untouched
433
+ */
434
+ tap(fn: (t: T) => void): this;
435
+ /**
436
+ * Performs a side effect for the `Err` variant of `Result`.
437
+ *
438
+ * @param fn The function to apply an `Err` value
439
+ * @returns the result of applying `f` or an `Ok` untouched
440
+ */
441
+ tapError(fn: (e: E) => void): this;
442
+ /**
443
+ * This method is used to clean up and release any resources that the `Result`
444
+ * @param f The function that will be called to clean up the resources
445
+ */
446
+ finally<X = T, Y = E>(f: (value: X, error: Y) => void): DisposableResult<X, Y>;
447
+ unwrapOrThrow(): T;
448
+ /**
449
+ * **This method is unsafe, and should only be used in a test environments**
450
+ *
451
+ * Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
452
+ *
453
+ * @param config
454
+ */
455
+ _unsafeUnwrap(config?: ErrorConfig): T;
456
+ /**
457
+ * **This method is unsafe, and should only be used in a test environments**
458
+ *
459
+ * takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
460
+ * otherwise it throws a custom object.
461
+ *
462
+ * @param config
463
+ */
464
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
465
+ [Symbol.iterator](): Generator<Result<never, E>, T>;
466
+ safeUnwrap(): Generator<Result<never, E>, T>;
467
+ }
468
+ /**
469
+ * A `Disposable` is an object that has a `dispose` method that can be used to
470
+ * clean up resources.
471
+ */
472
+ declare class DisposableResult<T, E> implements Resultable<T, E>, Disposable {
473
+ readonly result: Resultable<T, E>;
474
+ private readonly finalizer;
475
+ constructor(result: Resultable<T, E>, finalizer: (value: T, error: E) => void);
476
+ get value(): T;
477
+ get error(): E;
478
+ _unsafeUnwrap(config?: ErrorConfig): T;
479
+ _unsafeUnwrapErr(config?: ErrorConfig): E;
480
+ isOk(): boolean;
481
+ isErr(): boolean;
482
+ unwrapOr<A>(defaultValue: A): T | A;
483
+ [Symbol.dispose](): void;
484
+ }
485
+ /**
486
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
487
+ * whichever comes first.
488
+ *
489
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
490
+ * Rust's ? operator.
491
+ * See `/tests/safeTry.test.ts` for examples.
492
+ *
493
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
494
+ * Rust's `result?` expression.
495
+ * @returns The first occurence of either an yielded Err or a returned Result.
496
+ */
497
+ declare function safeTry<T, E>(body: () => Generator<Result<never, E>, Result<T, E>>): Result<T, E>;
498
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(body: () => Generator<YieldErr, GeneratorReturnResult>): Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
499
+ /**
500
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
501
+ * whichever comes first.
502
+ *
503
+ * This function, in combination with `Result.safeUnwrap()`, is intended to emulate
504
+ * Rust's ? operator.
505
+ * See `/tests/safeTry.test.ts` for examples.
506
+ *
507
+ * @param body - What is evaluated. In body, `yield* result.safeUnwrap()` and
508
+ * `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression.
509
+ * @returns The first occurence of either an yielded Err or a returned Result.
510
+ */
511
+ declare function safeTry<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
512
+ declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(body: () => AsyncGenerator<YieldErr, GeneratorReturnResult>): ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
513
+ declare const ok: typeof Result.ok;
514
+ declare const err: typeof Result.err;
515
+ declare const fromThrowable: typeof Result.fromThrowable;
516
+ declare const unit: typeof Result.unit;
517
+ declare const tryCatch: typeof Result.tryCatch;
518
+ type Prev = [
519
+ never,
520
+ 0,
521
+ 1,
522
+ 2,
523
+ 3,
524
+ 4,
525
+ 5,
526
+ 6,
527
+ 7,
528
+ 8,
529
+ 9,
530
+ 10,
531
+ 11,
532
+ 12,
533
+ 13,
534
+ 14,
535
+ 15,
536
+ 16,
537
+ 17,
538
+ 18,
539
+ 19,
540
+ 20,
541
+ 21,
542
+ 22,
543
+ 23,
544
+ 24,
545
+ 25,
546
+ 26,
547
+ 27,
548
+ 28,
549
+ 29,
550
+ 30,
551
+ 31,
552
+ 32,
553
+ 33,
554
+ 34,
555
+ 35,
556
+ 36,
557
+ 37,
558
+ 38,
559
+ 39,
560
+ 40,
561
+ 41,
562
+ 42,
563
+ 43,
564
+ 44,
565
+ 45,
566
+ 46,
567
+ 47,
568
+ 48,
569
+ 49,
570
+ ...0[]
571
+ ];
572
+ type CollectResults<T, Collected extends unknown[] = [], Depth extends number = 50> = [
573
+ Depth
574
+ ] extends [never] ? [] : T extends [infer H, ...infer Rest] ? H extends Result<infer L, infer R> ? CollectResults<Rest, [
575
+ ...Collected,
576
+ [L, R]
577
+ ], Prev[Depth]> : never : Collected;
578
+ 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;
579
+ type Combine<T, Depth extends number = 5> = Transpose<CollectResults<T>, [], Depth> extends [
580
+ infer L,
581
+ infer R
582
+ ] ? [UnknownMembersToNever<L>, UnknownMembersToNever<R>] : Transpose<CollectResults<T>, [], Depth> extends [] ? [[], []] : never;
583
+ type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
584
+ 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] : [];
585
+ 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;
586
+ type UnknownMembersToNever<T> = T extends [infer H, ...infer R] ? [[unknown] extends [H] ? never : H, ...UnknownMembersToNever<R>] : T;
587
+ type MembersToUnion<T> = T extends unknown[] ? T[number] : never;
588
+ type IsLiteralArray<T> = T extends {
589
+ length: infer L;
590
+ } ? L extends number ? number extends L ? 0 : 1 : 0 : 0;
591
+ type Traverse<T, Depth extends number = 5> = Combine<T, Depth> extends [infer Oks, infer Errs] ? Result<EmptyArrayToNever<Oks, 1>, MembersToUnion<Errs>> : never;
592
+ type TraverseWithAllErrors<T, Depth extends number = 5> = Traverse<T, Depth> extends Result<infer Oks, infer Errs> ? Result<Oks, Errs[]> : never;
593
+ type CombineResults<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? Traverse<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>;
594
+ type CombineResultsWithAllErrorsArray<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrors<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>;
595
+
596
+ interface ErrorConfig {
597
+ withStackTrace: boolean;
598
+ }
599
+ interface ResultarError<T, E> extends Error {
600
+ data: {
601
+ type: string;
602
+ value: T | undefined;
603
+ } | {
604
+ type: string;
605
+ value: E | undefined;
606
+ };
607
+ message: string;
608
+ stack: string | undefined;
609
+ }
610
+ declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
611
+
612
+ export { type CombineResultAsyncs as C, DisposableResultAsync as D, type EmptyArrayToNever as E, type ExtractErrAsyncTypes, type ExtractErrTypes, type ExtractOkAsyncTypes, type ExtractOkTypes, type IsLiteralArray as I, type InferAsyncErrTypes, type InferAsyncOkTypes, type InferErrTypes, type InferOkTypes, type MemberListOf as M, ResultAsync as R, type Transpose as T, fromSafePromise as a, fromThrowableAsync as b, type CombineResultsWithAllErrorsArrayAsync as c, combineResultAsyncList, combineResultAsyncListWithAllErrors, combineResultList, combineResultListWithAllErrors, Result as d, errAsync as e, fromPromise as f, DisposableResult as g, safeTry as h, ok as i, err as j, fromThrowable as k, unit as l, tryCatch as m, type Combine as n, okAsync as o, type Dedup as p, type MembersToUnion as q, type CombineResults as r, safeTryAsync as s, tryCatchAsync as t, unitAsync as u, type CombineResultsWithAllErrorsArray as v, type ErrorConfig as w, createResultarError as x };