retuple 1.0.0-next.1 → 1.0.0-next.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,75 +1,779 @@
1
- export type Ok<T> = OkTuple<T> & Retuple<T, never>;
2
- export type Err<E> = ErrTuple<E> & Retuple<never, E>;
1
+ export type Ok = typeof Ok;
2
+ export type Err = typeof Err;
3
+ export type nonNullable = typeof nonNullable;
4
+ export type truthy = typeof truthy;
5
+ export type safe = typeof safe;
6
+ export type safeAsync = typeof safeAsync;
7
+ export type safePromise = typeof safePromise;
3
8
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
4
9
  export { type ResultAsync };
5
- export declare class RetupleUnwrapFailed<E = unknown> extends Error {
10
+ /**
11
+ * ## Retuple Unwrap Failed
12
+ *
13
+ * An error which occurs when calling `$unwrap` on `Err`.
14
+ */
15
+ export declare class RetupleUnwrapFailed<const E = unknown> extends Error {
6
16
  value: E;
7
17
  constructor(value: E, msg?: string);
8
18
  }
9
- export declare class RetupleUnwrapErrFailed<T = unknown> extends Error {
19
+ /**
20
+ * ## Retuple Unwrap Err Failed
21
+ *
22
+ * An error which occurs when calling `$unwrapErr` on `Ok`.
23
+ */
24
+ export declare class RetupleUnwrapErrFailed<const T = unknown> extends Error {
10
25
  value: T;
11
26
  constructor(value: T, msg?: string);
12
27
  }
13
- export declare class RetupleExpectFailed<E = unknown> extends Error {
28
+ /**
29
+ * ## Retuple Expect Failed
30
+ *
31
+ * An error which occurs when calling `$expect` on `Err`, when the value
32
+ * contained in the `Err` is not an instance of `Error`.
33
+ */
34
+ export declare class RetupleExpectFailed<const E = unknown> extends Error {
14
35
  value: E;
15
36
  constructor(value: E);
16
37
  }
38
+ /**
39
+ * ## Retuple Expect Failed
40
+ *
41
+ * An error which occurs when calling `$flatten` on `Ok`, when the value
42
+ * contained in the `Ok` is not an `Ok` or `Err`.
43
+ */
44
+ export declare class RetupleFlattenFailed<const T = unknown> extends Error {
45
+ value: T;
46
+ constructor(value: T);
47
+ }
48
+ /**
49
+ * ## Retuple Thrown Value Error
50
+ *
51
+ * An error constructed when a safe function call throws or rejects, when the
52
+ * thrown error or rejected value is not an instance of `Error`, and when no
53
+ * map error function is provided.
54
+ */
17
55
  export declare class RetupleThrownValueError extends Error {
18
56
  value: unknown;
19
57
  constructor(value: unknown);
20
58
  }
59
+ /**
60
+ * ## Retuple Invalid Result Error
61
+ *
62
+ * This error is thrown when attempting to construct a `Result` from a tuple,
63
+ * when neither index 0 or 1 are null or undefined. In this case, it is
64
+ * impossible to determine whether the result should be `Ok` or `Err`.
65
+ */
66
+ export declare class RetupleInvalidResultError extends Error {
67
+ value: unknown[];
68
+ constructor(value: unknown[]);
69
+ }
70
+ /**
71
+ * ## Retuple Array Method Unavailable Error
72
+ *
73
+ * This error is thrown when calling a built-in array method from a `Result`.
74
+ */
75
+ export declare class RetupleArrayMethodUnavailableError extends Error {
76
+ value: unknown[];
77
+ constructor(value: unknown[], method: Exclude<keyof any[] & string, "length">);
78
+ }
21
79
  /**
22
80
  * ## Result
23
81
  *
24
82
  * @TODO
25
83
  */
26
- export declare const Result: {
27
- Ok: typeof Ok;
28
- Err: typeof Err;
29
- from: typeof from;
30
- safe: typeof safe;
31
- safeAsync: typeof safeAsync;
32
- safePromise: typeof safePromise;
33
- };
34
- export default Result;
84
+ 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;
85
+ export declare namespace Result {
86
+ var Ok: typeof import(".").Ok;
87
+ var Err: typeof import(".").Err;
88
+ var $nonNullable: typeof nonNullable;
89
+ var $truthy: typeof truthy;
90
+ var $safe: typeof safe;
91
+ var $safeAsync: typeof safeAsync;
92
+ var $safePromise: typeof safePromise;
93
+ }
35
94
  /**
36
- * ## Ok
95
+ * Create a new {@link Result} with the `Ok` variant. When called without
96
+ * arguments the `T` type is `void`.
37
97
  *
38
- * @TODO
98
+ * @example
99
+ *
100
+ * ```ts
101
+ * const [err, value] = Ok("test");
102
+ *
103
+ * assert.equal(err, undefined);
104
+ * assert.equal(value, "test");
105
+ * ```
106
+ *
107
+ * @example
108
+ *
109
+ * ```ts
110
+ * const result: Result<void, never> = Ok();
111
+ * ```
39
112
  */
40
- export declare function Ok(): Ok<void>;
41
- export declare function Ok<T>(val: T): Ok<T>;
113
+ export declare function Ok(): Result<void, never>;
114
+ export declare function Ok<const T>(val: T): Result<T, never>;
42
115
  /**
43
- * ## Err
116
+ * Create a new {@link Result} with the `Err` variant. When called without
117
+ * arguments the `E` type is `void`.
44
118
  *
45
- * @TODO
119
+ * @example
120
+ *
121
+ * ```ts
122
+ * const [err, value] = Err("test");
123
+ *
124
+ * assert.equal(err, "test");
125
+ * assert.equal(value, undefined);
126
+ * ```
127
+ *
128
+ * @example
129
+ *
130
+ * ```ts
131
+ * const result: Result<never, void> = Err();
132
+ * ```
46
133
  */
47
- export declare function Err(): Err<void>;
48
- export declare function Err<E>(err: E): Err<E>;
134
+ export declare function Err(): Result<never, void>;
135
+ export declare function Err<const E>(err: E): Result<never, E>;
136
+ /**
137
+ * Construct a {@link Result} from a value. If the value is neither null or
138
+ * undefined, the result is `Ok`.
139
+ *
140
+ * Otherwise, the result is `Err` containing:
141
+
142
+ * - the returned value from the error function when provided;
143
+ * - or `true` otherwise.
144
+ *
145
+ * @example
146
+ *
147
+ * ```ts
148
+ * const result: Result<User, Error> = Result.$nonNullable(
149
+ * users.find((user) => user.id === currentUserId),
150
+ * () => new Error("User not found"),
151
+ * );
152
+ * ```
153
+ *
154
+ * @example
155
+ *
156
+ * ```ts
157
+ * const [err, value] = Result.$nonNullable("test");
158
+ *
159
+ * assert.equal(err, undefined);
160
+ * assert.equal(value, "test");
161
+ * ```
162
+ *
163
+ * @example
164
+ *
165
+ * ```ts
166
+ * const [err, value] = Result.$nonNullable(null);
167
+ *
168
+ * assert.equal(err, true);
169
+ * assert.equal(value, undefined);
170
+ * ```
171
+ *
172
+ * @example
173
+ *
174
+ * ```ts
175
+ * const [err, value] = Result.$nonNullable(null, () => "error");
176
+ *
177
+ * assert.equal(err, "error");
178
+ * assert.equal(value, undefined);
179
+ * ```
180
+ */
181
+ export declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
182
+ export declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
49
183
  /**
50
184
  * Construct a {@link Result} from a value. If the value is truthy, the result
51
185
  * is `Ok`.
186
+ *
187
+ * Otherwise, the result is `Err` containing:
188
+ *
189
+ * - the returned value from the error function when provided;
190
+ * - or `true` otherwise.
191
+ *
192
+ * @example
193
+ *
194
+ * ```ts
195
+ * const result: Result<string, Error> = Result.$truthy(
196
+ * username.trim(),
197
+ * () => new Error("Username is empty"),
198
+ * );
199
+ * ```
200
+ *
201
+ * @example
202
+ *
203
+ * ```ts
204
+ * const [err, value] = Result.$truthy("test");
205
+ *
206
+ * assert.equal(err, undefined);
207
+ * assert.equal(value, "test");
208
+ * ```
209
+ *
210
+ * @example
211
+ *
212
+ * ```ts
213
+ * const [err, value] = Result.$truthy("");
214
+ *
215
+ * assert.equal(err, true);
216
+ * assert.equal(value, undefined);
217
+ * ```
218
+ *
219
+ * @example
220
+ *
221
+ * ```ts
222
+ * const [err, value] = Result.$truthy(0, () => "error");
223
+ *
224
+ * assert.equal(err, "error");
225
+ * assert.equal(value, undefined);
226
+ * ```
52
227
  */
53
- export declare function from<T>(value: T): Result<Truthy<T>, true>;
54
- export declare function from<T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
228
+ export declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
229
+ export declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
55
230
  /**
56
231
  * Construct a {@link Result} from a synchronous function call. If the function
57
232
  * returns without throwing, the result is `Ok`.
233
+ *
234
+ * Otherwise, the result is `Err` containing (in priority order):
235
+ *
236
+ * - the returned value from the map error function when provided;
237
+ * - the thrown error when it is an instance of `Error`;
238
+ * - `RetupleThrownValueError` when a non `Error` instance is thrown.
239
+ *
240
+ * @example
241
+ *
242
+ * ```ts
243
+ * const result: Result<URL, Error> = Result.$safe(
244
+ * () => new URL(user.url),
245
+ * () => new Error("Invalid URL"),
246
+ * );
247
+ * ```
248
+ *
249
+ * @example
250
+ *
251
+ * ```ts
252
+ * const [err, value] = Result.$safe(() => "test");
253
+ *
254
+ * assert.equal(err, undefined);
255
+ * assert.equal(value, "test");
256
+ * ```
257
+ *
258
+ * @example
259
+ *
260
+ * ```ts
261
+ * const [err, value] = Result.$safe(
262
+ * () => {
263
+ * throw new Error("throws");
264
+ * },
265
+ * () => "error",
266
+ * );
267
+ *
268
+ * assert.equal(err, "error");
269
+ * assert.equal(value, undefined);
270
+ * ```
271
+ *
272
+ * @example
273
+ *
274
+ * ```ts
275
+ * const [err, value] = Result.$safe(
276
+ * () => {
277
+ * throw new Error("throws")
278
+ * },
279
+ * );
280
+ *
281
+ * assert(err instanceof Error && err.message === "throws");
282
+ * assert.equal(value, undefined);
283
+ * ```
284
+ *
285
+ * @example
286
+ *
287
+ * ```ts
288
+ * const [err, value] = Result.$safe(() => {
289
+ * throw "non error";
290
+ * });
291
+ *
292
+ * assert(err instanceof RetupleThrownValueError && err.value === "non error");
293
+ * assert.equal(value, undefined);
58
294
  */
59
295
  export declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
60
296
  export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
61
297
  /**
62
298
  * Construct a {@link ResultAsync} from a function call. If the function returns
63
299
  * without throwing, and any promise returned resolves, the result is `Ok`.
300
+ *
301
+ * Otherwise, the result is `Err` containing (in priority order):
302
+ *
303
+ * - the returned value from the map error function when provided;
304
+ * - the thrown/rejected error when it is an instance of `Error`;
305
+ * - `RetupleThrownValueError` when throwing/rejecting with a non `Error`.
306
+ *
307
+ * @example
308
+ *
309
+ * ```ts
310
+ * const result: Result<Response, Error> = await Result.$safeAsync(
311
+ * () => fetch("http://example.com/api"),
312
+ * () => new Error("Fetch failed"),
313
+ * );
314
+ * ```
315
+ *
316
+ * @example
317
+ *
318
+ * ```ts
319
+ * const [err, value] = await Result.$safeAsync(async () => "test");
320
+ *
321
+ * assert.equal(err, undefined);
322
+ * assert.equal(value, "test");
323
+ * ```
324
+ *
325
+ * @example
326
+ *
327
+ * ```ts
328
+ * const [err, value] = await Result.$safeAsync(
329
+ * async () => {
330
+ * throw new Error("throws");
331
+ * },
332
+ * () => "error",
333
+ * );
334
+ *
335
+ * assert.equal(err, "error");
336
+ * assert.equal(value, undefined);
337
+ * ```
338
+ *
339
+ * @example
340
+ *
341
+ * ```ts
342
+ * const [err, value] = await Result.$safeAsync(
343
+ * async () => {
344
+ * throw new Error("throws")
345
+ * },
346
+ * );
347
+ *
348
+ * assert(err instanceof Error && err.message === "throws");
349
+ * assert.equal(value, undefined);
350
+ * ```
351
+ *
352
+ * @example
353
+ *
354
+ * ```ts
355
+ * const [err, value] = await Result.$safeAsync(async () => {
356
+ * throw "non error";
357
+ * });
358
+ *
359
+ * assert(err instanceof RetupleThrownValueError && err.value === "non error");
360
+ * assert.equal(value, undefined);
64
361
  */
65
362
  export declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
66
363
  export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
67
364
  /**
68
365
  * Construct a {@link Result} from a promise. If the promise resolves, the
69
366
  * result is `Ok`.
367
+ *
368
+ * Otherwise, the result is `Err` containing (in priority order):
369
+ *
370
+ * - the returned value from the map error function when provided;
371
+ * - the rejected error when it is an instance of `Error`;
372
+ * - `RetupleThrownValueError` when rejecting with a non `Error`.
373
+ *
374
+ * @example
375
+ *
376
+ * ```ts
377
+ * const result: Result<Response, Error> = await Result.$safePromise(
378
+ * fetch("http://example.com/api"),
379
+ * () => new Error("Fetch failed"),
380
+ * );
381
+ * ```
382
+ *
383
+ * @example
384
+ *
385
+ * ```ts
386
+ * const [err, value] = await Result.$safePromise(Promise.resolve("test"));
387
+ *
388
+ * assert.equal(err, undefined);
389
+ * assert.equal(value, "test");
390
+ * ```
391
+ *
392
+ * @example
393
+ *
394
+ * ```ts
395
+ * const [err, value] = await Result.$safePromise(
396
+ * Promise.reject(new Error("rejects")),
397
+ * () => "error",
398
+ * );
399
+ *
400
+ * assert.equal(err, "error");
401
+ * assert.equal(value, undefined);
402
+ * ```
403
+ *
404
+ * @example
405
+ *
406
+ * ```ts
407
+ * const [err, value] = await Result.$safePromise(
408
+ * Promise.reject(new Error("rejects")),
409
+ * );
410
+ *
411
+ * assert(err instanceof Error && err.message === "rejects");
412
+ * assert.equal(value, undefined);
413
+ * ```
414
+ *
415
+ * @example
416
+ *
417
+ * ```ts
418
+ * const [err, value] = await Result.$safeAsync(
419
+ * Promise.reject("non error"),
420
+ * );
421
+ *
422
+ * assert(err instanceof RetupleThrownValueError && err.value === "non error");
423
+ * assert.equal(value, undefined);
70
424
  */
71
425
  export declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
72
426
  export declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
427
+ /**
428
+ * ## RetupleArray
429
+ *
430
+ * Using built-in array methods on a `Result` is probably a mistake. This class
431
+ * makes the built-in methods throw `RetupleArrayMethodUnavailableError`.
432
+ */
433
+ declare class RetupleArray<T> extends Array<T> {
434
+ /**
435
+ * ## Method not available
436
+ *
437
+ * Built-in array methods not available on `Result` types, convert the result
438
+ * to a tuple using `$tuple()` first.
439
+ *
440
+ * @deprecated
441
+ */
442
+ at(): never;
443
+ /**
444
+ * ## Method not available
445
+ *
446
+ * Built-in array methods not available on `Result` types, convert the result
447
+ * to a tuple using `$tuple()` first.
448
+ *
449
+ * @deprecated
450
+ */
451
+ concat(): never;
452
+ /**
453
+ * ## Method not available
454
+ *
455
+ * Built-in array methods not available on `Result` types, convert the result
456
+ * to a tuple using `$tuple()` first.
457
+ *
458
+ * @deprecated
459
+ */
460
+ copyWithin(): never;
461
+ /**
462
+ * ## Method not available
463
+ *
464
+ * Built-in array methods not available on `Result` types, convert the result
465
+ * to a tuple using `$tuple()` first.
466
+ *
467
+ * @deprecated
468
+ */
469
+ entries(): never;
470
+ /**
471
+ * ## Method not available
472
+ *
473
+ * Built-in array methods not available on `Result` types, convert the result
474
+ * to a tuple using `$tuple()` first.
475
+ *
476
+ * @deprecated
477
+ */
478
+ every(): this is never[];
479
+ /**
480
+ * ## Method not available
481
+ *
482
+ * Built-in array methods not available on `Result` types, convert the result
483
+ * to a tuple using `$tuple()` first.
484
+ *
485
+ * @deprecated
486
+ */
487
+ fill(): never;
488
+ /**
489
+ * ## Method not available
490
+ *
491
+ * Built-in array methods not available on `Result` types, convert the result
492
+ * to a tuple using `$tuple()` first.
493
+ *
494
+ * @deprecated
495
+ */
496
+ filter(): never;
497
+ /**
498
+ * ## Method not available
499
+ *
500
+ * Built-in array methods not available on `Result` types, convert the result
501
+ * to a tuple using `$tuple()` first.
502
+ *
503
+ * @deprecated
504
+ */
505
+ find(): never;
506
+ /**
507
+ * ## Method not available
508
+ *
509
+ * Built-in array methods not available on `Result` types, convert the result
510
+ * to a tuple using `$tuple()` first.
511
+ *
512
+ * @deprecated
513
+ */
514
+ findIndex(): never;
515
+ /**
516
+ * ## Method not available
517
+ *
518
+ * Built-in array methods not available on `Result` types, convert the result
519
+ * to a tuple using `$tuple()` first.
520
+ *
521
+ * @deprecated
522
+ */
523
+ findLast(): never;
524
+ /**
525
+ * ## Method not available
526
+ *
527
+ * Built-in array methods not available on `Result` types, convert the result
528
+ * to a tuple using `$tuple()` first.
529
+ *
530
+ * @deprecated
531
+ */
532
+ findLastIndex(): never;
533
+ /**
534
+ * ## Method not available
535
+ *
536
+ * Built-in array methods not available on `Result` types, convert the result
537
+ * to a tuple using `$tuple()` first.
538
+ *
539
+ * @deprecated
540
+ */
541
+ flat(): never;
542
+ /**
543
+ * ## Method not available
544
+ *
545
+ * Built-in array methods not available on `Result` types, convert the result
546
+ * to a tuple using `$tuple()` first.
547
+ *
548
+ * @deprecated
549
+ */
550
+ flatMap(): never;
551
+ /**
552
+ * ## Method not available
553
+ *
554
+ * Built-in array methods not available on `Result` types, convert the result
555
+ * to a tuple using `$tuple()` first.
556
+ *
557
+ * @deprecated
558
+ */
559
+ forEach(): never;
560
+ /**
561
+ * ## Method not available
562
+ *
563
+ * Built-in array methods not available on `Result` types, convert the result
564
+ * to a tuple using `$tuple()` first.
565
+ *
566
+ * @deprecated
567
+ */
568
+ includes(): never;
569
+ /**
570
+ * ## Method not available
571
+ *
572
+ * Built-in array methods not available on `Result` types, convert the result
573
+ * to a tuple using `$tuple()` first.
574
+ *
575
+ * @deprecated
576
+ */
577
+ indexOf(): never;
578
+ /**
579
+ * ## Method not available
580
+ *
581
+ * Built-in array methods not available on `Result` types, convert the result
582
+ * to a tuple using `$tuple()` first.
583
+ *
584
+ * @deprecated
585
+ */
586
+ join(): never;
587
+ /**
588
+ * ## Method not available
589
+ *
590
+ * Built-in array methods not available on `Result` types, convert the result
591
+ * to a tuple using `$tuple()` first.
592
+ *
593
+ * @deprecated
594
+ */
595
+ keys(): never;
596
+ /**
597
+ * ## Method not available
598
+ *
599
+ * Built-in array methods not available on `Result` types, convert the result
600
+ * to a tuple using `$tuple()` first.
601
+ *
602
+ * @deprecated
603
+ */
604
+ lastIndexOf(): never;
605
+ /**
606
+ * ## Method not available
607
+ *
608
+ * Built-in array methods not available on `Result` types, convert the result
609
+ * to a tuple using `$tuple()` first.
610
+ *
611
+ * @deprecated
612
+ */
613
+ map(): never;
614
+ /**
615
+ * ## Method not available
616
+ *
617
+ * Built-in array methods not available on `Result` types, convert the result
618
+ * to a tuple using `$tuple()` first.
619
+ *
620
+ * @deprecated
621
+ */
622
+ pop(): never;
623
+ /**
624
+ * ## Method not available
625
+ *
626
+ * Built-in array methods not available on `Result` types, convert the result
627
+ * to a tuple using `$tuple()` first.
628
+ *
629
+ * @deprecated
630
+ */
631
+ push(): never;
632
+ /**
633
+ * ## Method not available
634
+ *
635
+ * Built-in array methods not available on `Result` types, convert the result
636
+ * to a tuple using `$tuple()` first.
637
+ *
638
+ * @deprecated
639
+ */
640
+ reduce(): never;
641
+ /**
642
+ * ## Method not available
643
+ *
644
+ * Built-in array methods not available on `Result` types, convert the result
645
+ * to a tuple using `$tuple()` first.
646
+ *
647
+ * @deprecated
648
+ */
649
+ reduceRight(): never;
650
+ /**
651
+ * ## Method not available
652
+ *
653
+ * Built-in array methods not available on `Result` types, convert the result
654
+ * to a tuple using `$tuple()` first.
655
+ *
656
+ * @deprecated
657
+ */
658
+ reverse(): never;
659
+ /**
660
+ * ## Method not available
661
+ *
662
+ * Built-in array methods not available on `Result` types, convert the result
663
+ * to a tuple using `$tuple()` first.
664
+ *
665
+ * @deprecated
666
+ */
667
+ shift(): never;
668
+ /**
669
+ * ## Method not available
670
+ *
671
+ * Built-in array methods not available on `Result` types, convert the result
672
+ * to a tuple using `$tuple()` first.
673
+ *
674
+ * @deprecated
675
+ */
676
+ slice(): never;
677
+ /**
678
+ * ## Method not available
679
+ *
680
+ * Built-in array methods not available on `Result` types, convert the result
681
+ * to a tuple using `$tuple()` first.
682
+ *
683
+ * @deprecated
684
+ */
685
+ some(): never;
686
+ /**
687
+ * ## Method not available
688
+ *
689
+ * Built-in array methods not available on `Result` types, convert the result
690
+ * to a tuple using `$tuple()` first.
691
+ *
692
+ * @deprecated
693
+ */
694
+ sort(): never;
695
+ /**
696
+ * ## Method not available
697
+ *
698
+ * Built-in array methods not available on `Result` types, convert the result
699
+ * to a tuple using `$tuple()` first.
700
+ *
701
+ * @deprecated
702
+ */
703
+ splice(): never;
704
+ /**
705
+ * ## Method not available
706
+ *
707
+ * Built-in array methods not available on `Result` types, convert the result
708
+ * to a tuple using `$tuple()` first.
709
+ *
710
+ * @deprecated
711
+ */
712
+ toString(): never;
713
+ /**
714
+ * ## Method not available
715
+ *
716
+ * Built-in array methods not available on `Result` types, convert the result
717
+ * to a tuple using `$tuple()` first.
718
+ *
719
+ * @deprecated
720
+ */
721
+ toLocaleString(): never;
722
+ /**
723
+ * ## Method not available
724
+ *
725
+ * Built-in array methods not available on `Result` types, convert the result
726
+ * to a tuple using `$tuple()` first.
727
+ *
728
+ * @deprecated
729
+ */
730
+ toReversed(): never;
731
+ /**
732
+ * ## Method not available
733
+ *
734
+ * Built-in array methods not available on `Result` types, convert the result
735
+ * to a tuple using `$tuple()` first.
736
+ *
737
+ * @deprecated
738
+ */
739
+ toSorted(): never;
740
+ /**
741
+ * ## Method not available
742
+ *
743
+ * Built-in array methods not available on `Result` types, convert the result
744
+ * to a tuple using `$tuple()` first.
745
+ *
746
+ * @deprecated
747
+ */
748
+ toSpliced(): never;
749
+ /**
750
+ * ## Method not available
751
+ *
752
+ * Built-in array methods not available on `Result` types, convert the result
753
+ * to a tuple using `$tuple()` first.
754
+ *
755
+ * @deprecated
756
+ */
757
+ unshift(): never;
758
+ /**
759
+ * ## Method not available
760
+ *
761
+ * Built-in array methods not available on `Result` types, convert the result
762
+ * to a tuple using `$tuple()` first.
763
+ *
764
+ * @deprecated
765
+ */
766
+ values(): never;
767
+ /**
768
+ * ## Method not available
769
+ *
770
+ * Built-in array methods not available on `Result` types, convert the result
771
+ * to a tuple using `$tuple()` first.
772
+ *
773
+ * @deprecated
774
+ */
775
+ with(): never;
776
+ }
73
777
  /**
74
778
  * ## ResultAsync
75
779
  *
@@ -78,209 +782,1174 @@ export declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (er
78
782
  declare class ResultAsync<T, E> {
79
783
  #private;
80
784
  constructor(inner: PromiseLike<Result<T, E>>);
81
- then<TResult1 = Result<T, E>, TResult2 = never>(onfulfilled?: ((result: Result<T, E>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>): PromiseLike<TResult1 | TResult2>;
82
- /**
83
- * @TODO
84
- */
85
- $value(this: ResultAsync<T, E>): Promise<T | E>;
785
+ 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>;
86
786
  /**
87
- * @TODO
787
+ * The same as {@link Retuple.$expect|$expect}, except it returns a `Promise`.
88
788
  */
89
789
  $expect(this: ResultAsync<T, Error>): Promise<T>;
90
790
  /**
91
- * @TODO
791
+ * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
92
792
  */
93
793
  $unwrap(this: ResultAsync<T, E>, msg?: string): Promise<T>;
94
794
  /**
95
- * @TODO
795
+ * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
796
+ * a `Promise`.
96
797
  */
97
798
  $unwrapErr(this: ResultAsync<T, E>, msg?: string): Promise<E>;
98
799
  /**
99
- * @TODO
800
+ * The same as {@link Retuple.$unwrapOr|$unwrapOr}, except it returns
801
+ * a `Promise`.
100
802
  */
101
- $unwrapOr<U>(this: ResultAsync<T, E>, def: U): Promise<T | U>;
803
+ $unwrapOr<const U = T>(this: ResultAsync<T, E>, def: U): Promise<T | U>;
102
804
  /**
103
- * @TODO
805
+ * The same as {@link Retuple.$unwrapOrElse|$unwrapOrElse}, except it returns
806
+ * a `Promise`.
104
807
  */
105
808
  $unwrapOrElse<U = T>(this: ResultAsync<T, E>, f: () => U): Promise<T | U>;
106
809
  /**
107
- * @TODO
810
+ * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
108
811
  */
109
812
  $map<U>(this: ResultAsync<T, E>, f: (val: T) => U): ResultAsync<U, E>;
110
813
  /**
111
- * @TODO
814
+ * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
815
+ * `ResultAsync`.
112
816
  */
113
- $mapErr<F>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
817
+ $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
114
818
  /**
115
- * @TODO
819
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
116
820
  */
117
- $mapOr<U>(this: ResultAsync<T, E>, def: U, f: (val: T) => U): ResultAsync<U, E>;
821
+ $mapOr<U, V = U>(this: ResultAsync<T, E>, def: U, f: (val: T) => V): ResultAsync<U | V, never>;
118
822
  /**
119
- * @TODO
823
+ * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
824
+ * `ResultAsync`.
120
825
  */
121
- $mapOrElse<U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => U): ResultAsync<U, E>;
826
+ $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
122
827
  /**
123
- * @TODO
828
+ * The same as {@link Retuple.$andAssertOr|$andAssertOr}, except it:
829
+ *
830
+ * - can also accept a `PromiseLike` default value;
831
+ * - returns `ResultAsync`.
124
832
  */
125
- $or<U = T, F = E>(this: ResultAsync<T, E>, or: Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<T | U, E | F>;
833
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
834
+ $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>;
835
+ $andAssertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
126
836
  /**
127
- * @TODO
837
+ * The same as {@link Retuple.$andAssertOrElse|$andAssertOrElse}, except it:
838
+ *
839
+ * - can also accept an `async` default function;
840
+ * - returns `ResultAsync`.
128
841
  */
129
- $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<T | U, E | F>;
842
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>): ResultAsync<Truthy<T> | U, E | F>;
843
+ $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>;
844
+ $andAssertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
130
845
  /**
131
- * @TODO
846
+ * The same as {@link Retuple.$or|$or}, except it:
847
+ *
848
+ * - can also accept a `PromiseLike` or value;
849
+ * - returns `ResultAsync`.
132
850
  */
133
- $orSafe<U = T, F = Error>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>, mapError?: (err: unknown) => F): ResultAsync<T | U, E | F>;
851
+ $or<U = T, F = E>(this: ResultAsync<T, E>, or: Retuple<U, F> | PromiseLike<Retuple<U, F>>): ResultAsync<T | U, F>;
134
852
  /**
135
- * @TODO
853
+ * The same as {@link Retuple.$orElse|$orElse}, except it:
854
+ *
855
+ * - can also accept an `async` or function;
856
+ * - returns `ResultAsync`.
136
857
  */
137
- $and<U = T, F = E>(this: ResultAsync<T, E>, and: Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<U, E | F>;
858
+ $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => RetupleAwaitable<U, F>): ResultAsync<T | U, F>;
138
859
  /**
139
- * @TODO
860
+ * The same as {@link Retuple.$orSafe|$orSafe}, except it:
861
+ *
862
+ * - can also accept an `async` safe function;
863
+ * - returns `ResultAsync`.
140
864
  */
141
- $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<U, E | F>;
865
+ $orSafe<U = T>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, Error>;
866
+ $orSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
142
867
  /**
143
868
  * @TODO
144
869
  */
145
- $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => Result<any, F> | PromiseLike<Result<any, F>>): ResultAsync<T, E | F>;
870
+ $orSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<T | U, Error>;
871
+ $orSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, F>;
146
872
  /**
147
- * @TODO
873
+ * The same as {@link Retuple.$and|$and}, except it:
874
+ *
875
+ * - can also accept a `PromiseLike` and value;
876
+ * - returns `ResultAsync`.
148
877
  */
149
- $andSafe<U = T, F = Error>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError?: (err: unknown) => F): ResultAsync<U, E | F>;
878
+ $and<U = T, F = E>(this: ResultAsync<T, E>, and: RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
879
+ /**
880
+ * The same as {@link Retuple.$andThen|$andThen}, except it:
881
+ *
882
+ * - can also accept an `async` and function;
883
+ * - returns `ResultAsync`.
884
+ */
885
+ $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
886
+ /**
887
+ * The same as {@link Retuple.$andThrough|$andThrough}, except it:
888
+ *
889
+ * - can also accept an `async` through function;
890
+ * - returns `ResultAsync`.
891
+ */
892
+ $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<any, F>): ResultAsync<T, E | F>;
893
+ /**
894
+ * The same as {@link Retuple.$andSafe|$andSafe}, except it:
895
+ *
896
+ * - can also accept an `async` safe function;
897
+ * - returns `ResultAsync`.
898
+ */
899
+ $andSafe<U = T>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
900
+ $andSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
150
901
  /**
151
902
  * @TODO
152
903
  */
904
+ $andSafePromise<U = T>(this: ResultAsync<T, E>, promise: PromiseLike<U>): ResultAsync<U, E | Error>;
905
+ $andSafePromise<U = T, F = E>(this: ResultAsync<T, E>, promise: PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
906
+ /**
907
+ * The same as {@link Retuple.$peek|$peek}, except it:
908
+ *
909
+ * - awaits the peek function;
910
+ * - returns `ResultAsync`.
911
+ */
153
912
  $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
154
913
  /**
155
- * @TODO
914
+ * The same as {@link Retuple.$tap|$tap}, except it:
915
+ *
916
+ * - awaits the tap function;
917
+ * - returns `ResultAsync`.
156
918
  */
157
919
  $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
158
920
  /**
159
- * @TODO
921
+ * The same as {@link Retuple.$tapErr|$tapErr}, except it:
922
+ *
923
+ * - awaits the tap error function;
924
+ * - returns `ResultAsync`.
160
925
  */
161
926
  $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
162
927
  /**
163
- * @TODO
928
+ * The same as {@link Retuple.$promise|$promise}.
164
929
  */
165
930
  $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
931
+ /**
932
+ * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
933
+ */
934
+ $tuple(this: ResultAsync<T, E>): Promise<[err: E | undefined, value: T | undefined]>;
935
+ /**
936
+ * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
937
+ */
938
+ $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
166
939
  }
940
+ type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
167
941
  type OkTuple<T> = [err: undefined, value: T];
168
942
  type ErrTuple<E> = [err: E, value: undefined];
169
- /**
170
- * @TODO - Result.all / Result.any
171
- */
172
- interface Retuple<T, E> {
173
- /**
174
- * @TODO
175
- */
176
- $isOk(this: Result<T, E>): this is Ok<T>;
943
+ type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
944
+ type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
945
+ type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
946
+ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
177
947
  /**
178
- * @TODO
948
+ * Returns true when this result is `Ok`. Acts as a type guard.
949
+ *
950
+ * @example
951
+ *
952
+ * ```ts
953
+ * const result = Ok("test");
954
+ * assert.equal(result.$isOk(), true);
955
+ * ```
956
+ *
957
+ * @example
958
+ *
959
+ * ```ts
960
+ * const result = Err("test");
961
+ * assert.equal(result.$isOk(), false);
962
+ * ```
963
+ *
964
+ * @example
965
+ *
966
+ * ```ts
967
+ * const result: Result<string, Error> = someResultFn();
968
+ *
969
+ * if (result.$isOk()) {
970
+ * result satisfies Result<string, never>;
971
+ * }
972
+ * ```
179
973
  */
180
- $isOkAnd<U extends T = T>(this: Result<T, E>, f: ((val: T) => val is U) | ((val: T) => boolean)): this is Ok<U>;
974
+ $isOk(this: Result<T, E>): this is Result<T, never>;
181
975
  /**
182
- * @TODO
976
+ * Returns true when this result is `Ok`, and when the predicate/condition
977
+ * function returns true. Acts as a type guard.
978
+ *
979
+ * @example
980
+ *
981
+ * ```ts
982
+ * const result = Ok("test");
983
+ * assert.equal(result.$isOkAnd((val) => val === "test"), true);
984
+ * ```
985
+ *
986
+ * @example
987
+ *
988
+ * ```ts
989
+ * const result = Ok<string>("test");
990
+ * assert.equal(result.$isOkAnd((val) => val !== "test"), false);
991
+ * ```
992
+ *
993
+ * @example
994
+ *
995
+ * ```ts
996
+ * const result = Err("test");
997
+ * assert.equal(result.$isOkAnd((err) => err === "test"), false);
998
+ * ```
999
+ *
1000
+ * @example
1001
+ *
1002
+ * ```ts
1003
+ * const result: Result<string | number, Error> = someResultFn();
1004
+ *
1005
+ * if (result.$isOkAnd((val): val is number => typeof val === "number")) {
1006
+ * result satisfies Result<number, never>;
1007
+ * }
1008
+ * ```
183
1009
  */
184
- $isErr(this: Result<T, E>): this is Err<E>;
1010
+ $isOkAnd<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): this is Result<U, never>;
1011
+ $isOkAnd(this: Result<T, E>, predicate: (val: T) => unknown): this is Result<T, never>;
185
1012
  /**
186
- * @TODO
1013
+ * Returns true when this result is `Err`. Acts as a type guard.
1014
+ *
1015
+ * @example
1016
+ *
1017
+ * ```ts
1018
+ * const result = Err("test");
1019
+ * assert.equal(result.$isErr(), true);
1020
+ * ```
1021
+ *
1022
+ * @example
1023
+ *
1024
+ * ```ts
1025
+ * const result = Ok("test");
1026
+ * assert.equal(result.$isErr(), false);
1027
+ * ```
1028
+ *
1029
+ * @example
1030
+ *
1031
+ * ```ts
1032
+ * const result: Result<string, Error> = someResultFn();
1033
+ *
1034
+ * if (result.$isErr()) {
1035
+ * result satisfies Result<never, Error>;
1036
+ * }
1037
+ * ```
187
1038
  */
188
- $isErrAnd<F extends E = E>(this: Result<T, E>, f: ((err: E) => err is F) | ((err: E) => boolean)): this is Err<F>;
1039
+ $isErr(this: Result<T, E>): this is Result<never, E>;
189
1040
  /**
190
- * @TODO
1041
+ * Returns true when this result is `Err`, and when the predicate/condition
1042
+ * function returns true. Acts as a type guard.
1043
+ *
1044
+ * @example
1045
+ *
1046
+ * ```ts
1047
+ * const result = Err("test");
1048
+ * assert.equal(result.$isErrAnd((err) => err === "test"), true);
1049
+ * ```
1050
+ *
1051
+ * @example
1052
+ *
1053
+ * ```ts
1054
+ * const result = Err<string>("test");
1055
+ * assert.equal(result.$isErrAnd((err) => err !== "test"), false);
1056
+ * ```
1057
+ *
1058
+ * @example
1059
+ *
1060
+ * ```ts
1061
+ * const result = Ok("test");
1062
+ * assert.equal(result.$isErrAnd((val) => val === "test"), false);
1063
+ * ```
1064
+ *
1065
+ * @example
1066
+ *
1067
+ * ```ts
1068
+ * const result: Result<string, Error | number> = someResultFn();
1069
+ *
1070
+ * if (result.$isErrAnd((err): err is number => typeof err === "number")) {
1071
+ * result satisfies Result<never, number>;
1072
+ * }
1073
+ * ```
191
1074
  */
192
- $value(this: Result<T, E>): T | E;
1075
+ $isErrAnd<F extends E = E>(this: Result<T, E>, prediacte: (val: E) => val is F): this is Result<never, F>;
1076
+ $isErrAnd(this: Result<T, E>, predicate: (val: E) => unknown): this is Result<never, E>;
193
1077
  /**
194
- * @TODO
1078
+ * Returns the ok value when this result is `Ok`.
1079
+ *
1080
+ * Otherwise, the error value is thrown.
1081
+ *
1082
+ * This method should only be called when the `E` type extends `Error`. This
1083
+ * is enforced with a type constraint. If the error value is not an instance
1084
+ * of Error, `RetupleExpectFailed` is thrown. Use
1085
+ * {@link Retuple.$unwrap|$unwrap} When the `E` type does not extend Error.
1086
+ *
1087
+ * @example
1088
+ *
1089
+ * ```ts
1090
+ * const result = Ok("test");
1091
+ * assert.equal(result.$expect(), "test");
1092
+ * ```
1093
+ *
1094
+ * @example
1095
+ *
1096
+ * ```ts
1097
+ * const error = new Error("test");
1098
+ * const result = Err(error);
1099
+ *
1100
+ * try {
1101
+ * const value = result.$expect(); // throws
1102
+ * } catch (e) {
1103
+ * assert.equal(e, error);
1104
+ * }
1105
+ * ```
1106
+ *
1107
+ * @example
1108
+ *
1109
+ * ```ts
1110
+ * const result = Err("test");
1111
+ *
1112
+ * try {
1113
+ * // This is a type error - the E type does not extend Error
1114
+ * const value = result.$expect(); // throws
1115
+ * } catch (e) {
1116
+ * assert(e instanceof RetupleExpectFailed && e.value === "test");
1117
+ * }
1118
+ * ```
195
1119
  */
196
1120
  $expect(this: Result<T, Error>): T;
197
1121
  /**
198
- * @TODO
1122
+ * Returns the ok value when this result is `Ok`.
1123
+ *
1124
+ * Otherwise, `RetupleUnwrapFailed` is thrown. A custom error message can be
1125
+ * provided.
1126
+ *
1127
+ * @example
1128
+ *
1129
+ * ```ts
1130
+ * const result = Ok("test");
1131
+ * assert.equal(result.$unwrap(), "test");
1132
+ * ```
1133
+ *
1134
+ * @example
1135
+ *
1136
+ * ```ts
1137
+ * const result = Err("test");
1138
+ *
1139
+ * try {
1140
+ * const value = result.$unwrap(); // throws
1141
+ * } catch (e) {
1142
+ * assert(e instanceof RetupleUnwrapFailed && e.value === "test");
1143
+ * }
1144
+ * ```
1145
+ *
1146
+ * @example
1147
+ *
1148
+ * ```ts
1149
+ * const error = new Error("test");
1150
+ * const result = Err(error);
1151
+ *
1152
+ * try {
1153
+ * const value = result.$unwrap("error-message"); // throws
1154
+ * } catch (e) {
1155
+ * assert(
1156
+ * e instanceof RetupleUnwrapFailed &&
1157
+ * e.message === "error-message" &&
1158
+ * e.value === error &&
1159
+ * e.cause === error, // set when error value was an instance of `Error`
1160
+ * );
1161
+ * }
1162
+ * ```
199
1163
  */
200
1164
  $unwrap(this: Result<T, E>, msg?: string): T;
201
1165
  /**
202
- * @TODO
1166
+ * Returns the error value when this result is `Err`.
1167
+ *
1168
+ * Otherwise, `RetupleUnwrapErrFailed` is thrown. A custom error message can
1169
+ * be provided.
1170
+ *
1171
+ * @example
1172
+ *
1173
+ * ```ts
1174
+ * const result = Err("test");
1175
+ * assert.equal(result.$unwrapErr(), "test");
1176
+ * ```
1177
+ *
1178
+ * @example
1179
+ *
1180
+ * ```ts
1181
+ * const result = Ok("test");
1182
+ *
1183
+ * try {
1184
+ * const value = result.$unwrapErr(); // throws
1185
+ * } catch (e) {
1186
+ * assert(e instanceof RetupleUnwrapErrFailed && e.value === "test");
1187
+ * }
1188
+ * ```
1189
+ *
1190
+ * @example
1191
+ *
1192
+ * ```ts
1193
+ * const result = Ok("test");
1194
+ *
1195
+ * try {
1196
+ * const value = result.$unwrapErr("error-message"); // throws
1197
+ * } catch (e) {
1198
+ * assert(
1199
+ * e instanceof RetupleUnwrapErrFailed &&
1200
+ * e.message === "error-message" &&
1201
+ * e.value === "test",
1202
+ * );
1203
+ * }
1204
+ * ```
203
1205
  */
204
1206
  $unwrapErr(this: Result<T, E>, msg?: string): E;
205
1207
  /**
206
- * @TODO
1208
+ * Returns the ok value when this result is `Ok`.
1209
+ *
1210
+ * Otherwise, returns the default value.
1211
+ *
1212
+ * @example
1213
+ *
1214
+ * ```ts
1215
+ * const result = Ok("test");
1216
+ * assert.equal(result.$unwrapOr("default"), "test");
1217
+ * ```
1218
+ *
1219
+ * @example
1220
+ *
1221
+ * ```ts
1222
+ * const result = Err("test");
1223
+ * assert.equal(result.$unwrapOr("default"), "default");
1224
+ * ```
207
1225
  */
208
- $unwrapOr<U = T>(this: Result<T, E>, def: U): T | U;
1226
+ $unwrapOr<const U = T>(this: Result<T, E>, def: U): T | U;
209
1227
  /**
210
- * @TODO
1228
+ * Returns the ok value when this result is `Ok`.
1229
+ *
1230
+ * Otherwise, returns the value returned by the default function.
1231
+ *
1232
+ * @example
1233
+ *
1234
+ * ```ts
1235
+ * const result = Ok("test");
1236
+ * assert.equal(result.$unwrapOrElse(() => "default"), "test");
1237
+ * ```
1238
+ *
1239
+ * @example
1240
+ *
1241
+ * ```ts
1242
+ * const result = Err("test");
1243
+ * assert.equal(result.$unwrapOrElse(() => "default"), "default");
1244
+ * ```
211
1245
  */
212
1246
  $unwrapOrElse<U = T>(this: Result<T, E>, f: () => U): T | U;
213
1247
  /**
214
- * @TODO
1248
+ * Performs an assertion when this result is `Ok`:
1249
+ *
1250
+ * - returning `Ok` containing the current ok value when it is truthy, and
1251
+ * when no predicate/condition function is provided. Narrows the `T` type
1252
+ * to include only truthy values;
1253
+ * - returning `Ok` containing the current ok value when a
1254
+ * predicate/condition function is provided and it returns a truthy value.
1255
+ * Narrows the `T` type to the predicate type (if any);
1256
+ * - returning the default result when no predicate/condition function is
1257
+ * provided and the current ok value is falsey;
1258
+ * - returning the default result when a predicate/condition function is
1259
+ * provided and it returns a falsey value.
1260
+ *
1261
+ * Otherwise returns `Err` containing the current error value.
1262
+ *
1263
+ * @example
1264
+ *
1265
+ * ```ts
1266
+ * const result: Result<string | null, string> = Ok("test");
1267
+ * const asserted = result.$andAssertOr(Ok("ok-default"));
1268
+ *
1269
+ * asserted satisfies Result<string, string>;
1270
+ * assert.equal(asserted.$unwrap(), "test");
1271
+ * ```
1272
+ *
1273
+ * @example
1274
+ *
1275
+ * ```ts
1276
+ * const result: Result<string | null, string> = Ok("test");
1277
+ * const asserted = result.$andAssertOr(
1278
+ * Err("err-default"),
1279
+ * (val): val is "test" => val === "test",
1280
+ * );
1281
+ *
1282
+ * asserted satisfies Result<"test", string>;
1283
+ * assert.equal(asserted.$unwrap(), "test");
1284
+ * ```
1285
+ *
1286
+ * @example
1287
+ *
1288
+ * ```ts
1289
+ * const result: Result<string | null, string> = Ok(null);
1290
+ * const asserted = result.$andAssertOr(Ok("ok-default"));
1291
+ *
1292
+ * asserted satisfies Result<string, string>;
1293
+ * assert.equal(asserted.$unwrap(), "ok-default");
1294
+ * ```
1295
+ *
1296
+ * @example
1297
+ *
1298
+ * ```ts
1299
+ * const result: Result<string | null, string> = Ok("value");
1300
+ * const asserted = result.$andAssertOr(
1301
+ * Err("err-default"),
1302
+ * (val): val is "test" => val === "test",
1303
+ * );
1304
+ *
1305
+ * asserted satisfies Result<"test", string>;
1306
+ * assert.equal(asserted.$unwrapErr(), "err-default");
1307
+ * ```
1308
+ *
1309
+ * @example
1310
+ *
1311
+ * ```ts
1312
+ * const result: Result<string | null, string> = Err("test");
1313
+ * const asserted = result.$andAssertOr(
1314
+ * Err("err-default"),
1315
+ * (val): val is "test" => val === "test",
1316
+ * );
1317
+ *
1318
+ * asserted satisfies Result<"test", string>;
1319
+ * assert.equal(asserted.$unwrapErr(), "test");
1320
+ * ```
1321
+ */
1322
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T> | U, E | F>;
1323
+ $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>;
1324
+ $andAssertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1325
+ /**
1326
+ * Performs an assertion when this result is `Ok`:
1327
+ *
1328
+ * - returning `Ok` containing the current ok value when it is truthy, and
1329
+ * when no predicate/condition function is provided. Narrows the `T` type
1330
+ * to include only truthy values;
1331
+ * - returning `Ok` containing the current ok value when a
1332
+ * predicate/condition function is provided and it returns a truthy value.
1333
+ * Narrows the `T` type to the predicate type (if any);
1334
+ * - returning the result returned by the default function when no
1335
+ * predicate/condition function is provided and the current ok value is
1336
+ * falsey;
1337
+ * - returning the result returned by the default function when a
1338
+ * predicate/condition function is provided and it returns a falsey value.
1339
+ *
1340
+ * Otherwise returns `Err` containing the current error value.
1341
+ *
1342
+ * @example
1343
+ *
1344
+ * ```ts
1345
+ * const result: Result<string | null, string> = Ok("test");
1346
+ * const asserted = result.$andAssertOrElse(
1347
+ * (val) => Ok(`ok-default:${val}`),
1348
+ * );
1349
+ *
1350
+ * asserted satisfies Result<string, string>;
1351
+ * assert.equal(asserted.$unwrap(), "test");
1352
+ * ```
1353
+ *
1354
+ * @example
1355
+ *
1356
+ * ```ts
1357
+ * const result: Result<string | null, string> = Ok("test");
1358
+ * const asserted = result.$andAssertOrElse(
1359
+ * (val) => Err(`err-default:${val}`),
1360
+ * (val): val is "test" => val === "test",
1361
+ * );
1362
+ *
1363
+ * asserted satisfies Result<"test", string>;
1364
+ * assert.equal(asserted.$unwrap(), "test");
1365
+ * ```
1366
+ *
1367
+ * @example
1368
+ *
1369
+ * ```ts
1370
+ * const result: Result<string | null, string> = Ok(null);
1371
+ * const asserted = result.$andAssertOrElse(
1372
+ * (val) => Ok(`ok-default:${val}`),
1373
+ * );
1374
+ *
1375
+ * asserted satisfies Result<string, string>;
1376
+ * assert.equal(asserted.$unwrap(), "ok-default:null");
1377
+ * ```
1378
+ *
1379
+ * @example
1380
+ *
1381
+ * ```ts
1382
+ * const result: Result<string | null, string> = Ok("value");
1383
+ * const asserted = result.$andAssertOrElse(
1384
+ * (val) => Err(`err-default:${val}`),
1385
+ * (val): val is "test" => val === "test",
1386
+ * );
1387
+ *
1388
+ * asserted satisfies Result<"test", string>;
1389
+ * assert.equal(asserted.$unwrapErr(), "err-default:value");
1390
+ * ```
1391
+ *
1392
+ * @example
1393
+ *
1394
+ * ```ts
1395
+ * const result: Result<string | null, string> = Err("test");
1396
+ * const asserted = result.$andAssertOrElse(
1397
+ * (val) => Err(`err-default:${val}`),
1398
+ * (val): val is "test" => val === "test",
1399
+ * );
1400
+ *
1401
+ * asserted satisfies Result<"test", string>;
1402
+ * assert.equal(asserted.$unwrapErr(), "test");
1403
+ * ```
1404
+ */
1405
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>): Result<Truthy<T> | U, E | F>;
1406
+ $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>;
1407
+ $andAssertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
1408
+ /**
1409
+ * Returns `Ok` containing the return value of the map function when this
1410
+ * result is `Ok`.
1411
+ *
1412
+ * Otherwise, returns `Err` containing the current error value.
1413
+ *
1414
+ * @example
1415
+ *
1416
+ * ```ts
1417
+ * const result = Ok("test");
1418
+ * assert.equal(
1419
+ * result.$map((val) => `map:${val}`).$unwrap(),
1420
+ * "map:test",
1421
+ * );
1422
+ * ```
1423
+ *
1424
+ * @example
1425
+ *
1426
+ * ```ts
1427
+ * const result: Result<string, string> = Err("test");
1428
+ * assert.equal(
1429
+ * result.$map((val) => `map:${val}`).$unwrapErr(),
1430
+ * "test",
1431
+ * );
1432
+ * ```
215
1433
  */
216
1434
  $map<U>(this: Result<T, E>, f: (value: T) => U): Result<U, E>;
217
1435
  /**
218
- * @TODO
1436
+ * Returns `Err` containing the return value of the map function when this
1437
+ * result is `Err`.
1438
+ *
1439
+ * Otherwise, returns `Ok` containing the current ok value.
1440
+ *
1441
+ * @example
1442
+ *
1443
+ * ```ts
1444
+ * const result = Err("test");
1445
+ * assert.equal(
1446
+ * result.$mapErr((err) => `map-err:${err}`).$unwrapErr(),
1447
+ * "map-err:test",
1448
+ * );
1449
+ * ```
1450
+ *
1451
+ * @example
1452
+ *
1453
+ * ```ts
1454
+ * const result: Result<string, string> = Ok("test");
1455
+ * assert.equal(
1456
+ * result.$mapErr((err) => `map-err:${err}`).$unwrap(),
1457
+ * "test",
1458
+ * );
1459
+ * ```
219
1460
  */
220
- $mapErr<F>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
1461
+ $mapErr<F = E>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
221
1462
  /**
222
- * @TODO
1463
+ * Returns `Ok` containing the return value of the map function when this
1464
+ * result is `Ok`.
1465
+ *
1466
+ * Otherwise, returns `Ok` containing the default value.
1467
+ *
1468
+ * @example
1469
+ *
1470
+ * ```ts
1471
+ * const result: Result<string, string> = Ok("test");
1472
+ * assert.equal(
1473
+ * result.$mapOr("default", (val) => `map:${val}`).$unwrap(),
1474
+ * "map:test",
1475
+ * );
1476
+ * ```
1477
+ *
1478
+ * @example
1479
+ *
1480
+ * ```ts
1481
+ * const result: Result<string, string> = Err("test");
1482
+ * assert.equal(
1483
+ * result.$mapOr("default", (val) => `map:${val}`).$unwrap(),
1484
+ * "default",
1485
+ * );
1486
+ * ```
223
1487
  */
224
- $mapOr<U>(this: Result<T, E>, def: U, f: (val: T) => U): Result<U, E>;
1488
+ $mapOr<U, V = U>(this: Result<T, E>, def: U, f: (val: T) => V): Result<U | V, E>;
225
1489
  /**
226
- * @TODO
1490
+ * Returns `Ok` containing the return value of the map function when this
1491
+ * result is `Ok`.
1492
+ *
1493
+ * Otherwise, returns `Ok` containing the return value of the default
1494
+ * function.
1495
+ *
1496
+ * @example
1497
+ *
1498
+ * ```ts
1499
+ * const result: Result<string, string> = Ok("test");
1500
+ * assert.equal(
1501
+ * result.$mapOrElse(
1502
+ * (err) => `default:${err}`,
1503
+ * (val) => `map:${val}`,
1504
+ * ).$unwrap(),
1505
+ * "map:test",
1506
+ * );
1507
+ * ```
1508
+ *
1509
+ * @example
1510
+ *
1511
+ * ```ts
1512
+ * const result: Result<string, string> = Err("test");
1513
+ * assert.equal(
1514
+ * result.$mapOrElse(
1515
+ * (err) => `default:${err}`,
1516
+ * (val) => `map:${val}`,
1517
+ * ).$unwrap(),
1518
+ * "default:test",
1519
+ * );
1520
+ * ```
227
1521
  */
228
- $mapOrElse<U>(this: Result<T, E>, def: (err: E) => U, f: (val: T) => U): Result<U, E>;
1522
+ $mapOrElse<U, V = U>(this: Result<T, E>, def: (err: E) => U, f: (val: T) => V): Result<U | V, E>;
229
1523
  /**
230
- * @TODO
1524
+ * Returns the or result, when this result is `Err`.
1525
+ *
1526
+ * Otherwise, returns `Ok` containing the current ok value.
1527
+ *
1528
+ * @example
1529
+ *
1530
+ * ```ts
1531
+ * const result = Err("test");
1532
+ * assert.equal(
1533
+ * result.$or(Ok("or-ok")).$unwrap(),
1534
+ * "or-ok",
1535
+ * );
1536
+ * ```
1537
+ *
1538
+ * @example
1539
+ *
1540
+ * ```ts
1541
+ * const result = Err("test");
1542
+ * assert.equal(
1543
+ * result.$or(Err("or-err")).$unwrapErr(),
1544
+ * "or-err",
1545
+ * );
1546
+ * ```
1547
+ *
1548
+ * @example
1549
+ *
1550
+ * ```ts
1551
+ * const result = Ok("test");
1552
+ * assert.equal(
1553
+ * result.$or(Ok("or-ok")).$unwrap(),
1554
+ * "test",
1555
+ * );
1556
+ * ```
231
1557
  */
232
- $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, E | F>;
1558
+ $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, F>;
233
1559
  /**
234
- * @TODO
1560
+ * Returns the result returned by the or function, when this result is `Err`.
1561
+ *
1562
+ * Otherwise, returns `Ok` containing the current ok value.
1563
+ *
1564
+ * @example
1565
+ *
1566
+ * ```ts
1567
+ * const result = Err("test");
1568
+ * assert.equal(
1569
+ * result.$orElse((err) => Ok(`or-ok:${err}`)).$unwrap(),
1570
+ * "or-ok:test",
1571
+ * );
1572
+ * ```
1573
+ *
1574
+ * @example
1575
+ *
1576
+ * ```ts
1577
+ * const result = Err("test");
1578
+ * assert.equal(
1579
+ * result.$orElse((err) => Err(`or-err:${err}`)).$unwrapErr(),
1580
+ * "or-err:test",
1581
+ * );
1582
+ * ```
1583
+ *
1584
+ * @example
1585
+ *
1586
+ * ```ts
1587
+ * const result: Result<string, string> = Ok("test");
1588
+ * assert.equal(
1589
+ * result.$orElse((err) => Ok(`or-ok:${err}`)).$unwrap(),
1590
+ * "test",
1591
+ * );
1592
+ * ```
235
1593
  */
236
- $orElse<U = never, F = never>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, E | F>;
1594
+ $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, F>;
237
1595
  /**
238
- * @TODO
1596
+ * Returns a `Result` based on the outcome of the safe function when this
1597
+ * result is `Err`.
1598
+ *
1599
+ * Otherwise, returns `Ok` containing the current ok value.
1600
+ *
1601
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1602
+ * `result.$or(Result.$safe(...))`.
239
1603
  */
240
- $orSafe<U = T>(this: Result<T, E>, f: (err: E) => U): Result<T | U, E | Error>;
241
- $orSafe<U = T, F = E>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
242
- $orSafe<U = T, F = Error>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
1604
+ $orSafe<U = T>(this: Result<T, E>, f: (err: E) => U): Result<T | U, Error>;
1605
+ $orSafe<U = T, F = E>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, F>;
243
1606
  /**
244
- * @TODO
1607
+ * Returns the and result, when this result is `Ok`.
1608
+ *
1609
+ * Otherwise, returns `Err` containing the current error value.
1610
+ *
1611
+ * @example
1612
+ *
1613
+ * ```ts
1614
+ * const result = Ok("test");
1615
+ * assert.equal(
1616
+ * result.$and(Ok("and-ok")).$unwrap(),
1617
+ * "and-ok",
1618
+ * );
1619
+ * ```
1620
+ *
1621
+ * @example
1622
+ *
1623
+ * ```ts
1624
+ * const result = Ok("test");
1625
+ * assert.equal(
1626
+ * result.$and(Err("and-err")).$unwrapErr(),
1627
+ * "and-err",
1628
+ * );
1629
+ * ```
1630
+ *
1631
+ * @example
1632
+ *
1633
+ * ```ts
1634
+ * const result = Err("test");
1635
+ * assert.equal(
1636
+ * result.$and(Ok("and-ok")).$unwrapErr(),
1637
+ * "test",
1638
+ * );
1639
+ * ```
245
1640
  */
246
1641
  $and<U = T, F = E>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
247
1642
  /**
248
- * @TODO
1643
+ * Returns the and result, when this result is `Ok`.
1644
+ *
1645
+ * Otherwise, returns `Err` containing the current error value.
1646
+ *
1647
+ * @example
1648
+ *
1649
+ * ```ts
1650
+ * const result = Ok("test");
1651
+ * assert.equal(
1652
+ * result.$and((val) => Ok(`and-ok:${val}`)).$unwrap(),
1653
+ * "and-ok:test",
1654
+ * );
1655
+ * ```
1656
+ *
1657
+ * @example
1658
+ *
1659
+ * ```ts
1660
+ * const result = Ok("test");
1661
+ * assert.equal(
1662
+ * result.$and((val) => Err(`and-err:${val}`)).$unwrapErr(),
1663
+ * "and-err:test",
1664
+ * );
1665
+ * ```
1666
+ *
1667
+ * @example
1668
+ *
1669
+ * ```ts
1670
+ * const result: Result<string, string> = Err("test");
1671
+ * assert.equal(
1672
+ * result.$and((val) => Ok(`and-ok:${val}`)).$unwrapErr(),
1673
+ * "test",
1674
+ * );
1675
+ * ```
249
1676
  */
250
- $andThen<U = never, F = never>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
1677
+ $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
251
1678
  /**
252
- * @TODO
1679
+ * Calls the through function when this result is `Ok` and returns:
1680
+ *
1681
+ * - `Ok` containing the original ok value when the through function
1682
+ * returns `Ok`;
1683
+ * - the `Err` returned by the through function when it returns `Err`.
1684
+ *
1685
+ * Otherwise, returns `Err` containing the current error value.
1686
+ *
1687
+ * @example
1688
+ *
1689
+ * ```ts
1690
+ * const result = Ok("test");
1691
+ * assert.equal(
1692
+ * result.$andThrough((val) => Ok(`ok-through:${val}`)).$unwrap(),
1693
+ * "test",
1694
+ * );
1695
+ * ```
1696
+ *
1697
+ * @example
1698
+ *
1699
+ * ```ts
1700
+ * const result = Ok("test");
1701
+ * assert.equal(
1702
+ * result.$andThrough((val) => Err(`err-through:${val}`)).$unwrapErr(),
1703
+ * "err-through:test",
1704
+ * );
1705
+ * ```
1706
+ *
1707
+ * @example
1708
+ *
1709
+ * ```ts
1710
+ * const result: Result<string, string> = Err("test");
1711
+ * assert.equal(
1712
+ * result.$andThrough((val) => Ok(`ok-through:${val}`)).$unwrapErr(),
1713
+ * "test",
1714
+ * );
1715
+ * ```
253
1716
  */
254
- $andThrough<F = never>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
1717
+ $andThrough<F = E>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
255
1718
  /**
256
- * @TODO
1719
+ * Returns a result based on the outcome of the safe function when this
1720
+ * result is `Ok`.
1721
+ *
1722
+ * Otherwise, returns `Err` containing the current error value.
1723
+ *
1724
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1725
+ * `result.$and(Result.$safe(...))`.
257
1726
  */
258
- $andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<T | U, E | Error>;
259
- $andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
260
- $andSafe<U, F = Error>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
1727
+ $andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
1728
+ $andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
261
1729
  /**
262
1730
  * @TODO
263
1731
  */
1732
+ /**
1733
+ * Calls the peek function and returns `Result` equivalent to this result.
1734
+ *
1735
+ * @example
1736
+ *
1737
+ * ```ts
1738
+ * const result: Result<string, string> = Ok("test");
1739
+ * assert.equal(
1740
+ * result
1741
+ * .$peek((res) => {
1742
+ * const [err, value] = res;
1743
+ *
1744
+ * console.log("Err:", err); // Err: undefined
1745
+ * console.log("Value:", value); // Value: test
1746
+ * })
1747
+ * .$unwrap(),
1748
+ * "test",
1749
+ * );
1750
+ * ```
1751
+ *
1752
+ * @example
1753
+ *
1754
+ * ```ts
1755
+ * const result: Result<string, string> = Err("test");
1756
+ * assert.equal(
1757
+ * result
1758
+ * .$peek((res) => {
1759
+ * const [err, value] = res;
1760
+ *
1761
+ * console.log("Err:", err); // Err: test
1762
+ * console.log("Value:", value); // Value: undefined
1763
+ * })
1764
+ * .$unwrapErr(),
1765
+ * "test",
1766
+ * );
1767
+ * ```
1768
+ */
264
1769
  $peek(this: Result<T, E>, f: (res: Result<T, E>) => void): Result<T, E>;
265
1770
  /**
266
- * @TODO
1771
+ * Calls the tap function when this result is `Ok`, and returns `Ok`
1772
+ * containing the current ok value.
1773
+ *
1774
+ * @example
1775
+ *
1776
+ * ```ts
1777
+ * const result = Ok("test");
1778
+ * assert.equal(
1779
+ * result
1780
+ * .$tap((val) => console.log("Value:", val)) // Value: test
1781
+ * .$unwrap(),
1782
+ * "test",
1783
+ * );
1784
+ * ```
1785
+ *
1786
+ * @example
1787
+ *
1788
+ * ```ts
1789
+ * const result: Record<string, string> = Err("test");
1790
+ * assert.equal(
1791
+ * result
1792
+ * .$tap((val) => console.log("Value:", val)) // not executed
1793
+ * .$unwrapErr(),
1794
+ * "test",
1795
+ * );
1796
+ * ```
267
1797
  */
268
1798
  $tap(this: Result<T, E>, f: (val: T) => any): Result<T, E>;
269
1799
  /**
270
- * @TODO
1800
+ * Calls the tap error function when this result is `Err`, and returns `Err`
1801
+ * containing the current error value.
1802
+ *
1803
+ * @example
1804
+ *
1805
+ * ```ts
1806
+ * const result = Err("test");
1807
+ * assert.equal(
1808
+ * result
1809
+ * .$tapErr((err) => console.log("Err:", err)) // Err: test
1810
+ * .$unwrapErr(),
1811
+ * "test",
1812
+ * );
1813
+ * ```
1814
+ *
1815
+ * @example
1816
+ *
1817
+ * ```ts
1818
+ * const result: Record<string, string> = Ok("test");
1819
+ * assert.equal(
1820
+ * result
1821
+ * .$tapErr((err) => console.log("Err:", err)) // not executed
1822
+ * .$unwrap(),
1823
+ * "test",
1824
+ * );
1825
+ * ```
271
1826
  */
272
1827
  $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
273
1828
  /**
274
- * @TODO
1829
+ * Returns the contained `Result` when this result is `Ok`.
1830
+ *
1831
+ * Otherwise returns `Err` containing the current error value.
1832
+ *
1833
+ * This method should only be called when the `T` type is `Result`. This
1834
+ * is enforced with a type constraint. If the ok value is not
1835
+ * a result, `RetupleFlattenFailed` is thrown.
1836
+ *
1837
+ * @example
1838
+ *
1839
+ * ```ts
1840
+ * const result = Ok(Ok("test"));
1841
+ * assert.equal(result.$flatten().$unwrap(), "test");
1842
+ * ```
1843
+ *
1844
+ * @example
1845
+ *
1846
+ * ```ts
1847
+ * const result = Ok(Err("test"));
1848
+ * assert.equal(result.$flatten().$unwrapErr(), "test");
1849
+ * ```
1850
+ *
1851
+ * @example
1852
+ *
1853
+ * ```ts
1854
+ * const result = Err("test");
1855
+ * assert.equal(result.$flatten().$unwrapErr(), "test");
1856
+ * ```
275
1857
  */
276
1858
  $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
277
1859
  /**
278
- * @TODO
1860
+ * Returns an equivalent `ResultAsync`.
1861
+ *
1862
+ * @example
1863
+ *
1864
+ * ```ts
1865
+ * const result = Ok("test").$async();
1866
+ * assert.equal(await result.$unwrap(), "test");
1867
+ * ```
1868
+ * @example
1869
+ *
1870
+ * ```ts
1871
+ * const result = Err("test").$async();
1872
+ * assert.equal(await result.$unwrapErr(), "test");
1873
+ * ```
279
1874
  */
280
1875
  $async(this: Result<T, E>): ResultAsync<T, E>;
281
1876
  /**
282
- * @TODO
1877
+ * Returns a `Promise` which resolves to this result.
1878
+ *
1879
+ * @example
1880
+ *
1881
+ * ```ts
1882
+ * const result = Ok("test").$promise();
1883
+ * assert.equal(await result, result);
1884
+ * ```
1885
+ *
1886
+ * @example
1887
+ *
1888
+ * ```ts
1889
+ * const result = Err("test").$promise();
1890
+ * assert.equal(await result, result);
1891
+ * ```
283
1892
  */
284
1893
  $promise(this: Result<T, E>): Promise<Result<T, E>>;
1894
+ /**
1895
+ * Returns a two-element, standard array tuple equivalent to this result.
1896
+ *
1897
+ * @example
1898
+ *
1899
+ * ```ts
1900
+ * const result = Ok("test");
1901
+ * assert.deepEqual(result.$tuple(), [undefined, "test"]);
1902
+ * ```
1903
+ *
1904
+ * @example
1905
+ *
1906
+ * ```ts
1907
+ * const result = Err("test");
1908
+ * assert.deepEqual(result.$tuple(), ["test", undefined]);
1909
+ * ```
1910
+ */
1911
+ $tuple(this: Result<T, E>): [err: E | undefined, value: T | undefined];
1912
+ /**
1913
+ * Returns an `IterableIterator` over the contained ok value, when this
1914
+ * result is `Ok`.
1915
+ *
1916
+ * Otherwise, returns an empty `IterableIterator`.
1917
+ *
1918
+ * This method should only be called when the `T` type is `Iterable`. This
1919
+ * is enforced with a type constraint. If the ok value is not iterable,
1920
+ * attempting to iterate over it will throw the built-in error.
1921
+ *
1922
+ * @example
1923
+ *
1924
+ * ```ts
1925
+ * const result = Ok([1, 2, 3]);
1926
+ *
1927
+ * for (const n of result.$iter()) {
1928
+ * console.log(n); // 1.. 2.. 3
1929
+ * }
1930
+ * ```
1931
+ *
1932
+ * @example
1933
+ *
1934
+ * ```ts
1935
+ * const result = Err([1, 2, 3]);
1936
+ *
1937
+ * for (const n of result.$iter()) {
1938
+ * console.log(n); // not executed, iterator is empty
1939
+ * }
1940
+ * ```
1941
+ *
1942
+ * @example
1943
+ *
1944
+ * ```ts
1945
+ * const result = Ok<any>(1);
1946
+ *
1947
+ * try {
1948
+ * for (const n of result.$iter()) {}
1949
+ * } catch (err) {
1950
+ * // err is 'TypeError: number 1 is not iterable' in V8
1951
+ * }
1952
+ * ```
1953
+ */
1954
+ $iter<U>(this: Result<Iterable<U>, E>): IterableIterator<U, undefined, unknown>;
285
1955
  }
286
- type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;