retuple 1.0.0-next.5 → 1.0.0-next.8

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,10 +1,10 @@
1
+ export type Ok = typeof Ok;
2
+ export type Err = typeof Err;
1
3
  export type nonNullable = typeof nonNullable;
2
4
  export type truthy = typeof truthy;
3
5
  export type safe = typeof safe;
4
6
  export type safeAsync = typeof safeAsync;
5
7
  export type safePromise = typeof safePromise;
6
- export type Ok<T> = OkTuple<T> & Retuple<T, never>;
7
- export type Err<E> = ErrTuple<E> & Retuple<never, E>;
8
8
  export type Result<T, E> = (OkTuple<T> | ErrTuple<E>) & Retuple<T, E>;
9
9
  export { type ResultAsync };
10
10
  /**
@@ -28,13 +28,23 @@ export declare class RetupleUnwrapErrFailed<const T = unknown> extends Error {
28
28
  /**
29
29
  * ## Retuple Expect Failed
30
30
  *
31
- * An error which occurs when calling `$expect` on `Err`, and when the value
31
+ * An error which occurs when calling `$expect` on `Err`, when the value
32
32
  * contained in the `Err` is not an instance of `Error`.
33
33
  */
34
34
  export declare class RetupleExpectFailed<const E = unknown> extends Error {
35
35
  value: E;
36
36
  constructor(value: E);
37
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
+ }
38
48
  /**
39
49
  * ## Retuple Thrown Value Error
40
50
  *
@@ -49,456 +59,1897 @@ export declare class RetupleThrownValueError extends Error {
49
59
  /**
50
60
  * ## Retuple Invalid Result Error
51
61
  *
52
- * An error thrown when attempting to construct a `Result` from a native tuple,
53
- * when neither index 0 or 1 are null or undefined. In this case, it is impossible
54
- * to determine whether the result should be `Ok` or `Err`.
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`.
55
65
  */
56
66
  export declare class RetupleInvalidResultError extends Error {
57
67
  value: unknown[];
58
68
  constructor(value: unknown[]);
59
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
+ }
60
79
  /**
61
80
  * ## Result
62
81
  *
63
82
  * @TODO
64
83
  */
65
- export declare function Result<R extends [err: null | undefined, value: unknown] | [err: unknown, value: null | undefined]>(resultLike: R): (R extends [null | undefined, infer T] ? Ok<T> : R extends [infer E, null | undefined] ? Err<E> : never) extends Ok<infer T> | Err<infer E> ? Result<T, NonNullable<E>> : never;
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;
66
85
  export declare namespace Result {
67
86
  var Ok: typeof import(".").Ok;
68
87
  var Err: typeof import(".").Err;
69
- var nonNullable: typeof import(".").nonNullable;
70
- var truthy: typeof import(".").truthy;
71
- var safe: typeof import(".").safe;
72
- var safeAsync: typeof import(".").safeAsync;
73
- var safePromise: typeof import(".").safePromise;
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;
74
93
  }
75
94
  /**
76
- * ## Ok
95
+ * Create a new {@link Result} with the `Ok` variant. When called without
96
+ * arguments the `T` type is `void`.
77
97
  *
78
- * @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
+ * ```
79
112
  */
80
- export declare function Ok(): Ok<void>;
81
- export declare function Ok<const 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>;
82
115
  /**
83
- * ## Err
116
+ * Create a new {@link Result} with the `Err` variant. When called without
117
+ * arguments the `E` type is `void`.
84
118
  *
85
- * @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
+ * ```
86
133
  */
87
- export declare function Err(): Err<void>;
88
- export declare function Err<const 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>;
89
136
  /**
90
137
  * Construct a {@link Result} from a value. If the value is neither null or
91
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
+ * ```
92
180
  */
93
181
  export declare function nonNullable<const T>(value: T): Result<NonNullable<T>, true>;
94
- export declare function nonNullable<const T, const E>(value: T, error: () => E): Result<NonNullable<T>, E>;
182
+ export declare function nonNullable<const T, E>(value: T, error: () => E): Result<NonNullable<T>, E>;
95
183
  /**
96
184
  * Construct a {@link Result} from a value. If the value is truthy, the result
97
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
+ * ```
98
227
  */
99
228
  export declare function truthy<const T>(value: T): Result<Truthy<T>, true>;
100
- export declare function truthy<const T, const E>(value: T, error: () => E): Result<Truthy<T>, E>;
229
+ export declare function truthy<const T, E>(value: T, error: () => E): Result<Truthy<T>, E>;
101
230
  /**
102
231
  * Construct a {@link Result} from a synchronous function call. If the function
103
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);
104
294
  */
105
295
  export declare function safe<T>(f: () => Awaited<T>): Result<T, Error>;
106
296
  export declare function safe<T, E>(f: () => Awaited<T>, mapError: (err: unknown) => E): Result<T, E>;
107
297
  /**
108
298
  * Construct a {@link ResultAsync} from a function call. If the function returns
109
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);
110
361
  */
111
362
  export declare function safeAsync<T>(f: () => T | PromiseLike<T>): ResultAsync<T, Error>;
112
363
  export declare function safeAsync<T, E>(f: () => T | PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
113
364
  /**
114
365
  * Construct a {@link Result} from a promise. If the promise resolves, the
115
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);
116
424
  */
117
425
  export declare function safePromise<T>(promise: PromiseLike<T>): ResultAsync<T, Error>;
118
426
  export declare function safePromise<T, E>(promise: PromiseLike<T>, mapError: (err: unknown) => E): ResultAsync<T, E>;
119
427
  /**
120
- * ## ResultAsync
428
+ * ## RetupleArray
121
429
  *
122
- * @TODO
430
+ * Using built-in array methods on a `Result` is probably a mistake. This class
431
+ * makes the built-in methods throw `RetupleArrayMethodUnavailableError`.
123
432
  */
124
- declare class ResultAsync<T, E> {
125
- #private;
126
- constructor(inner: PromiseLike<Result<T, E>>);
127
- 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>;
433
+ declare class RetupleArray<T> extends Array<T> {
128
434
  /**
129
- * @TODO
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
130
441
  */
131
- $toNativeTuple(this: ResultAsync<T, E>): Promise<OkTuple<T> | ErrTuple<E>>;
442
+ at(): never;
132
443
  /**
133
- * @TODO
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
134
450
  */
135
- $value(this: ResultAsync<T, E>): Promise<T | E>;
451
+ concat(): never;
136
452
  /**
137
- * @TODO
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
138
459
  */
139
- $ok(this: ResultAsync<T, E>): Promise<T | undefined>;
460
+ copyWithin(): never;
140
461
  /**
141
- * @TODO
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
142
468
  */
143
- $expect(this: ResultAsync<T, Error>): Promise<T>;
469
+ entries(): never;
144
470
  /**
145
- * @TODO
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
146
477
  */
147
- $unwrap(this: ResultAsync<T, E>, msg?: string): Promise<T>;
478
+ every(): this is never[];
148
479
  /**
149
- * @TODO
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
150
486
  */
151
- $unwrapErr(this: ResultAsync<T, E>, msg?: string): Promise<E>;
487
+ fill(): never;
152
488
  /**
153
- * @TODO
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
154
495
  */
155
- $unwrapOr<U>(this: ResultAsync<T, E>, def: U): Promise<T | U>;
496
+ filter(): never;
156
497
  /**
157
- * @TODO
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
158
504
  */
159
- $unwrapOrElse<U>(this: ResultAsync<T, E>, f: () => U): Promise<T | U>;
505
+ find(): never;
160
506
  /**
161
- * @TODO
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
162
513
  */
163
- $map<U>(this: ResultAsync<T, E>, f: (val: T) => U): ResultAsync<U, E>;
514
+ findIndex(): never;
164
515
  /**
165
- * @TODO
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
166
522
  */
167
- $mapErr<F>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
523
+ findLast(): never;
168
524
  /**
169
- * @TODO
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
170
531
  */
171
- $mapOr<U, V>(this: ResultAsync<T, E>, def: U, f: (val: T) => V): ResultAsync<U | V, E>;
532
+ findLastIndex(): never;
172
533
  /**
173
- * @TODO
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
174
540
  */
175
- $mapOrElse<U, V>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, E>;
541
+ flat(): never;
176
542
  /**
177
- * ## $assertOr
543
+ * ## Method not available
178
544
  *
179
- * {@link Retuple.$assertOr|$assertOr}
545
+ * Built-in array methods not available on `Result` types, convert the result
546
+ * to a tuple using `$tuple()` first.
547
+ *
548
+ * @deprecated
180
549
  */
181
- $assertOr<U, F>(this: ResultAsync<T, E>, def: Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<Truthy<T>, E | F>;
182
- $assertOr<U, F, A extends T>(this: ResultAsync<T, E>, def: Result<U, F> | PromiseLike<Result<U, F>>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
183
- $assertOr<U, F>(this: ResultAsync<T, E>, def: Result<U, F> | PromiseLike<Result<U, F>>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
184
- $assertOrElse<U, F>(this: ResultAsync<T, E>, def: () => Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<Truthy<T>, E | F>;
185
- $assertOrElse<U, F, A extends T>(this: ResultAsync<T, E>, def: () => Result<U, F> | PromiseLike<Result<U, F>>, predicate: (val: T) => val is A): ResultAsync<U | A, E | F>;
186
- $assertOrElse<U, F>(this: ResultAsync<T, E>, def: () => Result<U, F> | PromiseLike<Result<U, F>>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
550
+ flatMap(): never;
187
551
  /**
188
- * @TODO
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
189
558
  */
190
- $or<U, F>(this: ResultAsync<T, E>, or: Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<T | U, E | F>;
559
+ forEach(): never;
191
560
  /**
192
- * @TODO
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
193
567
  */
194
- $orElse<U, F>(this: ResultAsync<T, E>, f: (err: E) => Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<T | U, E | F>;
568
+ includes(): never;
195
569
  /**
196
- * @TODO
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
197
576
  */
198
- $orSafe<U>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>): ResultAsync<T | U, E | Error>;
199
- $orSafe<U, F>(this: ResultAsync<T, E>, f: (err: E) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, E | F>;
577
+ indexOf(): never;
200
578
  /**
201
- * @TODO
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
202
585
  */
203
- $and<U, F>(this: ResultAsync<T, E>, and: Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<U, E | F>;
586
+ join(): never;
204
587
  /**
205
- * @TODO
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
206
594
  */
207
- $andThen<U, F>(this: ResultAsync<T, E>, f: (val: T) => Result<U, F> | PromiseLike<Result<U, F>>): ResultAsync<U, E | F>;
595
+ keys(): never;
208
596
  /**
209
- * @TODO
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
210
603
  */
211
- $andThrough<F>(this: ResultAsync<T, E>, f: (val: T) => Result<any, F> | PromiseLike<Result<any, F>>): ResultAsync<T, E | F>;
604
+ lastIndexOf(): never;
212
605
  /**
213
- * @TODO
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
214
612
  */
215
- $andSafe<U>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<T | U, E | Error>;
216
- $andSafe<U, F>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<T | U, E | F>;
613
+ map(): never;
217
614
  /**
218
- * @TODO
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
219
621
  */
220
- $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
622
+ pop(): never;
221
623
  /**
222
- * @TODO
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
223
630
  */
224
- $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
631
+ push(): never;
225
632
  /**
226
- * @TODO
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
227
639
  */
228
- $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
640
+ reduce(): never;
229
641
  /**
230
- * @TODO
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
231
648
  */
232
- $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
233
- }
234
- type OkTuple<T> = [err: undefined, value: T];
235
- type ErrTuple<E> = [err: E, value: undefined];
236
- type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
237
- /**
238
- * @TODO - Result.all / Result.any
239
- */
240
- interface Retuple<T, E> {
649
+ reduceRight(): never;
241
650
  /**
242
- * @TODO
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
243
657
  */
244
- $toNativeTuple(this: Result<T, E>): OkTuple<T> | ErrTuple<E>;
658
+ reverse(): never;
245
659
  /**
246
- * @TODO
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
247
666
  */
248
- $value(this: Result<T, E>): T | E;
667
+ shift(): never;
249
668
  /**
250
- * @TODO
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
251
675
  */
252
- $ok(this: Result<T, E>): T | undefined;
676
+ slice(): never;
253
677
  /**
254
- * @TODO
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
255
684
  */
256
- $isOk(this: Result<T, E>): this is Ok<T>;
685
+ some(): never;
257
686
  /**
258
- * @TODO
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
259
693
  */
260
- $isOkAnd<U extends T>(this: Result<T, E>, predicate: (val: T) => val is U): this is Ok<U>;
261
- $isOkAnd(this: Result<T, E>, predicate: (val: T) => unknown): this is Ok<T>;
694
+ sort(): never;
262
695
  /**
263
- * @TODO
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
264
702
  */
265
- $isErr(this: Result<T, E>): this is Err<E>;
703
+ splice(): never;
266
704
  /**
267
- * @TODO
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
268
711
  */
269
- $isErrAnd<F extends E>(this: Result<T, E>, prediacte: (val: E) => val is F): this is Err<F>;
270
- $isErrAnd(this: Result<T, E>, predicate: (val: E) => unknown): this is Err<E>;
712
+ toString(): never;
271
713
  /**
272
- * @TODO
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
273
720
  */
274
- $expect(this: Result<T, Error>): T;
721
+ toLocaleString(): never;
275
722
  /**
276
- * @TODO
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
277
729
  */
278
- $unwrap(this: Result<T, E>, msg?: string): T;
730
+ toReversed(): never;
279
731
  /**
280
- * @TODO
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
281
738
  */
282
- $unwrapErr(this: Result<T, E>, msg?: string): E;
739
+ toSorted(): never;
283
740
  /**
284
- * @TODO
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
285
747
  */
286
- $unwrapOr<const U>(this: Result<T, E>, def: U): T | U;
748
+ toSpliced(): never;
287
749
  /**
288
- * @TODO
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
289
756
  */
290
- $unwrapOrElse<U>(this: Result<T, E>, f: () => U): T | U;
757
+ unshift(): never;
291
758
  /**
292
- * @TODO
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
293
765
  */
294
- $map<U>(this: Result<T, E>, f: (value: T) => U): Result<U, E>;
766
+ values(): never;
295
767
  /**
296
- * @TODO
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
297
774
  */
298
- $mapErr<F>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
775
+ with(): never;
776
+ }
777
+ /**
778
+ * ## ResultAsync
779
+ *
780
+ * @TODO
781
+ */
782
+ declare class ResultAsync<T, E> {
783
+ #private;
784
+ constructor(inner: PromiseLike<Result<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>;
299
786
  /**
300
- * @TODO
787
+ * The same as {@link Retuple.$expect|$expect}, except it returns a `Promise`.
301
788
  */
302
- $mapOr<U, V = U>(this: Result<T, E>, def: U, f: (val: T) => V): Result<U | V, E>;
789
+ $expect(this: ResultAsync<T, Error>): Promise<T>;
303
790
  /**
304
- * @TODO
791
+ * The same as {@link Retuple.$unwrap|$unwrap}, except it returns a `Promise`.
305
792
  */
306
- $mapOrElse<U, V = U>(this: Result<T, E>, def: (err: E) => U, f: (val: T) => V): Result<U | V, E>;
793
+ $unwrap(this: ResultAsync<T, E>, msg?: string): Promise<T>;
307
794
  /**
308
- * @TODO
795
+ * The same as {@link Retuple.$unwrapErr|$unwrapErr}, except it returns
796
+ * a `Promise`.
309
797
  */
310
- $or<U, F>(this: Result<T, E>, or: Result<U, F>): Result<T | U, E | F>;
798
+ $unwrapErr(this: ResultAsync<T, E>, msg?: string): Promise<E>;
311
799
  /**
312
- * @TODO
800
+ * The same as {@link Retuple.$unwrapOr|$unwrapOr}, except it returns
801
+ * a `Promise`.
313
802
  */
314
- $orElse<U, F>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, E | F>;
803
+ $unwrapOr<const U = T>(this: ResultAsync<T, E>, def: U): Promise<T | U>;
315
804
  /**
316
- * @TODO
805
+ * The same as {@link Retuple.$unwrapOrElse|$unwrapOrElse}, except it returns
806
+ * a `Promise`.
317
807
  */
318
- $orSafe<U>(this: Result<T, E>, f: (err: E) => U): Result<T | U, E | Error>;
319
- $orSafe<U, F>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
808
+ $unwrapOrElse<U = T>(this: ResultAsync<T, E>, f: () => U): Promise<T | U>;
320
809
  /**
321
- * @TODO
810
+ * The same as {@link Retuple.$map|$map}, except it returns `ResultAsync`.
322
811
  */
323
- $and<U, F>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
812
+ $map<U>(this: ResultAsync<T, E>, f: (val: T) => U): ResultAsync<U, E>;
324
813
  /**
325
- * @TODO
814
+ * The same as {@link Retuple.$mapErr|$mapErr}, except it returns
815
+ * `ResultAsync`.
326
816
  */
327
- $andThen<U, F>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
817
+ $mapErr<F = E>(this: ResultAsync<T, E>, f: (err: E) => F): ResultAsync<T, F>;
328
818
  /**
329
- * @TODO
819
+ * The same as {@link Retuple.$mapOr|$mapOr}, except it returns `ResultAsync`.
330
820
  */
331
- $andThrough<F>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
821
+ $mapOr<U, V = U>(this: ResultAsync<T, E>, def: U, f: (val: T) => V): ResultAsync<U | V, never>;
332
822
  /**
333
- * @TODO
823
+ * The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
824
+ * `ResultAsync`.
334
825
  */
335
- $andSafe<U>(this: Result<T, E>, f: (val: T) => U): Result<T | U, E | Error>;
336
- $andSafe<U, F>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<T | U, E | F>;
826
+ $mapOrElse<U, V = U>(this: ResultAsync<T, E>, def: (err: E) => U, f: (val: T) => V): ResultAsync<U | V, never>;
337
827
  /**
338
- * @TODO
828
+ * The same as {@link Retuple.$assertOr|$assertOr}, except it:
829
+ *
830
+ * - can also accept a `PromiseLike` default value;
831
+ * - returns `ResultAsync`.
339
832
  */
340
- $assertOr<U, F>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T>, E | F>;
341
- $assertOr<U, F, A extends T>(this: Result<T, E>, def: Result<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
342
- $assertOr<U, F>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
833
+ $assertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>): ResultAsync<Truthy<T>, E | F>;
834
+ $assertOr<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
+ $assertOr<U = T, F = E>(this: ResultAsync<T, E>, def: RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
343
836
  /**
344
- * @TODO
837
+ * The same as {@link Retuple.$assertOrElse|$assertOrElse}, except it:
838
+ *
839
+ * - can also accept an `async` default function;
840
+ * - returns `ResultAsync`.
345
841
  */
346
- $assertOrElse<U, F>(this: Result<T, E>, def: () => Result<U, F>): Result<Truthy<T>, E | F>;
347
- $assertOrElse<U, F, A extends T>(this: Result<T, E>, def: () => Result<U, F>, predicate: (val: T) => val is A): Result<U | A, E | F>;
348
- $assertOrElse<U, F>(this: Result<T, E>, def: () => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
842
+ $assertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>): ResultAsync<Truthy<T>, E | F>;
843
+ $assertOrElse<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
+ $assertOrElse<U = T, F = E>(this: ResultAsync<T, E>, def: (val: T) => RetupleAwaitable<U, F>, condition: (val: T) => unknown): ResultAsync<T | U, E | F>;
349
845
  /**
350
- * @TODO
846
+ * The same as {@link Retuple.$or|$or}, except it:
847
+ *
848
+ * - can also accept a `PromiseLike` or value;
849
+ * - returns `ResultAsync`.
351
850
  */
352
- $peek(this: Result<T, E>, f: (res: Result<T, E>) => void): Result<T, E>;
851
+ $or<U = T, F = E>(this: ResultAsync<T, E>, or: Retuple<U, F> | PromiseLike<Retuple<U, F>>): ResultAsync<T | U, F>;
353
852
  /**
354
- * @TODO
853
+ * The same as {@link Retuple.$orElse|$orElse}, except it:
854
+ *
855
+ * - can also accept an `async` or function;
856
+ * - returns `ResultAsync`.
355
857
  */
356
- $tap(this: Result<T, E>, f: (val: T) => any): Result<T, E>;
858
+ $orElse<U = T, F = E>(this: ResultAsync<T, E>, f: (err: E) => RetupleAwaitable<U, F>): ResultAsync<T | U, F>;
357
859
  /**
358
- * @TODO
860
+ * The same as {@link Retuple.$orSafe|$orSafe}, except it:
861
+ *
862
+ * - can also accept an `async` safe function;
863
+ * - returns `ResultAsync`.
359
864
  */
360
- $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
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>;
361
867
  /**
362
868
  * @TODO
363
869
  */
364
- $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, 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>;
365
872
  /**
366
- * @TODO
873
+ * The same as {@link Retuple.$and|$and}, except it:
874
+ *
875
+ * - can also accept a `PromiseLike` and value;
876
+ * - returns `ResultAsync`.
367
877
  */
368
- $async(this: Result<T, E>): ResultAsync<T, E>;
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>;
369
901
  /**
370
902
  * @TODO
371
903
  */
372
- $promise(this: Result<T, E>): Promise<Result<T, E>>;
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>;
373
906
  /**
374
- * Mark standard array methods as deprecated, to assist with type hinting.
907
+ * The same as {@link Retuple.$peek|$peek}, except it:
908
+ *
909
+ * - awaits the peek function;
910
+ * - returns `ResultAsync`.
375
911
  */
912
+ $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
376
913
  /**
377
- * @deprecated
914
+ * The same as {@link Retuple.$tap|$tap}, except it:
915
+ *
916
+ * - awaits the tap function;
917
+ * - returns `ResultAsync`.
378
918
  */
379
- at(...args: unknown[]): unknown;
919
+ $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
380
920
  /**
381
- * @deprecated
921
+ * The same as {@link Retuple.$tapErr|$tapErr}, except it:
922
+ *
923
+ * - awaits the tap error function;
924
+ * - returns `ResultAsync`.
382
925
  */
383
- concat(...args: unknown[]): unknown;
926
+ $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
384
927
  /**
385
- * @deprecated
928
+ * The same as {@link Retuple.$promise|$promise}.
386
929
  */
387
- copyWithin(...args: unknown[]): unknown;
930
+ $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
388
931
  /**
389
- * @deprecated
932
+ * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
390
933
  */
391
- entries(...args: unknown[]): unknown;
934
+ $tuple(this: ResultAsync<T, E>): Promise<[err: E | undefined, value: T | undefined]>;
392
935
  /**
393
- * @deprecated
936
+ * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
394
937
  */
395
- every(...args: unknown[]): unknown;
938
+ $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
939
+ }
940
+ type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
941
+ type OkTuple<T> = [err: undefined, value: T];
942
+ type ErrTuple<E> = [err: E, value: undefined];
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> {
396
947
  /**
397
- * @deprecated
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
+ * ```
398
973
  */
399
- fill(...args: unknown[]): unknown;
974
+ $isOk(this: Result<T, E>): this is Result<T, never>;
400
975
  /**
401
- * @deprecated
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
+ * ```
402
1009
  */
403
- filter(...args: unknown[]): unknown;
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>;
404
1012
  /**
405
- * @deprecated
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
+ * ```
406
1038
  */
407
- find(...args: unknown[]): unknown;
1039
+ $isErr(this: Result<T, E>): this is Result<never, E>;
408
1040
  /**
409
- * @deprecated
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
+ * ```
410
1074
  */
411
- findIndex(...args: unknown[]): unknown;
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>;
412
1077
  /**
413
- * @deprecated
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
+ * ```
414
1119
  */
415
- flat(...args: unknown[]): unknown;
1120
+ $expect(this: Result<T, Error>): T;
416
1121
  /**
417
- * @deprecated
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
+ * ```
418
1163
  */
419
- flatMap(...args: unknown[]): unknown;
1164
+ $unwrap(this: Result<T, E>, msg?: string): T;
420
1165
  /**
421
- * @deprecated
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
+ * ```
422
1205
  */
423
- forEach(...args: unknown[]): unknown;
1206
+ $unwrapErr(this: Result<T, E>, msg?: string): E;
424
1207
  /**
425
- * @deprecated
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
+ * ```
426
1225
  */
427
- includes(...args: unknown[]): unknown;
1226
+ $unwrapOr<const U = T>(this: Result<T, E>, def: U): T | U;
428
1227
  /**
429
- * @deprecated
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
+ * ```
430
1245
  */
431
- indexOf(...args: unknown[]): unknown;
1246
+ $unwrapOrElse<U = T>(this: Result<T, E>, f: () => U): T | U;
432
1247
  /**
433
- * @deprecated
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.$assertOr(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.$assertOr(
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.$assertOr(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.$assertOr(
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.$assertOr(
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
+ * ```
434
1321
  */
435
- join(...args: unknown[]): unknown;
1322
+ $assertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T> | U, E | F>;
1323
+ $assertOr<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
+ $assertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
436
1325
  /**
437
- * @deprecated
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.$assertOrElse(
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.$assertOrElse(
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.$assertOrElse(
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.$assertOrElse(
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.$assertOrElse(
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
+ * ```
438
1404
  */
439
- keys(...args: unknown[]): unknown;
1405
+ $assertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>): Result<Truthy<T> | U, E | F>;
1406
+ $assertOrElse<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
+ $assertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
440
1408
  /**
441
- * @deprecated
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
+ * ```
442
1433
  */
443
- lastIndexOf(...args: unknown[]): unknown;
1434
+ $map<U>(this: Result<T, E>, f: (value: T) => U): Result<U, E>;
444
1435
  /**
445
- * @deprecated
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
+ * ```
1460
+ */
1461
+ $mapErr<F = E>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
1462
+ /**
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
+ * ```
446
1487
  */
447
- map(...args: unknown[]): unknown;
1488
+ $mapOr<U, V = U>(this: Result<T, E>, def: U, f: (val: T) => V): Result<U | V, E>;
448
1489
  /**
449
- * @deprecated
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
+ * ```
450
1521
  */
451
- pop(...args: unknown[]): any;
1522
+ $mapOrElse<U, V = U>(this: Result<T, E>, def: (err: E) => U, f: (val: T) => V): Result<U | V, E>;
452
1523
  /**
453
- * @deprecated
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
+ * ```
454
1557
  */
455
- push(...args: unknown[]): unknown;
1558
+ $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, F>;
456
1559
  /**
457
- * @deprecated
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
+ * ```
1593
+ */
1594
+ $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, F>;
1595
+ /**
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(...))`.
458
1603
  */
459
- reduce(...args: unknown[]): unknown;
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>;
460
1606
  /**
461
- * @deprecated
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
+ * ```
462
1640
  */
463
- reduceRight(...args: unknown[]): unknown;
1641
+ $and<U = T, F = E>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
464
1642
  /**
465
- * @deprecated
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
+ * ```
466
1676
  */
467
- reverse(...args: unknown[]): unknown;
1677
+ $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
468
1678
  /**
469
- * @deprecated
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
+ * ```
1716
+ */
1717
+ $andThrough<F = E>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
1718
+ /**
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(...))`.
470
1726
  */
471
- shift(...args: unknown[]): unknown;
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>;
472
1729
  /**
473
- * @deprecated
1730
+ * @TODO
474
1731
  */
475
- slice(...args: unknown[]): unknown;
476
1732
  /**
477
- * @deprecated
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
+ * ```
478
1768
  */
479
- some(...args: unknown[]): unknown;
1769
+ $peek(this: Result<T, E>, f: (res: Result<T, E>) => void): Result<T, E>;
480
1770
  /**
481
- * @deprecated
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
+ * ```
482
1797
  */
483
- sort(...args: unknown[]): unknown;
1798
+ $tap(this: Result<T, E>, f: (val: T) => any): Result<T, E>;
484
1799
  /**
485
- * @deprecated
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
+ * ```
486
1826
  */
487
- splice(...args: unknown[]): unknown;
1827
+ $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
488
1828
  /**
489
- * @deprecated
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
+ * ```
490
1857
  */
491
- toString(...args: unknown[]): unknown;
1858
+ $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
492
1859
  /**
493
- * @deprecated
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
+ * ```
494
1874
  */
495
- toLocaleString(...args: unknown[]): unknown;
1875
+ $async(this: Result<T, E>): ResultAsync<T, E>;
496
1876
  /**
497
- * @deprecated
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
+ * ```
498
1892
  */
499
- unshift(...args: unknown[]): unknown;
1893
+ $promise(this: Result<T, E>): Promise<Result<T, E>>;
500
1894
  /**
501
- * @deprecated
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
+ * ```
502
1910
  */
503
- values(...args: unknown[]): unknown;
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>;
504
1955
  }