retuple 1.0.0-next.13 → 1.0.0-next.15

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.cts CHANGED
@@ -1,7 +1,51 @@
1
+ /**
2
+ * ## Result Like Symbol
3
+ *
4
+ * Implement a custom result-like by implementing the `ResultLike` interface
5
+ * on a class or object. An object with this implementation can be converted
6
+ * to a `Result` and can be used in most places where a `Result` is required.
7
+ *
8
+ * ```ts
9
+ * import { ResultLikeSymbol } from "retuple/symbol";
10
+ * import { Result, Ok, Err, type ResultLike } from "retuple";
11
+ *
12
+ * class CustomResult<T> implements ResultLike<T, CustomError> {
13
+ * value: T;
14
+ *
15
+ * constructor(value: T) {
16
+ * this.value = value;
17
+ * }
18
+ *
19
+ * [ResultLikeSymbol](): Result<T, CustomError> {
20
+ * return this.value === "test"
21
+ * ? Ok(this.value)
22
+ * : Err(new CustomError("Value was not test"));
23
+ * }
24
+ * }
25
+ *
26
+ * const custom = new CustomResult("test");
27
+ * const result: Result<string, Error> = Result(custom);
28
+ *
29
+ * const chain = Ok()
30
+ * .$map(() => "value")
31
+ * .$andThen((value) => new CustomResult(value))
32
+ * .$or(myresult);
33
+ * ```
34
+ */
35
+ export declare const ResultLikeSymbol: unique symbol;
36
+ export type ResultLikeSymbol = typeof ResultLikeSymbol;
37
+ export type ResultLike<T, E> = {
38
+ [ResultLikeSymbol](): Result<T, E>;
39
+ };
1
40
  export type Ok = typeof Ok;
2
41
  export type Err = typeof Err;
3
42
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
4
- export { type ResultAsync };
43
+ export { type ResultAsync, type ResultRetry };
44
+ export interface ResultRetryController<E> {
45
+ error: E;
46
+ attempt: number;
47
+ abort: () => void;
48
+ }
5
49
  /**
6
50
  * ## Retuple Unwrap Failed
7
51
  *
@@ -30,16 +74,6 @@ export declare class RetupleExpectFailed<const E = unknown> extends Error {
30
74
  value: E;
31
75
  constructor(value: E);
32
76
  }
33
- /**
34
- * ## Retuple Expect Failed
35
- *
36
- * An error which occurs when calling `$flatten` on `Ok`, when the value
37
- * contained in the `Ok` is not an `Ok` or `Err`.
38
- */
39
- export declare class RetupleFlattenFailed<const T = unknown> extends Error {
40
- value: T;
41
- constructor(value: T);
42
- }
43
77
  /**
44
78
  * ## Retuple Thrown Value Error
45
79
  *
@@ -47,21 +81,10 @@ export declare class RetupleFlattenFailed<const T = unknown> extends Error {
47
81
  * thrown error or rejected value is not an instance of `Error`, and when no
48
82
  * map error function is provided.
49
83
  */
50
- export declare class RetupleThrownValueError extends Error {
84
+ export declare class RetupleCaughtValueError extends Error {
51
85
  value: unknown;
52
86
  constructor(value: unknown);
53
87
  }
54
- /**
55
- * ## Retuple Invalid Result Error
56
- *
57
- * This error is thrown when attempting to construct a `Result` from a tuple,
58
- * when neither index 0 or 1 are null or undefined. In this case, it is
59
- * impossible to determine whether the result should be `Ok` or `Err`.
60
- */
61
- export declare class RetupleInvalidResultError extends Error {
62
- value: unknown[];
63
- constructor(value: unknown[]);
64
- }
65
88
  /**
66
89
  * ## Retuple Invalid Union Error
67
90
  *
@@ -88,19 +111,38 @@ export declare class RetupleArrayMethodUnavailableError extends Error {
88
111
  *
89
112
  * @TODO
90
113
  */
91
- export declare function Result<R extends [err: null | undefined, value: unknown] | [err: unknown, value: null | undefined]>(resultLike: R): (R extends [null | undefined, infer T] ? ThisOk<T> : R extends [infer E, null | undefined] ? ThisErr<E> : never) extends ThisOk<infer T> | ThisErr<infer E> ? Result<T, NonNullable<E>> : never;
114
+ export declare function Result<T, E>(resultLike: ResultLike<T, E>): Result<T, E>;
92
115
  export declare namespace Result {
93
116
  var Ok: typeof import(".").Ok;
94
117
  var Err: typeof import(".").Err;
95
- var $resolve: typeof resolve;
96
- var $nonNullable: typeof nonNullable;
97
- var $truthy: typeof truthy;
98
- var $union: typeof union;
99
- var $safe: typeof safe;
100
- var $safeAsync: typeof safeAsync;
101
- var $safePromise: typeof safePromise;
102
- var $retry: typeof retry;
103
- var $safeRetry: typeof safeRetry;
118
+ var $from: <T, E>(result: ResultLike<T, E>) => Result<T, E>;
119
+ var $resolve: <T, E>(result: ResultLikeAwaitable<T, E>) => ResultAsync<T, E>;
120
+ var $nonNullable: {
121
+ <const T>(value: T): Result<NonNullable<T>, true>;
122
+ <const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
123
+ };
124
+ var $truthy: {
125
+ <const T>(value: T): Result<Truthy<T>, true>;
126
+ <const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
127
+ };
128
+ var $fromUnion: <U extends ObjectUnionOk<any> | ObjectUnionErr<any>>(union: U) => Result<U extends ObjectUnionOk<infer T> ? T : never, U extends ObjectUnionErr<infer E> ? E : never>;
129
+ var $safe: {
130
+ <T>(f: () => Awaited<T>): Result<T, Error>;
131
+ <T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
132
+ };
133
+ var $safeAsync: {
134
+ <T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
135
+ <T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
136
+ };
137
+ var $safePromise: {
138
+ <T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
139
+ <T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
140
+ };
141
+ var $retry: <T, E>(f: () => ResultLike<T, E> | PromiseLike<ResultLike<T, E>>) => ResultRetry<T, E>;
142
+ var $safeRetry: {
143
+ <T>(f: () => T | PromiseLike<T>): ResultRetry<T, Error>;
144
+ <T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultRetry<T, E>;
145
+ };
104
146
  }
105
147
  /**
106
148
  * Create a new {@link Result} with the `Ok` variant. When called without
@@ -144,314 +186,6 @@ export declare function Ok<const T>(val: T): Result<T, never>;
144
186
  */
145
187
  export declare function Err(): Result<never, void>;
146
188
  export declare function Err<const E>(err: E): Result<never, E>;
147
- /**
148
- * @TODO
149
- */
150
- declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
151
- /**
152
- * Construct a {@link Result} from a value. If the value is neither null or
153
- * undefined, the result is `Ok`.
154
- *
155
- * Otherwise, the result is `Err` containing:
156
-
157
- * - the returned value from the error function when provided;
158
- * - or `true` otherwise.
159
- *
160
- * @example
161
- *
162
- * ```ts
163
- * const result: Result<User, Error> = Result.$nonNullable(
164
- * users.find((user) => user.id === currentUserId),
165
- * () => new Error("User not found"),
166
- * );
167
- * ```
168
- *
169
- * @example
170
- *
171
- * ```ts
172
- * const [err, value] = Result.$nonNullable("test");
173
- *
174
- * assert.equal(err, undefined);
175
- * assert.equal(value, "test");
176
- * ```
177
- *
178
- * @example
179
- *
180
- * ```ts
181
- * const [err, value] = Result.$nonNullable(null);
182
- *
183
- * assert.equal(err, true);
184
- * assert.equal(value, undefined);
185
- * ```
186
- *
187
- * @example
188
- *
189
- * ```ts
190
- * const [err, value] = Result.$nonNullable(null, () => "error");
191
- *
192
- * assert.equal(err, "error");
193
- * assert.equal(value, undefined);
194
- * ```
195
- */
196
- declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
197
- declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
198
- /**
199
- * Construct a {@link Result} from a value. If the value is truthy, the result
200
- * is `Ok`.
201
- *
202
- * Otherwise, the result is `Err` containing:
203
- *
204
- * - the returned value from the error function when provided;
205
- * - or `true` otherwise.
206
- *
207
- * @example
208
- *
209
- * ```ts
210
- * const result: Result<string, Error> = Result.$truthy(
211
- * username.trim(),
212
- * () => new Error("Username is empty"),
213
- * );
214
- * ```
215
- *
216
- * @example
217
- *
218
- * ```ts
219
- * const [err, value] = Result.$truthy("test");
220
- *
221
- * assert.equal(err, undefined);
222
- * assert.equal(value, "test");
223
- * ```
224
- *
225
- * @example
226
- *
227
- * ```ts
228
- * const [err, value] = Result.$truthy("");
229
- *
230
- * assert.equal(err, true);
231
- * assert.equal(value, undefined);
232
- * ```
233
- *
234
- * @example
235
- *
236
- * ```ts
237
- * const [err, value] = Result.$truthy(0, () => "error");
238
- *
239
- * assert.equal(err, "error");
240
- * assert.equal(value, undefined);
241
- * ```
242
- */
243
- declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
244
- declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
245
- /**
246
- * @TODO
247
- */
248
- declare function union<U extends ObjectUnionOk<any> | ObjectUnionErr<any>>(union: U): Result<U extends ObjectUnionOk<infer T> ? T : never, U extends ObjectUnionErr<infer E> ? E : never>;
249
- /**
250
- * Construct a {@link Result} from a synchronous function call. If the function
251
- * returns without throwing, the result is `Ok`.
252
- *
253
- * Otherwise, the result is `Err` containing (in priority order):
254
- *
255
- * - the returned value from the map error function when provided;
256
- * - the thrown error when it is an instance of `Error`;
257
- * - `RetupleThrownValueError` when a non `Error` instance is thrown.
258
- *
259
- * @example
260
- *
261
- * ```ts
262
- * const result: Result<URL, Error> = Result.$safe(
263
- * () => new URL(user.url),
264
- * () => new Error("Invalid URL"),
265
- * );
266
- * ```
267
- *
268
- * @example
269
- *
270
- * ```ts
271
- * const [err, value] = Result.$safe(() => "test");
272
- *
273
- * assert.equal(err, undefined);
274
- * assert.equal(value, "test");
275
- * ```
276
- *
277
- * @example
278
- *
279
- * ```ts
280
- * const [err, value] = Result.$safe(
281
- * () => {
282
- * throw new Error("throws");
283
- * },
284
- * () => "error",
285
- * );
286
- *
287
- * assert.equal(err, "error");
288
- * assert.equal(value, undefined);
289
- * ```
290
- *
291
- * @example
292
- *
293
- * ```ts
294
- * const [err, value] = Result.$safe(
295
- * () => {
296
- * throw new Error("throws")
297
- * },
298
- * );
299
- *
300
- * assert(err instanceof Error && err.message === "throws");
301
- * assert.equal(value, undefined);
302
- * ```
303
- *
304
- * @example
305
- *
306
- * ```ts
307
- * const [err, value] = Result.$safe(() => {
308
- * throw "non error";
309
- * });
310
- *
311
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
312
- * assert.equal(value, undefined);
313
- */
314
- declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
315
- declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
316
- /**
317
- * Construct a {@link ResultAsync} from a function call. If the function returns
318
- * without throwing, and any promise returned resolves, the result is `Ok`.
319
- *
320
- * Otherwise, the result is `Err` containing (in priority order):
321
- *
322
- * - the returned value from the map error function when provided;
323
- * - the thrown/rejected error when it is an instance of `Error`;
324
- * - `RetupleThrownValueError` when throwing/rejecting with a non `Error`.
325
- *
326
- * @example
327
- *
328
- * ```ts
329
- * const result: Result<Response, Error> = await Result.$safeAsync(
330
- * () => fetch("http://example.com/api"),
331
- * () => new Error("Fetch failed"),
332
- * );
333
- * ```
334
- *
335
- * @example
336
- *
337
- * ```ts
338
- * const [err, value] = await Result.$safeAsync(async () => "test");
339
- *
340
- * assert.equal(err, undefined);
341
- * assert.equal(value, "test");
342
- * ```
343
- *
344
- * @example
345
- *
346
- * ```ts
347
- * const [err, value] = await Result.$safeAsync(
348
- * async () => {
349
- * throw new Error("throws");
350
- * },
351
- * () => "error",
352
- * );
353
- *
354
- * assert.equal(err, "error");
355
- * assert.equal(value, undefined);
356
- * ```
357
- *
358
- * @example
359
- *
360
- * ```ts
361
- * const [err, value] = await Result.$safeAsync(
362
- * async () => {
363
- * throw new Error("throws")
364
- * },
365
- * );
366
- *
367
- * assert(err instanceof Error && err.message === "throws");
368
- * assert.equal(value, undefined);
369
- * ```
370
- *
371
- * @example
372
- *
373
- * ```ts
374
- * const [err, value] = await Result.$safeAsync(async () => {
375
- * throw "non error";
376
- * });
377
- *
378
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
379
- * assert.equal(value, undefined);
380
- */
381
- declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
382
- declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
383
- /**
384
- * Construct a {@link Result} from a promise. If the promise resolves, the
385
- * result is `Ok`.
386
- *
387
- * Otherwise, the result is `Err` containing (in priority order):
388
- *
389
- * - the returned value from the map error function when provided;
390
- * - the rejected error when it is an instance of `Error`;
391
- * - `RetupleThrownValueError` when rejecting with a non `Error`.
392
- *
393
- * @example
394
- *
395
- * ```ts
396
- * const result: Result<Response, Error> = await Result.$safePromise(
397
- * fetch("http://example.com/api"),
398
- * () => new Error("Fetch failed"),
399
- * );
400
- * ```
401
- *
402
- * @example
403
- *
404
- * ```ts
405
- * const [err, value] = await Result.$safePromise(Promise.resolve("test"));
406
- *
407
- * assert.equal(err, undefined);
408
- * assert.equal(value, "test");
409
- * ```
410
- *
411
- * @example
412
- *
413
- * ```ts
414
- * const [err, value] = await Result.$safePromise(
415
- * Promise.reject(new Error("rejects")),
416
- * () => "error",
417
- * );
418
- *
419
- * assert.equal(err, "error");
420
- * assert.equal(value, undefined);
421
- * ```
422
- *
423
- * @example
424
- *
425
- * ```ts
426
- * const [err, value] = await Result.$safePromise(
427
- * Promise.reject(new Error("rejects")),
428
- * );
429
- *
430
- * assert(err instanceof Error && err.message === "rejects");
431
- * assert.equal(value, undefined);
432
- * ```
433
- *
434
- * @example
435
- *
436
- * ```ts
437
- * const [err, value] = await Result.$safeAsync(
438
- * Promise.reject("non error"),
439
- * );
440
- *
441
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
442
- * assert.equal(value, undefined);
443
- */
444
- declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
445
- declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
446
- /**
447
- * @TODO
448
- */
449
- declare function retry<T, E>(f: () => Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultRetry<T, E>;
450
- /**
451
- * @TODO
452
- */
453
- declare function safeRetry<T>(f: () => T | PromiseLike<T>): ResultRetry<T, Error>;
454
- declare function safeRetry<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultRetry<T, E>;
455
189
  /**
456
190
  * ## RetupleArray
457
191
  *
@@ -462,8 +196,7 @@ declare class RetupleArray<T> extends Array<T> {
462
196
  /**
463
197
  * ## Method not available
464
198
  *
465
- * Built-in array methods not available on `Result` types, convert the result
466
- * to a tuple using `$tuple()` first.
199
+ * Built-in array methods not available on {@link Result} types.
467
200
  *
468
201
  * @deprecated
469
202
  */
@@ -471,8 +204,7 @@ declare class RetupleArray<T> extends Array<T> {
471
204
  /**
472
205
  * ## Method not available
473
206
  *
474
- * Built-in array methods not available on `Result` types, convert the result
475
- * to a tuple using `$tuple()` first.
207
+ * Built-in array methods not available on {@link Result} types.
476
208
  *
477
209
  * @deprecated
478
210
  */
@@ -480,8 +212,7 @@ declare class RetupleArray<T> extends Array<T> {
480
212
  /**
481
213
  * ## Method not available
482
214
  *
483
- * Built-in array methods not available on `Result` types, convert the result
484
- * to a tuple using `$tuple()` first.
215
+ * Built-in array methods not available on {@link Result} types.
485
216
  *
486
217
  * @deprecated
487
218
  */
@@ -489,8 +220,7 @@ declare class RetupleArray<T> extends Array<T> {
489
220
  /**
490
221
  * ## Method not available
491
222
  *
492
- * Built-in array methods not available on `Result` types, convert the result
493
- * to a tuple using `$tuple()` first.
223
+ * Built-in array methods not available on {@link Result} types.
494
224
  *
495
225
  * @deprecated
496
226
  */
@@ -498,8 +228,7 @@ declare class RetupleArray<T> extends Array<T> {
498
228
  /**
499
229
  * ## Method not available
500
230
  *
501
- * Built-in array methods not available on `Result` types, convert the result
502
- * to a tuple using `$tuple()` first.
231
+ * Built-in array methods not available on {@link Result} types.
503
232
  *
504
233
  * @deprecated
505
234
  */
@@ -507,8 +236,7 @@ declare class RetupleArray<T> extends Array<T> {
507
236
  /**
508
237
  * ## Method not available
509
238
  *
510
- * Built-in array methods not available on `Result` types, convert the result
511
- * to a tuple using `$tuple()` first.
239
+ * Built-in array methods not available on {@link Result} types.
512
240
  *
513
241
  * @deprecated
514
242
  */
@@ -516,8 +244,7 @@ declare class RetupleArray<T> extends Array<T> {
516
244
  /**
517
245
  * ## Method not available
518
246
  *
519
- * Built-in array methods not available on `Result` types, convert the result
520
- * to a tuple using `$tuple()` first.
247
+ * Built-in array methods not available on {@link Result} types.
521
248
  *
522
249
  * @deprecated
523
250
  */
@@ -525,8 +252,7 @@ declare class RetupleArray<T> extends Array<T> {
525
252
  /**
526
253
  * ## Method not available
527
254
  *
528
- * Built-in array methods not available on `Result` types, convert the result
529
- * to a tuple using `$tuple()` first.
255
+ * Built-in array methods not available on {@link Result} types.
530
256
  *
531
257
  * @deprecated
532
258
  */
@@ -534,8 +260,7 @@ declare class RetupleArray<T> extends Array<T> {
534
260
  /**
535
261
  * ## Method not available
536
262
  *
537
- * Built-in array methods not available on `Result` types, convert the result
538
- * to a tuple using `$tuple()` first.
263
+ * Built-in array methods not available on {@link Result} types.
539
264
  *
540
265
  * @deprecated
541
266
  */
@@ -543,8 +268,7 @@ declare class RetupleArray<T> extends Array<T> {
543
268
  /**
544
269
  * ## Method not available
545
270
  *
546
- * Built-in array methods not available on `Result` types, convert the result
547
- * to a tuple using `$tuple()` first.
271
+ * Built-in array methods not available on {@link Result} types.
548
272
  *
549
273
  * @deprecated
550
274
  */
@@ -552,8 +276,7 @@ declare class RetupleArray<T> extends Array<T> {
552
276
  /**
553
277
  * ## Method not available
554
278
  *
555
- * Built-in array methods not available on `Result` types, convert the result
556
- * to a tuple using `$tuple()` first.
279
+ * Built-in array methods not available on {@link Result} types.
557
280
  *
558
281
  * @deprecated
559
282
  */
@@ -561,8 +284,7 @@ declare class RetupleArray<T> extends Array<T> {
561
284
  /**
562
285
  * ## Method not available
563
286
  *
564
- * Built-in array methods not available on `Result` types, convert the result
565
- * to a tuple using `$tuple()` first.
287
+ * Built-in array methods not available on {@link Result} types.
566
288
  *
567
289
  * @deprecated
568
290
  */
@@ -570,8 +292,7 @@ declare class RetupleArray<T> extends Array<T> {
570
292
  /**
571
293
  * ## Method not available
572
294
  *
573
- * Built-in array methods not available on `Result` types, convert the result
574
- * to a tuple using `$tuple()` first.
295
+ * Built-in array methods not available on {@link Result} types.
575
296
  *
576
297
  * @deprecated
577
298
  */
@@ -579,8 +300,7 @@ declare class RetupleArray<T> extends Array<T> {
579
300
  /**
580
301
  * ## Method not available
581
302
  *
582
- * Built-in array methods not available on `Result` types, convert the result
583
- * to a tuple using `$tuple()` first.
303
+ * Built-in array methods not available on {@link Result} types.
584
304
  *
585
305
  * @deprecated
586
306
  */
@@ -588,8 +308,7 @@ declare class RetupleArray<T> extends Array<T> {
588
308
  /**
589
309
  * ## Method not available
590
310
  *
591
- * Built-in array methods not available on `Result` types, convert the result
592
- * to a tuple using `$tuple()` first.
311
+ * Built-in array methods not available on {@link Result} types.
593
312
  *
594
313
  * @deprecated
595
314
  */
@@ -597,8 +316,7 @@ declare class RetupleArray<T> extends Array<T> {
597
316
  /**
598
317
  * ## Method not available
599
318
  *
600
- * Built-in array methods not available on `Result` types, convert the result
601
- * to a tuple using `$tuple()` first.
319
+ * Built-in array methods not available on {@link Result} types.
602
320
  *
603
321
  * @deprecated
604
322
  */
@@ -606,8 +324,7 @@ declare class RetupleArray<T> extends Array<T> {
606
324
  /**
607
325
  * ## Method not available
608
326
  *
609
- * Built-in array methods not available on `Result` types, convert the result
610
- * to a tuple using `$tuple()` first.
327
+ * Built-in array methods not available on {@link Result} types.
611
328
  *
612
329
  * @deprecated
613
330
  */
@@ -615,8 +332,7 @@ declare class RetupleArray<T> extends Array<T> {
615
332
  /**
616
333
  * ## Method not available
617
334
  *
618
- * Built-in array methods not available on `Result` types, convert the result
619
- * to a tuple using `$tuple()` first.
335
+ * Built-in array methods not available on {@link Result} types.
620
336
  *
621
337
  * @deprecated
622
338
  */
@@ -624,8 +340,7 @@ declare class RetupleArray<T> extends Array<T> {
624
340
  /**
625
341
  * ## Method not available
626
342
  *
627
- * Built-in array methods not available on `Result` types, convert the result
628
- * to a tuple using `$tuple()` first.
343
+ * Built-in array methods not available on {@link Result} types.
629
344
  *
630
345
  * @deprecated
631
346
  */
@@ -633,8 +348,7 @@ declare class RetupleArray<T> extends Array<T> {
633
348
  /**
634
349
  * ## Method not available
635
350
  *
636
- * Built-in array methods not available on `Result` types, convert the result
637
- * to a tuple using `$tuple()` first.
351
+ * Built-in array methods not available on {@link Result} types.
638
352
  *
639
353
  * @deprecated
640
354
  */
@@ -642,8 +356,7 @@ declare class RetupleArray<T> extends Array<T> {
642
356
  /**
643
357
  * ## Method not available
644
358
  *
645
- * Built-in array methods not available on `Result` types, convert the result
646
- * to a tuple using `$tuple()` first.
359
+ * Built-in array methods not available on {@link Result} types.
647
360
  *
648
361
  * @deprecated
649
362
  */
@@ -651,8 +364,7 @@ declare class RetupleArray<T> extends Array<T> {
651
364
  /**
652
365
  * ## Method not available
653
366
  *
654
- * Built-in array methods not available on `Result` types, convert the result
655
- * to a tuple using `$tuple()` first.
367
+ * Built-in array methods not available on {@link Result} types.
656
368
  *
657
369
  * @deprecated
658
370
  */
@@ -660,8 +372,7 @@ declare class RetupleArray<T> extends Array<T> {
660
372
  /**
661
373
  * ## Method not available
662
374
  *
663
- * Built-in array methods not available on `Result` types, convert the result
664
- * to a tuple using `$tuple()` first.
375
+ * Built-in array methods not available on {@link Result} types.
665
376
  *
666
377
  * @deprecated
667
378
  */
@@ -669,8 +380,7 @@ declare class RetupleArray<T> extends Array<T> {
669
380
  /**
670
381
  * ## Method not available
671
382
  *
672
- * Built-in array methods not available on `Result` types, convert the result
673
- * to a tuple using `$tuple()` first.
383
+ * Built-in array methods not available on {@link Result} types.
674
384
  *
675
385
  * @deprecated
676
386
  */
@@ -678,8 +388,7 @@ declare class RetupleArray<T> extends Array<T> {
678
388
  /**
679
389
  * ## Method not available
680
390
  *
681
- * Built-in array methods not available on `Result` types, convert the result
682
- * to a tuple using `$tuple()` first.
391
+ * Built-in array methods not available on {@link Result} types.
683
392
  *
684
393
  * @deprecated
685
394
  */
@@ -687,8 +396,7 @@ declare class RetupleArray<T> extends Array<T> {
687
396
  /**
688
397
  * ## Method not available
689
398
  *
690
- * Built-in array methods not available on `Result` types, convert the result
691
- * to a tuple using `$tuple()` first.
399
+ * Built-in array methods not available on {@link Result} types.
692
400
  *
693
401
  * @deprecated
694
402
  */
@@ -696,8 +404,7 @@ declare class RetupleArray<T> extends Array<T> {
696
404
  /**
697
405
  * ## Method not available
698
406
  *
699
- * Built-in array methods not available on `Result` types, convert the result
700
- * to a tuple using `$tuple()` first.
407
+ * Built-in array methods not available on {@link Result} types.
701
408
  *
702
409
  * @deprecated
703
410
  */
@@ -705,8 +412,7 @@ declare class RetupleArray<T> extends Array<T> {
705
412
  /**
706
413
  * ## Method not available
707
414
  *
708
- * Built-in array methods not available on `Result` types, convert the result
709
- * to a tuple using `$tuple()` first.
415
+ * Built-in array methods not available on {@link Result} types.
710
416
  *
711
417
  * @deprecated
712
418
  */
@@ -714,8 +420,7 @@ declare class RetupleArray<T> extends Array<T> {
714
420
  /**
715
421
  * ## Method not available
716
422
  *
717
- * Built-in array methods not available on `Result` types, convert the result
718
- * to a tuple using `$tuple()` first.
423
+ * Built-in array methods not available on {@link Result} types.
719
424
  *
720
425
  * @deprecated
721
426
  */
@@ -723,8 +428,7 @@ declare class RetupleArray<T> extends Array<T> {
723
428
  /**
724
429
  * ## Method not available
725
430
  *
726
- * Built-in array methods not available on `Result` types, convert the result
727
- * to a tuple using `$tuple()` first.
431
+ * Built-in array methods not available on {@link Result} types.
728
432
  *
729
433
  * @deprecated
730
434
  */
@@ -732,8 +436,7 @@ declare class RetupleArray<T> extends Array<T> {
732
436
  /**
733
437
  * ## Method not available
734
438
  *
735
- * Built-in array methods not available on `Result` types, convert the result
736
- * to a tuple using `$tuple()` first.
439
+ * Built-in array methods not available on {@link Result} types.
737
440
  *
738
441
  * @deprecated
739
442
  */
@@ -741,8 +444,7 @@ declare class RetupleArray<T> extends Array<T> {
741
444
  /**
742
445
  * ## Method not available
743
446
  *
744
- * Built-in array methods not available on `Result` types, convert the result
745
- * to a tuple using `$tuple()` first.
447
+ * Built-in array methods not available on {@link Result} types.
746
448
  *
747
449
  * @deprecated
748
450
  */
@@ -750,8 +452,7 @@ declare class RetupleArray<T> extends Array<T> {
750
452
  /**
751
453
  * ## Method not available
752
454
  *
753
- * Built-in array methods not available on `Result` types, convert the result
754
- * to a tuple using `$tuple()` first.
455
+ * Built-in array methods not available on {@link Result} types.
755
456
  *
756
457
  * @deprecated
757
458
  */
@@ -759,8 +460,7 @@ declare class RetupleArray<T> extends Array<T> {
759
460
  /**
760
461
  * ## Method not available
761
462
  *
762
- * Built-in array methods not available on `Result` types, convert the result
763
- * to a tuple using `$tuple()` first.
463
+ * Built-in array methods not available on {@link Result} types.
764
464
  *
765
465
  * @deprecated
766
466
  */
@@ -768,8 +468,7 @@ declare class RetupleArray<T> extends Array<T> {
768
468
  /**
769
469
  * ## Method not available
770
470
  *
771
- * Built-in array methods not available on `Result` types, convert the result
772
- * to a tuple using `$tuple()` first.
471
+ * Built-in array methods not available on {@link Result} types.
773
472
  *
774
473
  * @deprecated
775
474
  */
@@ -777,8 +476,7 @@ declare class RetupleArray<T> extends Array<T> {
777
476
  /**
778
477
  * ## Method not available
779
478
  *
780
- * Built-in array methods not available on `Result` types, convert the result
781
- * to a tuple using `$tuple()` first.
479
+ * Built-in array methods not available on {@link Result} types.
782
480
  *
783
481
  * @deprecated
784
482
  */
@@ -786,8 +484,7 @@ declare class RetupleArray<T> extends Array<T> {
786
484
  /**
787
485
  * ## Method not available
788
486
  *
789
- * Built-in array methods not available on `Result` types, convert the result
790
- * to a tuple using `$tuple()` first.
487
+ * Built-in array methods not available on {@link Result} types.
791
488
  *
792
489
  * @deprecated
793
490
  */
@@ -795,8 +492,7 @@ declare class RetupleArray<T> extends Array<T> {
795
492
  /**
796
493
  * ## Method not available
797
494
  *
798
- * Built-in array methods not available on `Result` types, convert the result
799
- * to a tuple using `$tuple()` first.
495
+ * Built-in array methods not available on {@link Result} types.
800
496
  *
801
497
  * @deprecated
802
498
  */
@@ -835,65 +531,77 @@ declare class ResultAsync<T, E> {
835
531
  */
836
532
  $unwrapOrElse<U = T>(this: ResultAsync<T, E>, f: () => U): Promise<T | U>;
837
533
  /**
838
- * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
534
+ * The same as {@link Retuple.$map|$map}, except it returns
535
+ * {@link ResultAsync}.
839
536
  */
840
537
  $map<U>(this: ResultAsync<T, E>, f: (val: T) => U): ResultAsync<U, E>;
841
538
  /**
842
539
  * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
843
- * `ResultAsync`.
540
+ * {@link ResultAsync}.
844
541
  */
845
542
  $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
846
543
  /**
847
- * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
544
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
545
+ * {@link ResultAsync}.
848
546
  */
849
547
  $mapOr<U, V = U>(this: ResultAsync<T, E>, def: U, f: (val: T) => V): ResultAsync<U | V, never>;
850
548
  /**
851
549
  * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
852
- * `ResultAsync`.
550
+ * {@link ResultAsync}.
853
551
  */
854
552
  $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
855
553
  /**
856
554
  * The same as {@link Retuple.$andAssertOr|$andAssertOr}, except it:
857
555
  *
858
556
  * - can also accept a `PromiseLike` default value;
859
- * - returns `ResultAsync`.
557
+ * - returns {@link ResultAsync}.
860
558
  */
861
- $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
862
- $andAssertOr<U = T, F = E, A extends T = T>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
863
- $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
559
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: ResultLikeAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
560
+ $andAssertOr<U = T, F = E, A extends T = T>(this: ResultAsync<T, E>, def: ResultLikeAwaitable<U, F>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
561
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: ResultLikeAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
864
562
  /**
865
563
  * The same as {@link Retuple.$andAssertOrElse|$andAssertOrElse}, except it:
866
564
  *
867
565
  * - can also accept an `async` default function;
868
- * - returns `ResultAsync`.
566
+ * - returns {@link ResultAsync}.
869
567
  */
870
- $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
871
- $andAssertOrElse<U = T, F = E, A extends T = T>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
872
- $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
568
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
569
+ $andAssertOrElse<U = T, F = E, A extends T = T>(this: ResultAsync<T, E>, def: (val: T) => ResultLikeAwaitable<U, F>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
570
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => ResultLikeAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
873
571
  /**
874
572
  * The same as {@link Retuple.$or|$or}, except it:
875
573
  *
876
574
  * - can also accept a `PromiseLike` or value;
877
- * - returns `ResultAsync`.
575
+ * - returns {@link ResultAsync}.
878
576
  */
879
- $or<U = T, F = E>(this: ResultAsync<T, E>, or: Retuple<U, F> | PromiseLike<Retuple<U, F>>): ResultAsync<T | U, F>;
577
+ $or<U = T, F = E>(this: ResultAsync<T, E>, or: ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
880
578
  /**
881
579
  * The same as {@link Retuple.$orElse|$orElse}, except it:
882
580
  *
883
581
  * - can also accept an `async` or function;
884
- * - returns `ResultAsync`.
582
+ * - returns {@link ResultAsync}.
885
583
  */
886
- $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => RetupleAwaitable<U, F>): ResultAsync<T | U, F>;
584
+ $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
887
585
  /**
888
586
  * The same as {@link Retuple.$orSafe|$orSafe}, except it:
889
587
  *
890
588
  * - can also accept an `async` safe function;
891
- * - returns `ResultAsync`.
589
+ * - returns {@link ResultAsync}.
892
590
  */
893
591
  $orSafe<U = T>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, Error>;
894
592
  $orSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
895
593
  /**
896
- * @TODO
594
+ * Returns {@link ResultAsync} based on the outcome of the promise when this
595
+ * result is `Err`.
596
+ *
597
+ * Otherwise, returns `Ok` containing the current contained value.
598
+ *
599
+ * Uses the same strategy as {@link Result.$safePromise}, equivalent to
600
+ * calling:
601
+ *
602
+ * ```ts
603
+ * resultAsync.$orElse(() => Result.$safePromise(...))
604
+ * ```
897
605
  */
898
606
  $orSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<T | U, Error>;
899
607
  $orSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
@@ -901,33 +609,43 @@ declare class ResultAsync<T, E> {
901
609
  * The same as {@link Retuple.$and|$and}, except it:
902
610
  *
903
611
  * - can also accept a `PromiseLike` and value;
904
- * - returns `ResultAsync`.
612
+ * - returns {@link ResultAsync}.
905
613
  */
906
- $and<U = T, F = E>(this: ResultAsync<T, E>, and: RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
614
+ $and<U = T, F = E>(this: ResultAsync<T, E>, and: ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
907
615
  /**
908
616
  * The same as {@link Retuple.$andThen|$andThen}, except it:
909
617
  *
910
618
  * - can also accept an `async` and function;
911
- * - returns `ResultAsync`.
619
+ * - returns {@link ResultAsync}.
912
620
  */
913
- $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
621
+ $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
914
622
  /**
915
623
  * The same as {@link Retuple.$andThrough|$andThrough}, except it:
916
624
  *
917
625
  * - can also accept an `async` through function;
918
- * - returns `ResultAsync`.
626
+ * - returns {@link ResultAsync}.
919
627
  */
920
- $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<any, F>): ResultAsync<T, E | F>;
628
+ $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<any, F>): ResultAsync<T, E | F>;
921
629
  /**
922
630
  * The same as {@link Retuple.$andSafe|$andSafe}, except it:
923
631
  *
924
632
  * - can also accept an `async` safe function;
925
- * - returns `ResultAsync`.
633
+ * - returns {@link ResultAsync}.
926
634
  */
927
635
  $andSafe<U = T>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
928
636
  $andSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
929
637
  /**
930
- * @TODO
638
+ * Returns {@link ResultAsync} based on the outcome of the promise when this
639
+ * result is `Ok`.
640
+ *
641
+ * Otherwise, returns `Err` containing the current error value.
642
+ *
643
+ * Uses the same strategy as {@link Result.$safePromise}, equivalent to
644
+ * calling:
645
+ *
646
+ * ```ts
647
+ * resultAsync.$andThen(() => Result.$safePromise(...))
648
+ * ```
931
649
  */
932
650
  $andSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<U, E | Error>;
933
651
  $andSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
@@ -935,21 +653,21 @@ declare class ResultAsync<T, E> {
935
653
  * The same as {@link Retuple.$peek|$peek}, except it:
936
654
  *
937
655
  * - awaits the peek function;
938
- * - returns `ResultAsync`.
656
+ * - returns {@link ResultAsync}.
939
657
  */
940
658
  $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
941
659
  /**
942
660
  * The same as {@link Retuple.$tap|$tap}, except it:
943
661
  *
944
662
  * - awaits the tap function;
945
- * - returns `ResultAsync`.
663
+ * - returns {@link ResultAsync}.
946
664
  */
947
665
  $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
948
666
  /**
949
667
  * The same as {@link Retuple.$tapErr|$tapErr}, except it:
950
668
  *
951
669
  * - awaits the tap error function;
952
- * - returns `ResultAsync`.
670
+ * - returns {@link ResultAsync}.
953
671
  */
954
672
  $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
955
673
  /**
@@ -957,21 +675,12 @@ declare class ResultAsync<T, E> {
957
675
  */
958
676
  $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
959
677
  /**
960
- * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
961
- */
962
- $tuple(this: ResultAsync<T, E>): Promise<[err: E | undefined, value: T | undefined]>;
963
- /**
964
- * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
678
+ * The same as {@link Retuple.$iter|$iter}, except it returns a `Promise`.
965
679
  */
966
680
  $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
967
681
  }
968
- interface ResultRetryMonitor<E> {
969
- error: E;
970
- attempt: number;
971
- abort: () => void;
972
- }
973
682
  /**
974
- * @TODO
683
+ * ## ResultRetry
975
684
  */
976
685
  declare class ResultRetry<T, E> implements PromiseLike<Result<T, E>> {
977
686
  #private;
@@ -980,28 +689,118 @@ declare class ResultRetry<T, E> implements PromiseLike<Result<T, E>> {
980
689
  private static zero;
981
690
  private static delay;
982
691
  private static integer;
983
- constructor(f: () => RetupleAwaitable<T, E>);
984
- then<U = Result<T, E>, F = never>(onfulfilled?: ((value: Result<T, E>) => U | PromiseLike<U>) | null | undefined, onrejected?: ((reason: any) => F | PromiseLike<F>) | null | undefined): PromiseLike<U | F>;
692
+ constructor(f: () => ResultLikeAwaitable<T, E>);
693
+ then<U = Result<T, E>, F = never>(this: ResultRetry<T, E>, onfulfilled?: ((value: Result<T, E>) => U | PromiseLike<U>) | null | undefined, onrejected?: ((reason: any) => F | PromiseLike<F>) | null | undefined): PromiseLike<U | F>;
985
694
  /**
986
- * @TODO - Capped 100
695
+ * Sets the maximum number of times the retry function can be executed,
696
+ * mutating this `ResultRetry` instance.
697
+ *
698
+ * **The default value is 1 - meaning that unless set, no retries will be
699
+ * attempted.**
700
+ *
701
+ * The retry function can be called up to the maximum number of times until
702
+ * it returns `Ok`. If it never returns `Ok`, the most recent `Err` is
703
+ * returned.
704
+ *
705
+ * This function accepts a positive integer between 1 and 100:
706
+ *
707
+ * - Integers outside of this range are clamped to the nearest valid value;
708
+ * - Any other value (NaN, Infinity, fractions, strings) are treated as 1.
709
+ *
710
+ * @example
711
+ *
712
+ * ```ts
713
+ * // Retry someResultFn up to 3 times until Ok is returned:
714
+ * const result = await Result.$retry(someResultFn).$times(3);
715
+ * ```
987
716
  */
988
717
  $times<N extends number>(this: ResultRetry<T, E>, times: NonZero<N> & NonNegativeOrDecimal<N>): ResultRetry<T, E>;
989
718
  /**
990
- * @TODO - Capped 1 hour
719
+ * Sets the delay between each retry attempt, mutating this `ResultRetry`
720
+ * instance.
721
+ *
722
+ * - Provide a number of milliseconds to introduce a static delay between
723
+ * attempts;
724
+ * - Provide a function to compute the delay dynamically for each attempt;
725
+ * - If the maximum number of retries is 1, this setting as no effect.
726
+ *
727
+ * **The default value is 0 - meaning that unless set, there will be no delay
728
+ * between attempts.**
729
+ *
730
+ * This function accepts an integer between 0 and 3600000:
731
+ *
732
+ * - Integers outside of this range are clamped to the nearest valid value;
733
+ * - Any other value (NaN, Infinity, fractions, strings) are treated as 0.
734
+ *
735
+ * @example
736
+ *
737
+ * ```ts
738
+ * // Retry someResultFn up to 3 times until Ok is returned,
739
+ * // with a 1 second delay between each invocation:
740
+ * const result = await Result.$retry(someResultFn).$times(3).$delay(1000);
741
+ * ```
991
742
  */
992
743
  $delay<N extends number>(this: ResultRetry<T, E>, f: (attempt: number) => NonNegativeOrDecimal<N>): ResultRetry<T, E>;
993
744
  $delay<N extends number>(this: ResultRetry<T, E>, ms: NonNegativeOrDecimal<N>): ResultRetry<T, E>;
994
745
  /**
995
- * @TODO
746
+ * Sets a handler to be called when an attempt returns `Err`, mutating this
747
+ * `ResultRetry` instance. The handler can be used to capture information
748
+ * about each failure, and to abort early and prevent further retries.
749
+ *
750
+ * The handler function is called with `ResultRetryHandleState`, containing:
751
+ *
752
+ * - **error** - The error value from the last failed attempt;
753
+ * - **attempt** - The attempt number;
754
+ * - **abort** - A function which when called, prevents further retries.
755
+ *
756
+ * @example
757
+ *
758
+ * ```ts
759
+ * // Retry someResultFn up to 3 times until Ok is returned, logging each
760
+ * // attempt and aborting early if the error code is "UNAUTHORIZED".
761
+ * const result = await Result.$retry(someResultFn)
762
+ * .$times(3)
763
+ * .$handle(({ error, attempt, abort }) => {
764
+ * console.info(`Attempt ${attempt} failed: ${error}`);
765
+ * if (error === "UNAUTHORIZED") {
766
+ * abort();
767
+ * }
768
+ * });
769
+ * ```
996
770
  */
997
- $monitor(f: (state: ResultRetryMonitor<E>) => void): ResultRetry<T, E>;
771
+ $handle(f: (controller: ResultRetryController<E>) => void): ResultRetry<T, E>;
998
772
  /**
999
- * @TODO
773
+ * Returns {@link ResultAsync} which resolves to this retried {@link Result}.
774
+ *
775
+ * @example
776
+ *
777
+ * ```ts
778
+ * const result: Result<string, SomeError> = await Result
779
+ * .$retry(someResultFn)
780
+ * .$times(3)
781
+ * .$delay(100)
782
+ * .$async()
783
+ * .$andThen((message) => `Success: ${message}`)
784
+ * .$mapErr((code) => new SomeError({ code }));
785
+ * ```
1000
786
  */
1001
787
  $async(this: ResultRetry<T, E>): ResultAsync<T, E>;
788
+ /**
789
+ * Returns a `Promise` which resolves to this retried {@link Result}.
790
+ *
791
+ * @example
792
+ *
793
+ * ```ts
794
+ * const promise: Promise<Result<string, Error>> = Result
795
+ * .$retry(someResultFn)
796
+ * .$times(3)
797
+ * .$promise();
798
+ * ```
799
+ */
800
+ $promise(this: ResultRetry<T, E>): Promise<Result<T, E>>;
1002
801
  private drain;
1003
802
  }
1004
- interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
803
+ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E> {
1005
804
  /**
1006
805
  * Returns true when this result is `Ok`. Acts as a type guard.
1007
806
  *
@@ -1377,9 +1176,9 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1377
1176
  * assert.equal(asserted.$unwrapErr(), "test");
1378
1177
  * ```
1379
1178
  */
1380
- $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T> | U, E | F>;
1381
- $andAssertOr<U = T, F = E, A extends T = T>(this: Result<T, E>, def: Result<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
1382
- $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1179
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: ResultLike<U, F>): Result<Truthy<T> | U, E | F>;
1180
+ $andAssertOr<U = T, F = E, A extends T = T>(this: Result<T, E>, def: ResultLike<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
1181
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: ResultLike<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1383
1182
  /**
1384
1183
  * Performs an assertion when this result is `Ok`:
1385
1184
  *
@@ -1460,9 +1259,9 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1460
1259
  * assert.equal(asserted.$unwrapErr(), "test");
1461
1260
  * ```
1462
1261
  */
1463
- $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>): Result<Truthy<T> | U, E | F>;
1464
- $andAssertOrElse<U = T, F = E, A extends T = T>(this: Result<T, E>, def: (val: T) => Result<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
1465
- $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1262
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => ResultLike<U, F>): Result<Truthy<T> | U, E | F>;
1263
+ $andAssertOrElse<U = T, F = E, A extends T = T>(this: Result<T, E>, def: (val: T) => ResultLike<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
1264
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => ResultLike<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1466
1265
  /**
1467
1266
  * Returns `Ok` containing the return value of the map function when this
1468
1267
  * result is `Ok`.
@@ -1613,7 +1412,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1613
1412
  * );
1614
1413
  * ```
1615
1414
  */
1616
- $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, F>;
1415
+ $or<U = T, F = E>(this: Result<T, E>, or: ResultLike<U, F>): Result<T | U, F>;
1617
1416
  /**
1618
1417
  * Returns the result returned by the or function, when this result is `Err`.
1619
1418
  *
@@ -1649,10 +1448,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1649
1448
  * );
1650
1449
  * ```
1651
1450
  */
1652
- $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, F>;
1451
+ $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLike<U, F>): Result<T | U, F>;
1653
1452
  /**
1654
- * Returns a `Result` based on the outcome of the safe function when this
1655
- * result is `Err`.
1453
+ * Returns a {@link Result} based on the outcome of the safe function when
1454
+ * this result is `Err`.
1656
1455
  *
1657
1456
  * Otherwise, returns `Ok` containing the current ok value.
1658
1457
  *
@@ -1696,7 +1495,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1696
1495
  * );
1697
1496
  * ```
1698
1497
  */
1699
- $and<U = T, F = E>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
1498
+ $and<U = T, F = E>(this: Result<T, E>, and: ResultLike<U, F>): Result<U, E | F>;
1700
1499
  /**
1701
1500
  * Returns the and result, when this result is `Ok`.
1702
1501
  *
@@ -1732,7 +1531,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1732
1531
  * );
1733
1532
  * ```
1734
1533
  */
1735
- $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
1534
+ $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
1736
1535
  /**
1737
1536
  * Calls the through function when this result is `Ok` and returns:
1738
1537
  *
@@ -1772,23 +1571,24 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1772
1571
  * );
1773
1572
  * ```
1774
1573
  */
1775
- $andThrough<F = E>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
1574
+ $andThrough<F = E>(this: Result<T, E>, f: (val: T) => ResultLike<any, F>): Result<T, E | F>;
1776
1575
  /**
1777
1576
  * Returns a result based on the outcome of the safe function when this
1778
1577
  * result is `Ok`.
1779
1578
  *
1780
1579
  * Otherwise, returns `Err` containing the current error value.
1781
1580
  *
1782
- * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1783
- * `result.$and(Result.$safe(...))`.
1581
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling:
1582
+ *
1583
+ * ```ts
1584
+ * result.$andThen(() => Result.$safe(...))
1585
+ * ```
1784
1586
  */
1785
1587
  $andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
1786
1588
  $andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
1787
1589
  /**
1788
- * @TODO
1789
- */
1790
- /**
1791
- * Calls the peek function and returns `Result` equivalent to this result.
1590
+ * Calls the peek function and returns {@link Result} equivalent to this
1591
+ * result.
1792
1592
  *
1793
1593
  * @example
1794
1594
  *
@@ -1884,7 +1684,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1884
1684
  */
1885
1685
  $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
1886
1686
  /**
1887
- * Returns the contained `Result` when this result is `Ok`.
1687
+ * Returns the contained {@link Result} when this result is `Ok`.
1888
1688
  *
1889
1689
  * Otherwise returns `Err` containing the current error value.
1890
1690
  *
@@ -1915,7 +1715,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1915
1715
  */
1916
1716
  $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
1917
1717
  /**
1918
- * Returns an equivalent `ResultAsync`.
1718
+ * Returns an equivalent {@link ResultAsync}.
1919
1719
  *
1920
1720
  * @example
1921
1721
  *
@@ -1949,24 +1749,6 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1949
1749
  * ```
1950
1750
  */
1951
1751
  $promise(this: Result<T, E>): Promise<Result<T, E>>;
1952
- /**
1953
- * Returns a two-element, standard array tuple equivalent to this result.
1954
- *
1955
- * @example
1956
- *
1957
- * ```ts
1958
- * const result = Ok("test");
1959
- * assert.deepEqual(result.$tuple(), [undefined, "test"]);
1960
- * ```
1961
- *
1962
- * @example
1963
- *
1964
- * ```ts
1965
- * const result = Err("test");
1966
- * assert.deepEqual(result.$tuple(), ["test", undefined]);
1967
- * ```
1968
- */
1969
- $tuple(this: Result<T, E>): [err: E | undefined, value: T | undefined];
1970
1752
  /**
1971
1753
  * Returns an `IterableIterator` over the contained ok value, when this
1972
1754
  * result is `Ok`.
@@ -2013,9 +1795,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
2013
1795
  }
2014
1796
  type OkTuple<T> = [err: undefined, value: T];
2015
1797
  type ErrTuple<E> = [err: E, value: undefined];
2016
- type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
2017
- type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
2018
- type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
1798
+ type ResultLikeAwaitable<T, E> = ResultLike<T, E> | PromiseLike<ResultLike<T, E>>;
2019
1799
  type ObjectUnionOk<T> = {
2020
1800
  success: true;
2021
1801
  data: T;