retuple 1.0.0-next.1 → 1.0.0-next.11

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