retuple 1.0.0-next.12 → 1.0.0-next.14

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,16 @@
1
+ import { ResultLikeSymbol } from "./symbol.cjs";
1
2
  export type Ok = typeof Ok;
2
3
  export type Err = typeof Err;
3
4
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
4
- export { type ResultAsync };
5
+ export type ResultLike<T, E> = {
6
+ [ResultLikeSymbol](): Result<T, E>;
7
+ };
8
+ export { type ResultAsync, type ResultRetry };
9
+ export interface ResultRetryController<E> {
10
+ error: E;
11
+ attempt: number;
12
+ abort: () => void;
13
+ }
5
14
  /**
6
15
  * ## Retuple Unwrap Failed
7
16
  *
@@ -30,16 +39,6 @@ export declare class RetupleExpectFailed<const E = unknown> extends Error {
30
39
  value: E;
31
40
  constructor(value: E);
32
41
  }
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
42
  /**
44
43
  * ## Retuple Thrown Value Error
45
44
  *
@@ -47,21 +46,10 @@ export declare class RetupleFlattenFailed<const T = unknown> extends Error {
47
46
  * thrown error or rejected value is not an instance of `Error`, and when no
48
47
  * map error function is provided.
49
48
  */
50
- export declare class RetupleThrownValueError extends Error {
49
+ export declare class RetupleCaughtValueError extends Error {
51
50
  value: unknown;
52
51
  constructor(value: unknown);
53
52
  }
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
53
  /**
66
54
  * ## Retuple Invalid Union Error
67
55
  *
@@ -88,17 +76,38 @@ export declare class RetupleArrayMethodUnavailableError extends Error {
88
76
  *
89
77
  * @TODO
90
78
  */
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;
79
+ export declare function Result<T, E>(resultLike: ResultLike<T, E>): Result<T, E>;
92
80
  export declare namespace Result {
93
- var Ok: typeof import(".").Ok;
94
- 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;
81
+ var Ok: typeof import("./index.js").Ok;
82
+ var Err: typeof import("./index.js").Err;
83
+ var $from: <T, E>(result: ResultLike<T, E>) => Result<T, E>;
84
+ var $resolve: <T, E>(result: ResultLikeAwaitable<T, E>) => ResultAsync<T, E>;
85
+ var $nonNullable: {
86
+ <const T>(value: T): Result<NonNullable<T>, true>;
87
+ <const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
88
+ };
89
+ var $truthy: {
90
+ <const T>(value: T): Result<Truthy<T>, true>;
91
+ <const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
92
+ };
93
+ 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>;
94
+ var $safe: {
95
+ <T>(f: () => Awaited<T>): Result<T, Error>;
96
+ <T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
97
+ };
98
+ var $safeAsync: {
99
+ <T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
100
+ <T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
101
+ };
102
+ var $safePromise: {
103
+ <T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
104
+ <T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
105
+ };
106
+ var $retry: <T, E>(f: () => ResultLike<T, E> | PromiseLike<ResultLike<T, E>>) => ResultRetry<T, E>;
107
+ var $safeRetry: {
108
+ <T>(f: () => T | PromiseLike<T>): ResultRetry<T, Error>;
109
+ <T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultRetry<T, E>;
110
+ };
102
111
  }
103
112
  /**
104
113
  * Create a new {@link Result} with the `Ok` variant. When called without
@@ -142,299 +151,6 @@ export declare function Ok<const T>(val: T): Result<T, never>;
142
151
  */
143
152
  export declare function Err(): Result<never, void>;
144
153
  export declare function Err<const E>(err: E): Result<never, E>;
145
- declare function resolve<T, E>(result: Retuple<T, E> | PromiseLike<Retuple<T, E>>): ResultAsync<T, E>;
146
- /**
147
- * Construct a {@link Result} from a value. If the value is neither null or
148
- * undefined, the result is `Ok`.
149
- *
150
- * Otherwise, the result is `Err` containing:
151
-
152
- * - the returned value from the error function when provided;
153
- * - or `true` otherwise.
154
- *
155
- * @example
156
- *
157
- * ```ts
158
- * const result: Result<User, Error> = Result.$nonNullable(
159
- * users.find((user) => user.id === currentUserId),
160
- * () => new Error("User not found"),
161
- * );
162
- * ```
163
- *
164
- * @example
165
- *
166
- * ```ts
167
- * const [err, value] = Result.$nonNullable("test");
168
- *
169
- * assert.equal(err, undefined);
170
- * assert.equal(value, "test");
171
- * ```
172
- *
173
- * @example
174
- *
175
- * ```ts
176
- * const [err, value] = Result.$nonNullable(null);
177
- *
178
- * assert.equal(err, true);
179
- * assert.equal(value, undefined);
180
- * ```
181
- *
182
- * @example
183
- *
184
- * ```ts
185
- * const [err, value] = Result.$nonNullable(null, () => "error");
186
- *
187
- * assert.equal(err, "error");
188
- * assert.equal(value, undefined);
189
- * ```
190
- */
191
- declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
192
- declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
193
- /**
194
- * Construct a {@link Result} from a value. If the value is truthy, the result
195
- * is `Ok`.
196
- *
197
- * Otherwise, the result is `Err` containing:
198
- *
199
- * - the returned value from the error function when provided;
200
- * - or `true` otherwise.
201
- *
202
- * @example
203
- *
204
- * ```ts
205
- * const result: Result<string, Error> = Result.$truthy(
206
- * username.trim(),
207
- * () => new Error("Username is empty"),
208
- * );
209
- * ```
210
- *
211
- * @example
212
- *
213
- * ```ts
214
- * const [err, value] = Result.$truthy("test");
215
- *
216
- * assert.equal(err, undefined);
217
- * assert.equal(value, "test");
218
- * ```
219
- *
220
- * @example
221
- *
222
- * ```ts
223
- * const [err, value] = Result.$truthy("");
224
- *
225
- * assert.equal(err, true);
226
- * assert.equal(value, undefined);
227
- * ```
228
- *
229
- * @example
230
- *
231
- * ```ts
232
- * const [err, value] = Result.$truthy(0, () => "error");
233
- *
234
- * assert.equal(err, "error");
235
- * assert.equal(value, undefined);
236
- * ```
237
- */
238
- declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
239
- declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
240
- 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>;
241
- /**
242
- * Construct a {@link Result} from a synchronous function call. If the function
243
- * returns without throwing, the result is `Ok`.
244
- *
245
- * Otherwise, the result is `Err` containing (in priority order):
246
- *
247
- * - the returned value from the map error function when provided;
248
- * - the thrown error when it is an instance of `Error`;
249
- * - `RetupleThrownValueError` when a non `Error` instance is thrown.
250
- *
251
- * @example
252
- *
253
- * ```ts
254
- * const result: Result<URL, Error> = Result.$safe(
255
- * () => new URL(user.url),
256
- * () => new Error("Invalid URL"),
257
- * );
258
- * ```
259
- *
260
- * @example
261
- *
262
- * ```ts
263
- * const [err, value] = Result.$safe(() => "test");
264
- *
265
- * assert.equal(err, undefined);
266
- * assert.equal(value, "test");
267
- * ```
268
- *
269
- * @example
270
- *
271
- * ```ts
272
- * const [err, value] = Result.$safe(
273
- * () => {
274
- * throw new Error("throws");
275
- * },
276
- * () => "error",
277
- * );
278
- *
279
- * assert.equal(err, "error");
280
- * assert.equal(value, undefined);
281
- * ```
282
- *
283
- * @example
284
- *
285
- * ```ts
286
- * const [err, value] = Result.$safe(
287
- * () => {
288
- * throw new Error("throws")
289
- * },
290
- * );
291
- *
292
- * assert(err instanceof Error && err.message === "throws");
293
- * assert.equal(value, undefined);
294
- * ```
295
- *
296
- * @example
297
- *
298
- * ```ts
299
- * const [err, value] = Result.$safe(() => {
300
- * throw "non error";
301
- * });
302
- *
303
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
304
- * assert.equal(value, undefined);
305
- */
306
- declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
307
- declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
308
- /**
309
- * Construct a {@link ResultAsync} from a function call. If the function returns
310
- * without throwing, and any promise returned resolves, the result is `Ok`.
311
- *
312
- * Otherwise, the result is `Err` containing (in priority order):
313
- *
314
- * - the returned value from the map error function when provided;
315
- * - the thrown/rejected error when it is an instance of `Error`;
316
- * - `RetupleThrownValueError` when throwing/rejecting with a non `Error`.
317
- *
318
- * @example
319
- *
320
- * ```ts
321
- * const result: Result<Response, Error> = await Result.$safeAsync(
322
- * () => fetch("http://example.com/api"),
323
- * () => new Error("Fetch failed"),
324
- * );
325
- * ```
326
- *
327
- * @example
328
- *
329
- * ```ts
330
- * const [err, value] = await Result.$safeAsync(async () => "test");
331
- *
332
- * assert.equal(err, undefined);
333
- * assert.equal(value, "test");
334
- * ```
335
- *
336
- * @example
337
- *
338
- * ```ts
339
- * const [err, value] = await Result.$safeAsync(
340
- * async () => {
341
- * throw new Error("throws");
342
- * },
343
- * () => "error",
344
- * );
345
- *
346
- * assert.equal(err, "error");
347
- * assert.equal(value, undefined);
348
- * ```
349
- *
350
- * @example
351
- *
352
- * ```ts
353
- * const [err, value] = await Result.$safeAsync(
354
- * async () => {
355
- * throw new Error("throws")
356
- * },
357
- * );
358
- *
359
- * assert(err instanceof Error && err.message === "throws");
360
- * assert.equal(value, undefined);
361
- * ```
362
- *
363
- * @example
364
- *
365
- * ```ts
366
- * const [err, value] = await Result.$safeAsync(async () => {
367
- * throw "non error";
368
- * });
369
- *
370
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
371
- * assert.equal(value, undefined);
372
- */
373
- declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
374
- declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
375
- /**
376
- * Construct a {@link Result} from a promise. If the promise resolves, the
377
- * result is `Ok`.
378
- *
379
- * Otherwise, the result is `Err` containing (in priority order):
380
- *
381
- * - the returned value from the map error function when provided;
382
- * - the rejected error when it is an instance of `Error`;
383
- * - `RetupleThrownValueError` when rejecting with a non `Error`.
384
- *
385
- * @example
386
- *
387
- * ```ts
388
- * const result: Result<Response, Error> = await Result.$safePromise(
389
- * fetch("http://example.com/api"),
390
- * () => new Error("Fetch failed"),
391
- * );
392
- * ```
393
- *
394
- * @example
395
- *
396
- * ```ts
397
- * const [err, value] = await Result.$safePromise(Promise.resolve("test"));
398
- *
399
- * assert.equal(err, undefined);
400
- * assert.equal(value, "test");
401
- * ```
402
- *
403
- * @example
404
- *
405
- * ```ts
406
- * const [err, value] = await Result.$safePromise(
407
- * Promise.reject(new Error("rejects")),
408
- * () => "error",
409
- * );
410
- *
411
- * assert.equal(err, "error");
412
- * assert.equal(value, undefined);
413
- * ```
414
- *
415
- * @example
416
- *
417
- * ```ts
418
- * const [err, value] = await Result.$safePromise(
419
- * Promise.reject(new Error("rejects")),
420
- * );
421
- *
422
- * assert(err instanceof Error && err.message === "rejects");
423
- * assert.equal(value, undefined);
424
- * ```
425
- *
426
- * @example
427
- *
428
- * ```ts
429
- * const [err, value] = await Result.$safeAsync(
430
- * Promise.reject("non error"),
431
- * );
432
- *
433
- * assert(err instanceof RetupleThrownValueError && err.value === "non error");
434
- * assert.equal(value, undefined);
435
- */
436
- declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
437
- declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
438
154
  /**
439
155
  * ## RetupleArray
440
156
  *
@@ -445,8 +161,7 @@ declare class RetupleArray<T> extends Array<T> {
445
161
  /**
446
162
  * ## Method not available
447
163
  *
448
- * Built-in array methods not available on `Result` types, convert the result
449
- * to a tuple using `$tuple()` first.
164
+ * Built-in array methods not available on {@link Result} types.
450
165
  *
451
166
  * @deprecated
452
167
  */
@@ -454,8 +169,7 @@ declare class RetupleArray<T> extends Array<T> {
454
169
  /**
455
170
  * ## Method not available
456
171
  *
457
- * Built-in array methods not available on `Result` types, convert the result
458
- * to a tuple using `$tuple()` first.
172
+ * Built-in array methods not available on {@link Result} types.
459
173
  *
460
174
  * @deprecated
461
175
  */
@@ -463,8 +177,7 @@ declare class RetupleArray<T> extends Array<T> {
463
177
  /**
464
178
  * ## Method not available
465
179
  *
466
- * Built-in array methods not available on `Result` types, convert the result
467
- * to a tuple using `$tuple()` first.
180
+ * Built-in array methods not available on {@link Result} types.
468
181
  *
469
182
  * @deprecated
470
183
  */
@@ -472,8 +185,7 @@ declare class RetupleArray<T> extends Array<T> {
472
185
  /**
473
186
  * ## Method not available
474
187
  *
475
- * Built-in array methods not available on `Result` types, convert the result
476
- * to a tuple using `$tuple()` first.
188
+ * Built-in array methods not available on {@link Result} types.
477
189
  *
478
190
  * @deprecated
479
191
  */
@@ -481,8 +193,7 @@ declare class RetupleArray<T> extends Array<T> {
481
193
  /**
482
194
  * ## Method not available
483
195
  *
484
- * Built-in array methods not available on `Result` types, convert the result
485
- * to a tuple using `$tuple()` first.
196
+ * Built-in array methods not available on {@link Result} types.
486
197
  *
487
198
  * @deprecated
488
199
  */
@@ -490,8 +201,7 @@ declare class RetupleArray<T> extends Array<T> {
490
201
  /**
491
202
  * ## Method not available
492
203
  *
493
- * Built-in array methods not available on `Result` types, convert the result
494
- * to a tuple using `$tuple()` first.
204
+ * Built-in array methods not available on {@link Result} types.
495
205
  *
496
206
  * @deprecated
497
207
  */
@@ -499,8 +209,7 @@ declare class RetupleArray<T> extends Array<T> {
499
209
  /**
500
210
  * ## Method not available
501
211
  *
502
- * Built-in array methods not available on `Result` types, convert the result
503
- * to a tuple using `$tuple()` first.
212
+ * Built-in array methods not available on {@link Result} types.
504
213
  *
505
214
  * @deprecated
506
215
  */
@@ -508,8 +217,7 @@ declare class RetupleArray<T> extends Array<T> {
508
217
  /**
509
218
  * ## Method not available
510
219
  *
511
- * Built-in array methods not available on `Result` types, convert the result
512
- * to a tuple using `$tuple()` first.
220
+ * Built-in array methods not available on {@link Result} types.
513
221
  *
514
222
  * @deprecated
515
223
  */
@@ -517,8 +225,7 @@ declare class RetupleArray<T> extends Array<T> {
517
225
  /**
518
226
  * ## Method not available
519
227
  *
520
- * Built-in array methods not available on `Result` types, convert the result
521
- * to a tuple using `$tuple()` first.
228
+ * Built-in array methods not available on {@link Result} types.
522
229
  *
523
230
  * @deprecated
524
231
  */
@@ -526,8 +233,7 @@ declare class RetupleArray<T> extends Array<T> {
526
233
  /**
527
234
  * ## Method not available
528
235
  *
529
- * Built-in array methods not available on `Result` types, convert the result
530
- * to a tuple using `$tuple()` first.
236
+ * Built-in array methods not available on {@link Result} types.
531
237
  *
532
238
  * @deprecated
533
239
  */
@@ -535,8 +241,7 @@ declare class RetupleArray<T> extends Array<T> {
535
241
  /**
536
242
  * ## Method not available
537
243
  *
538
- * Built-in array methods not available on `Result` types, convert the result
539
- * to a tuple using `$tuple()` first.
244
+ * Built-in array methods not available on {@link Result} types.
540
245
  *
541
246
  * @deprecated
542
247
  */
@@ -544,8 +249,7 @@ declare class RetupleArray<T> extends Array<T> {
544
249
  /**
545
250
  * ## Method not available
546
251
  *
547
- * Built-in array methods not available on `Result` types, convert the result
548
- * to a tuple using `$tuple()` first.
252
+ * Built-in array methods not available on {@link Result} types.
549
253
  *
550
254
  * @deprecated
551
255
  */
@@ -553,8 +257,7 @@ declare class RetupleArray<T> extends Array<T> {
553
257
  /**
554
258
  * ## Method not available
555
259
  *
556
- * Built-in array methods not available on `Result` types, convert the result
557
- * to a tuple using `$tuple()` first.
260
+ * Built-in array methods not available on {@link Result} types.
558
261
  *
559
262
  * @deprecated
560
263
  */
@@ -562,8 +265,7 @@ declare class RetupleArray<T> extends Array<T> {
562
265
  /**
563
266
  * ## Method not available
564
267
  *
565
- * Built-in array methods not available on `Result` types, convert the result
566
- * to a tuple using `$tuple()` first.
268
+ * Built-in array methods not available on {@link Result} types.
567
269
  *
568
270
  * @deprecated
569
271
  */
@@ -571,8 +273,7 @@ declare class RetupleArray<T> extends Array<T> {
571
273
  /**
572
274
  * ## Method not available
573
275
  *
574
- * Built-in array methods not available on `Result` types, convert the result
575
- * to a tuple using `$tuple()` first.
276
+ * Built-in array methods not available on {@link Result} types.
576
277
  *
577
278
  * @deprecated
578
279
  */
@@ -580,8 +281,7 @@ declare class RetupleArray<T> extends Array<T> {
580
281
  /**
581
282
  * ## Method not available
582
283
  *
583
- * Built-in array methods not available on `Result` types, convert the result
584
- * to a tuple using `$tuple()` first.
284
+ * Built-in array methods not available on {@link Result} types.
585
285
  *
586
286
  * @deprecated
587
287
  */
@@ -589,8 +289,7 @@ declare class RetupleArray<T> extends Array<T> {
589
289
  /**
590
290
  * ## Method not available
591
291
  *
592
- * Built-in array methods not available on `Result` types, convert the result
593
- * to a tuple using `$tuple()` first.
292
+ * Built-in array methods not available on {@link Result} types.
594
293
  *
595
294
  * @deprecated
596
295
  */
@@ -598,8 +297,7 @@ declare class RetupleArray<T> extends Array<T> {
598
297
  /**
599
298
  * ## Method not available
600
299
  *
601
- * Built-in array methods not available on `Result` types, convert the result
602
- * to a tuple using `$tuple()` first.
300
+ * Built-in array methods not available on {@link Result} types.
603
301
  *
604
302
  * @deprecated
605
303
  */
@@ -607,8 +305,7 @@ declare class RetupleArray<T> extends Array<T> {
607
305
  /**
608
306
  * ## Method not available
609
307
  *
610
- * Built-in array methods not available on `Result` types, convert the result
611
- * to a tuple using `$tuple()` first.
308
+ * Built-in array methods not available on {@link Result} types.
612
309
  *
613
310
  * @deprecated
614
311
  */
@@ -616,8 +313,7 @@ declare class RetupleArray<T> extends Array<T> {
616
313
  /**
617
314
  * ## Method not available
618
315
  *
619
- * Built-in array methods not available on `Result` types, convert the result
620
- * to a tuple using `$tuple()` first.
316
+ * Built-in array methods not available on {@link Result} types.
621
317
  *
622
318
  * @deprecated
623
319
  */
@@ -625,8 +321,7 @@ declare class RetupleArray<T> extends Array<T> {
625
321
  /**
626
322
  * ## Method not available
627
323
  *
628
- * Built-in array methods not available on `Result` types, convert the result
629
- * to a tuple using `$tuple()` first.
324
+ * Built-in array methods not available on {@link Result} types.
630
325
  *
631
326
  * @deprecated
632
327
  */
@@ -634,8 +329,7 @@ declare class RetupleArray<T> extends Array<T> {
634
329
  /**
635
330
  * ## Method not available
636
331
  *
637
- * Built-in array methods not available on `Result` types, convert the result
638
- * to a tuple using `$tuple()` first.
332
+ * Built-in array methods not available on {@link Result} types.
639
333
  *
640
334
  * @deprecated
641
335
  */
@@ -643,8 +337,7 @@ declare class RetupleArray<T> extends Array<T> {
643
337
  /**
644
338
  * ## Method not available
645
339
  *
646
- * Built-in array methods not available on `Result` types, convert the result
647
- * to a tuple using `$tuple()` first.
340
+ * Built-in array methods not available on {@link Result} types.
648
341
  *
649
342
  * @deprecated
650
343
  */
@@ -652,8 +345,7 @@ declare class RetupleArray<T> extends Array<T> {
652
345
  /**
653
346
  * ## Method not available
654
347
  *
655
- * Built-in array methods not available on `Result` types, convert the result
656
- * to a tuple using `$tuple()` first.
348
+ * Built-in array methods not available on {@link Result} types.
657
349
  *
658
350
  * @deprecated
659
351
  */
@@ -661,8 +353,7 @@ declare class RetupleArray<T> extends Array<T> {
661
353
  /**
662
354
  * ## Method not available
663
355
  *
664
- * Built-in array methods not available on `Result` types, convert the result
665
- * to a tuple using `$tuple()` first.
356
+ * Built-in array methods not available on {@link Result} types.
666
357
  *
667
358
  * @deprecated
668
359
  */
@@ -670,8 +361,7 @@ declare class RetupleArray<T> extends Array<T> {
670
361
  /**
671
362
  * ## Method not available
672
363
  *
673
- * Built-in array methods not available on `Result` types, convert the result
674
- * to a tuple using `$tuple()` first.
364
+ * Built-in array methods not available on {@link Result} types.
675
365
  *
676
366
  * @deprecated
677
367
  */
@@ -679,8 +369,7 @@ declare class RetupleArray<T> extends Array<T> {
679
369
  /**
680
370
  * ## Method not available
681
371
  *
682
- * Built-in array methods not available on `Result` types, convert the result
683
- * to a tuple using `$tuple()` first.
372
+ * Built-in array methods not available on {@link Result} types.
684
373
  *
685
374
  * @deprecated
686
375
  */
@@ -688,8 +377,7 @@ declare class RetupleArray<T> extends Array<T> {
688
377
  /**
689
378
  * ## Method not available
690
379
  *
691
- * Built-in array methods not available on `Result` types, convert the result
692
- * to a tuple using `$tuple()` first.
380
+ * Built-in array methods not available on {@link Result} types.
693
381
  *
694
382
  * @deprecated
695
383
  */
@@ -697,8 +385,7 @@ declare class RetupleArray<T> extends Array<T> {
697
385
  /**
698
386
  * ## Method not available
699
387
  *
700
- * Built-in array methods not available on `Result` types, convert the result
701
- * to a tuple using `$tuple()` first.
388
+ * Built-in array methods not available on {@link Result} types.
702
389
  *
703
390
  * @deprecated
704
391
  */
@@ -706,8 +393,7 @@ declare class RetupleArray<T> extends Array<T> {
706
393
  /**
707
394
  * ## Method not available
708
395
  *
709
- * Built-in array methods not available on `Result` types, convert the result
710
- * to a tuple using `$tuple()` first.
396
+ * Built-in array methods not available on {@link Result} types.
711
397
  *
712
398
  * @deprecated
713
399
  */
@@ -715,8 +401,7 @@ declare class RetupleArray<T> extends Array<T> {
715
401
  /**
716
402
  * ## Method not available
717
403
  *
718
- * Built-in array methods not available on `Result` types, convert the result
719
- * to a tuple using `$tuple()` first.
404
+ * Built-in array methods not available on {@link Result} types.
720
405
  *
721
406
  * @deprecated
722
407
  */
@@ -724,8 +409,7 @@ declare class RetupleArray<T> extends Array<T> {
724
409
  /**
725
410
  * ## Method not available
726
411
  *
727
- * Built-in array methods not available on `Result` types, convert the result
728
- * to a tuple using `$tuple()` first.
412
+ * Built-in array methods not available on {@link Result} types.
729
413
  *
730
414
  * @deprecated
731
415
  */
@@ -733,8 +417,7 @@ declare class RetupleArray<T> extends Array<T> {
733
417
  /**
734
418
  * ## Method not available
735
419
  *
736
- * Built-in array methods not available on `Result` types, convert the result
737
- * to a tuple using `$tuple()` first.
420
+ * Built-in array methods not available on {@link Result} types.
738
421
  *
739
422
  * @deprecated
740
423
  */
@@ -742,8 +425,7 @@ declare class RetupleArray<T> extends Array<T> {
742
425
  /**
743
426
  * ## Method not available
744
427
  *
745
- * Built-in array methods not available on `Result` types, convert the result
746
- * to a tuple using `$tuple()` first.
428
+ * Built-in array methods not available on {@link Result} types.
747
429
  *
748
430
  * @deprecated
749
431
  */
@@ -751,8 +433,7 @@ declare class RetupleArray<T> extends Array<T> {
751
433
  /**
752
434
  * ## Method not available
753
435
  *
754
- * Built-in array methods not available on `Result` types, convert the result
755
- * to a tuple using `$tuple()` first.
436
+ * Built-in array methods not available on {@link Result} types.
756
437
  *
757
438
  * @deprecated
758
439
  */
@@ -760,8 +441,7 @@ declare class RetupleArray<T> extends Array<T> {
760
441
  /**
761
442
  * ## Method not available
762
443
  *
763
- * Built-in array methods not available on `Result` types, convert the result
764
- * to a tuple using `$tuple()` first.
444
+ * Built-in array methods not available on {@link Result} types.
765
445
  *
766
446
  * @deprecated
767
447
  */
@@ -769,8 +449,7 @@ declare class RetupleArray<T> extends Array<T> {
769
449
  /**
770
450
  * ## Method not available
771
451
  *
772
- * Built-in array methods not available on `Result` types, convert the result
773
- * to a tuple using `$tuple()` first.
452
+ * Built-in array methods not available on {@link Result} types.
774
453
  *
775
454
  * @deprecated
776
455
  */
@@ -778,8 +457,7 @@ declare class RetupleArray<T> extends Array<T> {
778
457
  /**
779
458
  * ## Method not available
780
459
  *
781
- * Built-in array methods not available on `Result` types, convert the result
782
- * to a tuple using `$tuple()` first.
460
+ * Built-in array methods not available on {@link Result} types.
783
461
  *
784
462
  * @deprecated
785
463
  */
@@ -818,65 +496,77 @@ declare class ResultAsync<T, E> {
818
496
  */
819
497
  $unwrapOrElse<U = T>(this: ResultAsync<T, E>, f: () => U): Promise<T | U>;
820
498
  /**
821
- * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
499
+ * The same as {@link Retuple.$map|$map}, except it returns
500
+ * {@link ResultAsync}.
822
501
  */
823
502
  $map<U>(this: ResultAsync<T, E>, f: (val: T) => U): ResultAsync<U, E>;
824
503
  /**
825
504
  * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
826
- * `ResultAsync`.
505
+ * {@link ResultAsync}.
827
506
  */
828
507
  $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
829
508
  /**
830
- * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
509
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns
510
+ * {@link ResultAsync}.
831
511
  */
832
512
  $mapOr<U, V = U>(this: ResultAsync<T, E>, def: U, f: (val: T) => V): ResultAsync<U | V, never>;
833
513
  /**
834
514
  * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
835
- * `ResultAsync`.
515
+ * {@link ResultAsync}.
836
516
  */
837
517
  $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
838
518
  /**
839
519
  * The same as {@link Retuple.$andAssertOr|$andAssertOr}, except it:
840
520
  *
841
521
  * - can also accept a `PromiseLike` default value;
842
- * - returns `ResultAsync`.
522
+ * - returns {@link ResultAsync}.
843
523
  */
844
- $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
845
- $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>;
846
- $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
524
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: ResultLikeAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
525
+ $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>;
526
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: ResultLikeAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
847
527
  /**
848
528
  * The same as {@link Retuple.$andAssertOrElse|$andAssertOrElse}, except it:
849
529
  *
850
530
  * - can also accept an `async` default function;
851
- * - returns `ResultAsync`.
531
+ * - returns {@link ResultAsync}.
852
532
  */
853
- $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
854
- $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>;
855
- $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
533
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
534
+ $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>;
535
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => ResultLikeAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
856
536
  /**
857
537
  * The same as {@link Retuple.$or|$or}, except it:
858
538
  *
859
539
  * - can also accept a `PromiseLike` or value;
860
- * - returns `ResultAsync`.
540
+ * - returns {@link ResultAsync}.
861
541
  */
862
- $or<U = T, F = E>(this: ResultAsync<T, E>, or: Retuple<U, F> | PromiseLike<Retuple<U, F>>): ResultAsync<T | U, F>;
542
+ $or<U = T, F = E>(this: ResultAsync<T, E>, or: ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
863
543
  /**
864
544
  * The same as {@link Retuple.$orElse|$orElse}, except it:
865
545
  *
866
546
  * - can also accept an `async` or function;
867
- * - returns `ResultAsync`.
547
+ * - returns {@link ResultAsync}.
868
548
  */
869
- $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => RetupleAwaitable<U, F>): ResultAsync<T | U, F>;
549
+ $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => ResultLikeAwaitable<U, F>): ResultAsync<T | U, F>;
870
550
  /**
871
551
  * The same as {@link Retuple.$orSafe|$orSafe}, except it:
872
552
  *
873
553
  * - can also accept an `async` safe function;
874
- * - returns `ResultAsync`.
554
+ * - returns {@link ResultAsync}.
875
555
  */
876
556
  $orSafe<U = T>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, Error>;
877
557
  $orSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
878
558
  /**
879
- * @TODO
559
+ * Returns {@link ResultAsync} based on the outcome of the promise when this
560
+ * result is `Err`.
561
+ *
562
+ * Otherwise, returns `Ok` containing the current contained value.
563
+ *
564
+ * Uses the same strategy as {@link Result.$safePromise}, equivalent to
565
+ * calling:
566
+ *
567
+ * ```ts
568
+ * resultAsync.$orElse(() => Result.$safePromise(...))
569
+ * ```
880
570
  */
881
571
  $orSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<T | U, Error>;
882
572
  $orSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
@@ -884,33 +574,43 @@ declare class ResultAsync<T, E> {
884
574
  * The same as {@link Retuple.$and|$and}, except it:
885
575
  *
886
576
  * - can also accept a `PromiseLike` and value;
887
- * - returns `ResultAsync`.
577
+ * - returns {@link ResultAsync}.
888
578
  */
889
- $and<U = T, F = E>(this: ResultAsync<T, E>, and: RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
579
+ $and<U = T, F = E>(this: ResultAsync<T, E>, and: ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
890
580
  /**
891
581
  * The same as {@link Retuple.$andThen|$andThen}, except it:
892
582
  *
893
583
  * - can also accept an `async` and function;
894
- * - returns `ResultAsync`.
584
+ * - returns {@link ResultAsync}.
895
585
  */
896
- $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
586
+ $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<U, F>): ResultAsync<U, E | F>;
897
587
  /**
898
588
  * The same as {@link Retuple.$andThrough|$andThrough}, except it:
899
589
  *
900
590
  * - can also accept an `async` through function;
901
- * - returns `ResultAsync`.
591
+ * - returns {@link ResultAsync}.
902
592
  */
903
- $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<any, F>): ResultAsync<T, E | F>;
593
+ $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => ResultLikeAwaitable<any, F>): ResultAsync<T, E | F>;
904
594
  /**
905
595
  * The same as {@link Retuple.$andSafe|$andSafe}, except it:
906
596
  *
907
597
  * - can also accept an `async` safe function;
908
- * - returns `ResultAsync`.
598
+ * - returns {@link ResultAsync}.
909
599
  */
910
600
  $andSafe<U = T>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
911
601
  $andSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
912
602
  /**
913
- * @TODO
603
+ * Returns {@link ResultAsync} based on the outcome of the promise when this
604
+ * result is `Ok`.
605
+ *
606
+ * Otherwise, returns `Err` containing the current error value.
607
+ *
608
+ * Uses the same strategy as {@link Result.$safePromise}, equivalent to
609
+ * calling:
610
+ *
611
+ * ```ts
612
+ * resultAsync.$andThen(() => Result.$safePromise(...))
613
+ * ```
914
614
  */
915
615
  $andSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<U, E | Error>;
916
616
  $andSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
@@ -918,21 +618,21 @@ declare class ResultAsync<T, E> {
918
618
  * The same as {@link Retuple.$peek|$peek}, except it:
919
619
  *
920
620
  * - awaits the peek function;
921
- * - returns `ResultAsync`.
621
+ * - returns {@link ResultAsync}.
922
622
  */
923
623
  $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
924
624
  /**
925
625
  * The same as {@link Retuple.$tap|$tap}, except it:
926
626
  *
927
627
  * - awaits the tap function;
928
- * - returns `ResultAsync`.
628
+ * - returns {@link ResultAsync}.
929
629
  */
930
630
  $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
931
631
  /**
932
632
  * The same as {@link Retuple.$tapErr|$tapErr}, except it:
933
633
  *
934
634
  * - awaits the tap error function;
935
- * - returns `ResultAsync`.
635
+ * - returns {@link ResultAsync}.
936
636
  */
937
637
  $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
938
638
  /**
@@ -940,31 +640,132 @@ declare class ResultAsync<T, E> {
940
640
  */
941
641
  $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
942
642
  /**
943
- * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
643
+ * The same as {@link Retuple.$iter|$iter}, except it returns a `Promise`.
944
644
  */
945
- $tuple(this: ResultAsync<T, E>): Promise<[err: E | undefined, value: T | undefined]>;
645
+ $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
646
+ }
647
+ /**
648
+ * ## ResultRetry
649
+ */
650
+ declare class ResultRetry<T, E> implements PromiseLike<Result<T, E>> {
651
+ #private;
652
+ private static MAX_TIMEOUT;
653
+ private static MAX_RETRY;
654
+ private static zero;
655
+ private static delay;
656
+ private static integer;
657
+ constructor(f: () => ResultLikeAwaitable<T, E>);
658
+ 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>;
946
659
  /**
947
- * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
660
+ * Sets the maximum number of times the retry function can be executed,
661
+ * mutating this `ResultRetry` instance.
662
+ *
663
+ * **The default value is 1 - meaning that unless set, no retries will be
664
+ * attempted.**
665
+ *
666
+ * The retry function can be called up to the maximum number of times until
667
+ * it returns `Ok`. If it never returns `Ok`, the most recent `Err` is
668
+ * returned.
669
+ *
670
+ * This function accepts a positive integer between 1 and 100:
671
+ *
672
+ * - Integers outside of this range are clamped to the nearest valid value;
673
+ * - Any other value (NaN, Infinity, fractions, strings) are treated as 1.
674
+ *
675
+ * @example
676
+ *
677
+ * ```ts
678
+ * // Retry someResultFn up to 3 times until Ok is returned:
679
+ * const result = await Result.$retry(someResultFn).$times(3);
680
+ * ```
948
681
  */
949
- $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
682
+ $times<N extends number>(this: ResultRetry<T, E>, times: NonZero<N> & NonNegativeOrDecimal<N>): ResultRetry<T, E>;
683
+ /**
684
+ * Sets the delay between each retry attempt, mutating this `ResultRetry`
685
+ * instance.
686
+ *
687
+ * - Provide a number of milliseconds to introduce a static delay between
688
+ * attempts;
689
+ * - Provide a function to compute the delay dynamically for each attempt;
690
+ * - If the maximum number of retries is 1, this setting as no effect.
691
+ *
692
+ * **The default value is 0 - meaning that unless set, there will be no delay
693
+ * between attempts.**
694
+ *
695
+ * This function accepts an integer between 0 and 3600000:
696
+ *
697
+ * - Integers outside of this range are clamped to the nearest valid value;
698
+ * - Any other value (NaN, Infinity, fractions, strings) are treated as 0.
699
+ *
700
+ * @example
701
+ *
702
+ * ```ts
703
+ * // Retry someResultFn up to 3 times until Ok is returned,
704
+ * // with a 1 second delay between each invocation:
705
+ * const result = await Result.$retry(someResultFn).$times(3).$delay(1000);
706
+ * ```
707
+ */
708
+ $delay<N extends number>(this: ResultRetry<T, E>, f: (attempt: number) => NonNegativeOrDecimal<N>): ResultRetry<T, E>;
709
+ $delay<N extends number>(this: ResultRetry<T, E>, ms: NonNegativeOrDecimal<N>): ResultRetry<T, E>;
710
+ /**
711
+ * Sets a handler to be called when an attempt returns `Err`, mutating this
712
+ * `ResultRetry` instance. The handler can be used to capture information
713
+ * about each failure, and to abort early and prevent further retries.
714
+ *
715
+ * The handler function is called with `ResultRetryHandleState`, containing:
716
+ *
717
+ * - **error** - The error value from the last failed attempt;
718
+ * - **attempt** - The attempt number;
719
+ * - **abort** - A function which when called, prevents further retries.
720
+ *
721
+ * @example
722
+ *
723
+ * ```ts
724
+ * // Retry someResultFn up to 3 times until Ok is returned, logging each
725
+ * // attempt and aborting early if the error code is "UNAUTHORIZED".
726
+ * const result = await Result.$retry(someResultFn)
727
+ * .$times(3)
728
+ * .$handle(({ error, attempt, abort }) => {
729
+ * console.info(`Attempt ${attempt} failed: ${error}`);
730
+ * if (error === "UNAUTHORIZED") {
731
+ * abort();
732
+ * }
733
+ * });
734
+ * ```
735
+ */
736
+ $handle(f: (controller: ResultRetryController<E>) => void): ResultRetry<T, E>;
737
+ /**
738
+ * Returns {@link ResultAsync} which resolves to this retried {@link Result}.
739
+ *
740
+ * @example
741
+ *
742
+ * ```ts
743
+ * const result: Result<string, SomeError> = await Result
744
+ * .$retry(someResultFn)
745
+ * .$times(3)
746
+ * .$delay(100)
747
+ * .$async()
748
+ * .$andThen((message) => `Success: ${message}`)
749
+ * .$mapErr((code) => new SomeError({ code }));
750
+ * ```
751
+ */
752
+ $async(this: ResultRetry<T, E>): ResultAsync<T, E>;
753
+ /**
754
+ * Returns a `Promise` which resolves to this retried {@link Result}.
755
+ *
756
+ * @example
757
+ *
758
+ * ```ts
759
+ * const promise: Promise<Result<string, Error>> = Result
760
+ * .$retry(someResultFn)
761
+ * .$times(3)
762
+ * .$promise();
763
+ * ```
764
+ */
765
+ $promise(this: ResultRetry<T, E>): Promise<Result<T, E>>;
766
+ private drain;
950
767
  }
951
- type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
952
- type OkTuple<T> = [err: undefined, value: T];
953
- type ErrTuple<E> = [err: E, value: undefined];
954
- type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
955
- type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
956
- type ObjectUnionOk<T> = {
957
- success: true;
958
- data: T;
959
- error?: never | undefined;
960
- };
961
- type ObjectUnionErr<E> = {
962
- success: false;
963
- data?: never | undefined;
964
- error: E;
965
- };
966
- type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
967
- interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
768
+ interface Retuple<T, E> extends RetupleArray<T | E | undefined>, ResultLike<T, E> {
968
769
  /**
969
770
  * Returns true when this result is `Ok`. Acts as a type guard.
970
771
  *
@@ -1340,9 +1141,9 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1340
1141
  * assert.equal(asserted.$unwrapErr(), "test");
1341
1142
  * ```
1342
1143
  */
1343
- $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T> | U, E | F>;
1344
- $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>;
1345
- $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1144
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: ResultLike<U, F>): Result<Truthy<T> | U, E | F>;
1145
+ $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>;
1146
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: ResultLike<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1346
1147
  /**
1347
1148
  * Performs an assertion when this result is `Ok`:
1348
1149
  *
@@ -1423,9 +1224,9 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1423
1224
  * assert.equal(asserted.$unwrapErr(), "test");
1424
1225
  * ```
1425
1226
  */
1426
- $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>): Result<Truthy<T> | U, E | F>;
1427
- $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>;
1428
- $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1227
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => ResultLike<U, F>): Result<Truthy<T> | U, E | F>;
1228
+ $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>;
1229
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => ResultLike<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1429
1230
  /**
1430
1231
  * Returns `Ok` containing the return value of the map function when this
1431
1232
  * result is `Ok`.
@@ -1576,7 +1377,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1576
1377
  * );
1577
1378
  * ```
1578
1379
  */
1579
- $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, F>;
1380
+ $or<U = T, F = E>(this: Result<T, E>, or: ResultLike<U, F>): Result<T | U, F>;
1580
1381
  /**
1581
1382
  * Returns the result returned by the or function, when this result is `Err`.
1582
1383
  *
@@ -1612,10 +1413,10 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1612
1413
  * );
1613
1414
  * ```
1614
1415
  */
1615
- $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, F>;
1416
+ $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => ResultLike<U, F>): Result<T | U, F>;
1616
1417
  /**
1617
- * Returns a `Result` based on the outcome of the safe function when this
1618
- * result is `Err`.
1418
+ * Returns a {@link Result} based on the outcome of the safe function when
1419
+ * this result is `Err`.
1619
1420
  *
1620
1421
  * Otherwise, returns `Ok` containing the current ok value.
1621
1422
  *
@@ -1659,7 +1460,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1659
1460
  * );
1660
1461
  * ```
1661
1462
  */
1662
- $and<U = T, F = E>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
1463
+ $and<U = T, F = E>(this: Result<T, E>, and: ResultLike<U, F>): Result<U, E | F>;
1663
1464
  /**
1664
1465
  * Returns the and result, when this result is `Ok`.
1665
1466
  *
@@ -1695,7 +1496,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1695
1496
  * );
1696
1497
  * ```
1697
1498
  */
1698
- $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
1499
+ $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => ResultLike<U, F>): Result<U, E | F>;
1699
1500
  /**
1700
1501
  * Calls the through function when this result is `Ok` and returns:
1701
1502
  *
@@ -1735,23 +1536,24 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1735
1536
  * );
1736
1537
  * ```
1737
1538
  */
1738
- $andThrough<F = E>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
1539
+ $andThrough<F = E>(this: Result<T, E>, f: (val: T) => ResultLike<any, F>): Result<T, E | F>;
1739
1540
  /**
1740
1541
  * Returns a result based on the outcome of the safe function when this
1741
1542
  * result is `Ok`.
1742
1543
  *
1743
1544
  * Otherwise, returns `Err` containing the current error value.
1744
1545
  *
1745
- * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1746
- * `result.$and(Result.$safe(...))`.
1546
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling:
1547
+ *
1548
+ * ```ts
1549
+ * result.$andThen(() => Result.$safe(...))
1550
+ * ```
1747
1551
  */
1748
1552
  $andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
1749
1553
  $andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
1750
1554
  /**
1751
- * @TODO
1752
- */
1753
- /**
1754
- * Calls the peek function and returns `Result` equivalent to this result.
1555
+ * Calls the peek function and returns {@link Result} equivalent to this
1556
+ * result.
1755
1557
  *
1756
1558
  * @example
1757
1559
  *
@@ -1847,7 +1649,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1847
1649
  */
1848
1650
  $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
1849
1651
  /**
1850
- * Returns the contained `Result` when this result is `Ok`.
1652
+ * Returns the contained {@link Result} when this result is `Ok`.
1851
1653
  *
1852
1654
  * Otherwise returns `Err` containing the current error value.
1853
1655
  *
@@ -1878,7 +1680,7 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1878
1680
  */
1879
1681
  $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
1880
1682
  /**
1881
- * Returns an equivalent `ResultAsync`.
1683
+ * Returns an equivalent {@link ResultAsync}.
1882
1684
  *
1883
1685
  * @example
1884
1686
  *
@@ -1912,24 +1714,6 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1912
1714
  * ```
1913
1715
  */
1914
1716
  $promise(this: Result<T, E>): Promise<Result<T, E>>;
1915
- /**
1916
- * Returns a two-element, standard array tuple equivalent to this result.
1917
- *
1918
- * @example
1919
- *
1920
- * ```ts
1921
- * const result = Ok("test");
1922
- * assert.deepEqual(result.$tuple(), [undefined, "test"]);
1923
- * ```
1924
- *
1925
- * @example
1926
- *
1927
- * ```ts
1928
- * const result = Err("test");
1929
- * assert.deepEqual(result.$tuple(), ["test", undefined]);
1930
- * ```
1931
- */
1932
- $tuple(this: Result<T, E>): [err: E | undefined, value: T | undefined];
1933
1717
  /**
1934
1718
  * Returns an `IterableIterator` over the contained ok value, when this
1935
1719
  * result is `Ok`.
@@ -1974,3 +1758,19 @@ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
1974
1758
  */
1975
1759
  $iter<U>(this: Result<Iterable<U>, E>): IterableIterator<U, undefined, unknown>;
1976
1760
  }
1761
+ type OkTuple<T> = [err: undefined, value: T];
1762
+ type ErrTuple<E> = [err: E, value: undefined];
1763
+ type ResultLikeAwaitable<T, E> = ResultLike<T, E> | PromiseLike<ResultLike<T, E>>;
1764
+ type ObjectUnionOk<T> = {
1765
+ success: true;
1766
+ data: T;
1767
+ error?: never | undefined;
1768
+ };
1769
+ type ObjectUnionErr<E> = {
1770
+ success: false;
1771
+ data?: never | undefined;
1772
+ error: E;
1773
+ };
1774
+ type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
1775
+ type NonZero<N extends number> = N & (`${N}` extends "0" ? never : N);
1776
+ type NonNegativeOrDecimal<N extends number> = N & (`${N}` extends `-${string}` | `${string}.${string}` ? never : N);