retuple 1.0.0-next.5 → 1.0.0-next.7

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,1884 @@ 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
- * @TODO
868
+ * The same as {@link Retuple.$and|$and}, except it:
869
+ *
870
+ * - can also accept a `PromiseLike` and value;
871
+ * - returns `ResultAsync`.
363
872
  */
364
- $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
873
+ $and<U = T, F = E>(this: ResultAsync<T, E>, and: RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
365
874
  /**
366
- * @TODO
875
+ * The same as {@link Retuple.$andThen|$andThen}, except it:
876
+ *
877
+ * - can also accept an `async` and function;
878
+ * - returns `ResultAsync`.
367
879
  */
368
- $async(this: Result<T, E>): ResultAsync<T, E>;
880
+ $andThen<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<U, F>): ResultAsync<U, E | F>;
369
881
  /**
370
- * @TODO
882
+ * The same as {@link Retuple.$andThrough|$andThrough}, except it:
883
+ *
884
+ * - can also accept an `async` through function;
885
+ * - returns `ResultAsync`.
371
886
  */
372
- $promise(this: Result<T, E>): Promise<Result<T, E>>;
887
+ $andThrough<F = E>(this: ResultAsync<T, E>, f: (val: T) => RetupleAwaitable<any, F>): ResultAsync<T, E | F>;
373
888
  /**
374
- * Mark standard array methods as deprecated, to assist with type hinting.
889
+ * The same as {@link Retuple.$andSafe|$andSafe}, except it:
890
+ *
891
+ * - can also accept an `async` safe function;
892
+ * - returns `ResultAsync`.
375
893
  */
894
+ $andSafe<U = T>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>): ResultAsync<U, E | Error>;
895
+ $andSafe<U = T, F = E>(this: ResultAsync<T, E>, f: (val: T) => U | PromiseLike<U>, mapError: (err: unknown) => F): ResultAsync<U, E | F>;
376
896
  /**
377
- * @deprecated
897
+ * The same as {@link Retuple.$peek|$peek}, except it:
898
+ *
899
+ * - awaits the peek function;
900
+ * - returns `ResultAsync`.
378
901
  */
379
- at(...args: unknown[]): unknown;
902
+ $peek(this: ResultAsync<T, E>, f: (res: Result<T, E>) => any): ResultAsync<T, E>;
380
903
  /**
381
- * @deprecated
904
+ * The same as {@link Retuple.$tap|$tap}, except it:
905
+ *
906
+ * - awaits the tap function;
907
+ * - returns `ResultAsync`.
382
908
  */
383
- concat(...args: unknown[]): unknown;
909
+ $tap(this: ResultAsync<T, E>, f: (val: T) => any): ResultAsync<T, E>;
384
910
  /**
385
- * @deprecated
911
+ * The same as {@link Retuple.$tapErr|$tapErr}, except it:
912
+ *
913
+ * - awaits the tap error function;
914
+ * - returns `ResultAsync`.
386
915
  */
387
- copyWithin(...args: unknown[]): unknown;
916
+ $tapErr(this: ResultAsync<T, E>, f: (err: E) => any): ResultAsync<T, E>;
388
917
  /**
389
- * @deprecated
918
+ * The same as {@link Retuple.$promise|$promise}.
390
919
  */
391
- entries(...args: unknown[]): unknown;
920
+ $promise(this: ResultAsync<T, E>): Promise<Result<T, E>>;
392
921
  /**
393
- * @deprecated
922
+ * The same as {@link Retuple.$tuple|$tuple}, except it returns a `Promise`.
394
923
  */
395
- every(...args: unknown[]): unknown;
924
+ $tuple(this: ResultAsync<T, E>): Promise<[err: E | undefined, value: T | undefined]>;
396
925
  /**
397
- * @deprecated
926
+ * The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
398
927
  */
399
- fill(...args: unknown[]): unknown;
928
+ $iter<U>(this: ResultAsync<Iterable<U>, E>): Promise<IterableIterator<U, undefined, unknown>>;
929
+ }
930
+ type Truthy<T> = Exclude<T, false | null | undefined | 0 | 0n | "">;
931
+ type OkTuple<T> = [err: undefined, value: T];
932
+ type ErrTuple<E> = [err: E, value: undefined];
933
+ type ThisOk<T> = OkTuple<T> & Retuple<T, never>;
934
+ type ThisErr<E> = ErrTuple<E> & Retuple<never, E>;
935
+ type RetupleAwaitable<T, E> = Retuple<T, E> | PromiseLike<Retuple<T, E>>;
936
+ interface Retuple<T, E> extends RetupleArray<T | E | undefined> {
400
937
  /**
401
- * @deprecated
938
+ * Returns true when this result is `Ok`. Acts as a type guard.
939
+ *
940
+ * @example
941
+ *
942
+ * ```ts
943
+ * const result = Ok("test");
944
+ * assert.equal(result.$isOk(), true);
945
+ * ```
946
+ *
947
+ * @example
948
+ *
949
+ * ```ts
950
+ * const result = Err("test");
951
+ * assert.equal(result.$isOk(), false);
952
+ * ```
953
+ *
954
+ * @example
955
+ *
956
+ * ```ts
957
+ * const result: Result<string, Error> = someResultFn();
958
+ *
959
+ * if (result.$isOk()) {
960
+ * result satisfies Result<string, never>;
961
+ * }
962
+ * ```
402
963
  */
403
- filter(...args: unknown[]): unknown;
964
+ $isOk(this: Result<T, E>): this is Result<T, never>;
404
965
  /**
405
- * @deprecated
966
+ * Returns true when this result is `Ok`, and when the predicate/condition
967
+ * function returns true. Acts as a type guard.
968
+ *
969
+ * @example
970
+ *
971
+ * ```ts
972
+ * const result = Ok("test");
973
+ * assert.equal(result.$isOkAnd((val) => val === "test"), true);
974
+ * ```
975
+ *
976
+ * @example
977
+ *
978
+ * ```ts
979
+ * const result = Ok<string>("test");
980
+ * assert.equal(result.$isOkAnd((val) => val !== "test"), false);
981
+ * ```
982
+ *
983
+ * @example
984
+ *
985
+ * ```ts
986
+ * const result = Err("test");
987
+ * assert.equal(result.$isOkAnd((err) => err === "test"), false);
988
+ * ```
989
+ *
990
+ * @example
991
+ *
992
+ * ```ts
993
+ * const result: Result<string | number, Error> = someResultFn();
994
+ *
995
+ * if (result.$isOkAnd((val): val is number => typeof val === "number")) {
996
+ * result satisfies Result<number, never>;
997
+ * }
998
+ * ```
406
999
  */
407
- find(...args: unknown[]): unknown;
1000
+ $isOkAnd<U extends T = T>(this: Result<T, E>, predicate: (val: T) => val is U): this is Result<U, never>;
1001
+ $isOkAnd(this: Result<T, E>, predicate: (val: T) => unknown): this is Result<T, never>;
408
1002
  /**
409
- * @deprecated
1003
+ * Returns true when this result is `Err`. Acts as a type guard.
1004
+ *
1005
+ * @example
1006
+ *
1007
+ * ```ts
1008
+ * const result = Err("test");
1009
+ * assert.equal(result.$isErr(), true);
1010
+ * ```
1011
+ *
1012
+ * @example
1013
+ *
1014
+ * ```ts
1015
+ * const result = Ok("test");
1016
+ * assert.equal(result.$isErr(), false);
1017
+ * ```
1018
+ *
1019
+ * @example
1020
+ *
1021
+ * ```ts
1022
+ * const result: Result<string, Error> = someResultFn();
1023
+ *
1024
+ * if (result.$isErr()) {
1025
+ * result satisfies Result<never, Error>;
1026
+ * }
1027
+ * ```
410
1028
  */
411
- findIndex(...args: unknown[]): unknown;
1029
+ $isErr(this: Result<T, E>): this is Result<never, E>;
412
1030
  /**
413
- * @deprecated
1031
+ * Returns true when this result is `Err`, and when the predicate/condition
1032
+ * function returns true. Acts as a type guard.
1033
+ *
1034
+ * @example
1035
+ *
1036
+ * ```ts
1037
+ * const result = Err("test");
1038
+ * assert.equal(result.$isErrAnd((err) => err === "test"), true);
1039
+ * ```
1040
+ *
1041
+ * @example
1042
+ *
1043
+ * ```ts
1044
+ * const result = Err<string>("test");
1045
+ * assert.equal(result.$isErrAnd((err) => err !== "test"), false);
1046
+ * ```
1047
+ *
1048
+ * @example
1049
+ *
1050
+ * ```ts
1051
+ * const result = Ok("test");
1052
+ * assert.equal(result.$isErrAnd((val) => val === "test"), false);
1053
+ * ```
1054
+ *
1055
+ * @example
1056
+ *
1057
+ * ```ts
1058
+ * const result: Result<string, Error | number> = someResultFn();
1059
+ *
1060
+ * if (result.$isErrAnd((err): err is number => typeof err === "number")) {
1061
+ * result satisfies Result<never, number>;
1062
+ * }
1063
+ * ```
414
1064
  */
415
- flat(...args: unknown[]): unknown;
1065
+ $isErrAnd<F extends E = E>(this: Result<T, E>, prediacte: (val: E) => val is F): this is Result<never, F>;
1066
+ $isErrAnd(this: Result<T, E>, predicate: (val: E) => unknown): this is Result<never, E>;
416
1067
  /**
417
- * @deprecated
1068
+ * Returns the ok value when this result is `Ok`.
1069
+ *
1070
+ * Otherwise, the error value is thrown.
1071
+ *
1072
+ * This method should only be called when the `E` type extends `Error`. This
1073
+ * is enforced with a type constraint. If the error value is not an instance
1074
+ * of Error, `RetupleExpectFailed` is thrown. Use
1075
+ * {@link Retuple.$unwrap|$unwrap} When the `E` type does not extend Error.
1076
+ *
1077
+ * @example
1078
+ *
1079
+ * ```ts
1080
+ * const result = Ok("test");
1081
+ * assert.equal(result.$expect(), "test");
1082
+ * ```
1083
+ *
1084
+ * @example
1085
+ *
1086
+ * ```ts
1087
+ * const error = new Error("test");
1088
+ * const result = Err(error);
1089
+ *
1090
+ * try {
1091
+ * const value = result.$expect(); // throws
1092
+ * } catch (e) {
1093
+ * assert.equal(e, error);
1094
+ * }
1095
+ * ```
1096
+ *
1097
+ * @example
1098
+ *
1099
+ * ```ts
1100
+ * const result = Err("test");
1101
+ *
1102
+ * try {
1103
+ * // This is a type error - the E type does not extend Error
1104
+ * const value = result.$expect(); // throws
1105
+ * } catch (e) {
1106
+ * assert(e instanceof RetupleExpectFailed && e.value === "test");
1107
+ * }
1108
+ * ```
418
1109
  */
419
- flatMap(...args: unknown[]): unknown;
1110
+ $expect(this: Result<T, Error>): T;
420
1111
  /**
421
- * @deprecated
1112
+ * Returns the ok value when this result is `Ok`.
1113
+ *
1114
+ * Otherwise, `RetupleUnwrapFailed` is thrown. A custom error message can be
1115
+ * provided.
1116
+ *
1117
+ * @example
1118
+ *
1119
+ * ```ts
1120
+ * const result = Ok("test");
1121
+ * assert.equal(result.$unwrap(), "test");
1122
+ * ```
1123
+ *
1124
+ * @example
1125
+ *
1126
+ * ```ts
1127
+ * const result = Err("test");
1128
+ *
1129
+ * try {
1130
+ * const value = result.$unwrap(); // throws
1131
+ * } catch (e) {
1132
+ * assert(e instanceof RetupleUnwrapFailed && e.value === "test");
1133
+ * }
1134
+ * ```
1135
+ *
1136
+ * @example
1137
+ *
1138
+ * ```ts
1139
+ * const error = new Error("test");
1140
+ * const result = Err(error);
1141
+ *
1142
+ * try {
1143
+ * const value = result.$unwrap("error-message"); // throws
1144
+ * } catch (e) {
1145
+ * assert(
1146
+ * e instanceof RetupleUnwrapFailed &&
1147
+ * e.message === "error-message" &&
1148
+ * e.value === error &&
1149
+ * e.cause === error, // set when error value was an instance of `Error`
1150
+ * );
1151
+ * }
1152
+ * ```
422
1153
  */
423
- forEach(...args: unknown[]): unknown;
1154
+ $unwrap(this: Result<T, E>, msg?: string): T;
424
1155
  /**
425
- * @deprecated
1156
+ * Returns the error value when this result is `Err`.
1157
+ *
1158
+ * Otherwise, `RetupleUnwrapErrFailed` is thrown. A custom error message can
1159
+ * be provided.
1160
+ *
1161
+ * @example
1162
+ *
1163
+ * ```ts
1164
+ * const result = Err("test");
1165
+ * assert.equal(result.$unwrapErr(), "test");
1166
+ * ```
1167
+ *
1168
+ * @example
1169
+ *
1170
+ * ```ts
1171
+ * const result = Ok("test");
1172
+ *
1173
+ * try {
1174
+ * const value = result.$unwrapErr(); // throws
1175
+ * } catch (e) {
1176
+ * assert(e instanceof RetupleUnwrapErrFailed && e.value === "test");
1177
+ * }
1178
+ * ```
1179
+ *
1180
+ * @example
1181
+ *
1182
+ * ```ts
1183
+ * const result = Ok("test");
1184
+ *
1185
+ * try {
1186
+ * const value = result.$unwrapErr("error-message"); // throws
1187
+ * } catch (e) {
1188
+ * assert(
1189
+ * e instanceof RetupleUnwrapErrFailed &&
1190
+ * e.message === "error-message" &&
1191
+ * e.value === "test",
1192
+ * );
1193
+ * }
1194
+ * ```
426
1195
  */
427
- includes(...args: unknown[]): unknown;
1196
+ $unwrapErr(this: Result<T, E>, msg?: string): E;
428
1197
  /**
429
- * @deprecated
1198
+ * Returns the ok value when this result is `Ok`.
1199
+ *
1200
+ * Otherwise, returns the default value.
1201
+ *
1202
+ * @example
1203
+ *
1204
+ * ```ts
1205
+ * const result = Ok("test");
1206
+ * assert.equal(result.$unwrapOr("default"), "test");
1207
+ * ```
1208
+ *
1209
+ * @example
1210
+ *
1211
+ * ```ts
1212
+ * const result = Err("test");
1213
+ * assert.equal(result.$unwrapOr("default"), "default");
1214
+ * ```
430
1215
  */
431
- indexOf(...args: unknown[]): unknown;
1216
+ $unwrapOr<const U = T>(this: Result<T, E>, def: U): T | U;
432
1217
  /**
433
- * @deprecated
1218
+ * Returns the ok value when this result is `Ok`.
1219
+ *
1220
+ * Otherwise, returns the value returned by the default function.
1221
+ *
1222
+ * @example
1223
+ *
1224
+ * ```ts
1225
+ * const result = Ok("test");
1226
+ * assert.equal(result.$unwrapOrElse(() => "default"), "test");
1227
+ * ```
1228
+ *
1229
+ * @example
1230
+ *
1231
+ * ```ts
1232
+ * const result = Err("test");
1233
+ * assert.equal(result.$unwrapOrElse(() => "default"), "default");
1234
+ * ```
434
1235
  */
435
- join(...args: unknown[]): unknown;
1236
+ $unwrapOrElse<U = T>(this: Result<T, E>, f: () => U): T | U;
436
1237
  /**
437
- * @deprecated
1238
+ * Performs an assertion when this result is `Ok`:
1239
+ *
1240
+ * - returning `Ok` containing the current ok value when it is truthy, and
1241
+ * when no predicate/condition function is provided. Narrows the `T` type
1242
+ * to include only truthy values;
1243
+ * - returning `Ok` containing the current ok value when a
1244
+ * predicate/condition function is provided and it returns a truthy value.
1245
+ * Narrows the `T` type to the predicate type (if any);
1246
+ * - returning the default result when no predicate/condition function is
1247
+ * provided and the current ok value is falsey;
1248
+ * - returning the default result when a predicate/condition function is
1249
+ * provided and it returns a falsey value.
1250
+ *
1251
+ * Otherwise returns `Err` containing the current error value.
1252
+ *
1253
+ * @example
1254
+ *
1255
+ * ```ts
1256
+ * const result: Result<string | null, string> = Ok("test");
1257
+ * const asserted = result.$assertOr(Ok("ok-default"));
1258
+ *
1259
+ * asserted satisfies Result<string, string>;
1260
+ * assert.equal(asserted.$unwrap(), "test");
1261
+ * ```
1262
+ *
1263
+ * @example
1264
+ *
1265
+ * ```ts
1266
+ * const result: Result<string | null, string> = Ok("test");
1267
+ * const asserted = result.$assertOr(
1268
+ * Err("err-default"),
1269
+ * (val): val is "test" => val === "test",
1270
+ * );
1271
+ *
1272
+ * asserted satisfies Result<"test", string>;
1273
+ * assert.equal(asserted.$unwrap(), "test");
1274
+ * ```
1275
+ *
1276
+ * @example
1277
+ *
1278
+ * ```ts
1279
+ * const result: Result<string | null, string> = Ok(null);
1280
+ * const asserted = result.$assertOr(Ok("ok-default"));
1281
+ *
1282
+ * asserted satisfies Result<string, string>;
1283
+ * assert.equal(asserted.$unwrap(), "ok-default");
1284
+ * ```
1285
+ *
1286
+ * @example
1287
+ *
1288
+ * ```ts
1289
+ * const result: Result<string | null, string> = Ok("value");
1290
+ * const asserted = result.$assertOr(
1291
+ * Err("err-default"),
1292
+ * (val): val is "test" => val === "test",
1293
+ * );
1294
+ *
1295
+ * asserted satisfies Result<"test", string>;
1296
+ * assert.equal(asserted.$unwrapErr(), "err-default");
1297
+ * ```
1298
+ *
1299
+ * @example
1300
+ *
1301
+ * ```ts
1302
+ * const result: Result<string | null, string> = Err("test");
1303
+ * const asserted = result.$assertOr(
1304
+ * Err("err-default"),
1305
+ * (val): val is "test" => val === "test",
1306
+ * );
1307
+ *
1308
+ * asserted satisfies Result<"test", string>;
1309
+ * assert.equal(asserted.$unwrapErr(), "test");
1310
+ * ```
438
1311
  */
439
- keys(...args: unknown[]): unknown;
1312
+ $assertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>): Result<Truthy<T>, E | F>;
1313
+ $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>;
1314
+ $assertOr<U = T, F = E>(this: Result<T, E>, def: Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
440
1315
  /**
441
- * @deprecated
1316
+ * Performs an assertion when this result is `Ok`:
1317
+ *
1318
+ * - returning `Ok` containing the current ok value when it is truthy, and
1319
+ * when no predicate/condition function is provided. Narrows the `T` type
1320
+ * to include only truthy values;
1321
+ * - returning `Ok` containing the current ok value when a
1322
+ * predicate/condition function is provided and it returns a truthy value.
1323
+ * Narrows the `T` type to the predicate type (if any);
1324
+ * - returning the result returned by the default function when no
1325
+ * predicate/condition function is provided and the current ok value is
1326
+ * falsey;
1327
+ * - returning the result returned by the default function when a
1328
+ * predicate/condition function is provided and it returns a falsey value.
1329
+ *
1330
+ * Otherwise returns `Err` containing the current error value.
1331
+ *
1332
+ * @example
1333
+ *
1334
+ * ```ts
1335
+ * const result: Result<string | null, string> = Ok("test");
1336
+ * const asserted = result.$assertOrElse(
1337
+ * (val) => Ok(`ok-default:${val}`),
1338
+ * );
1339
+ *
1340
+ * asserted satisfies Result<string, string>;
1341
+ * assert.equal(asserted.$unwrap(), "test");
1342
+ * ```
1343
+ *
1344
+ * @example
1345
+ *
1346
+ * ```ts
1347
+ * const result: Result<string | null, string> = Ok("test");
1348
+ * const asserted = result.$assertOrElse(
1349
+ * (val) => Err(`err-default:${val}`),
1350
+ * (val): val is "test" => val === "test",
1351
+ * );
1352
+ *
1353
+ * asserted satisfies Result<"test", string>;
1354
+ * assert.equal(asserted.$unwrap(), "test");
1355
+ * ```
1356
+ *
1357
+ * @example
1358
+ *
1359
+ * ```ts
1360
+ * const result: Result<string | null, string> = Ok(null);
1361
+ * const asserted = result.$assertOrElse(
1362
+ * (val) => Ok(`ok-default:${val}`),
1363
+ * );
1364
+ *
1365
+ * asserted satisfies Result<string, string>;
1366
+ * assert.equal(asserted.$unwrap(), "ok-default:null");
1367
+ * ```
1368
+ *
1369
+ * @example
1370
+ *
1371
+ * ```ts
1372
+ * const result: Result<string | null, string> = Ok("value");
1373
+ * const asserted = result.$assertOrElse(
1374
+ * (val) => Err(`err-default:${val}`),
1375
+ * (val): val is "test" => val === "test",
1376
+ * );
1377
+ *
1378
+ * asserted satisfies Result<"test", string>;
1379
+ * assert.equal(asserted.$unwrapErr(), "err-default:value");
1380
+ * ```
1381
+ *
1382
+ * @example
1383
+ *
1384
+ * ```ts
1385
+ * const result: Result<string | null, string> = Err("test");
1386
+ * const asserted = result.$assertOrElse(
1387
+ * (val) => Err(`err-default:${val}`),
1388
+ * (val): val is "test" => val === "test",
1389
+ * );
1390
+ *
1391
+ * asserted satisfies Result<"test", string>;
1392
+ * assert.equal(asserted.$unwrapErr(), "test");
1393
+ * ```
442
1394
  */
443
- lastIndexOf(...args: unknown[]): unknown;
1395
+ $assertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>): Result<Truthy<T>, E | F>;
1396
+ $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>;
1397
+ $assertOrElse<U = T, F = E>(this: Result<T, E>, def: (val: T) => Result<U, F>, condition: (val: T) => unknown): Result<T | U, E | F>;
444
1398
  /**
445
- * @deprecated
1399
+ * Returns `Ok` containing the return value of the map function when this
1400
+ * result is `Ok`.
1401
+ *
1402
+ * Otherwise, returns `Err` containing the current error value.
1403
+ *
1404
+ * @example
1405
+ *
1406
+ * ```ts
1407
+ * const result = Ok("test");
1408
+ * assert.equal(
1409
+ * result.$map((val) => `map:${val}`).$unwrap(),
1410
+ * "map:test",
1411
+ * );
1412
+ * ```
1413
+ *
1414
+ * @example
1415
+ *
1416
+ * ```ts
1417
+ * const result: Result<string, string> = Err("test");
1418
+ * assert.equal(
1419
+ * result.$map((val) => `map:${val}`).$unwrapErr(),
1420
+ * "test",
1421
+ * );
1422
+ * ```
446
1423
  */
447
- map(...args: unknown[]): unknown;
1424
+ $map<U>(this: Result<T, E>, f: (value: T) => U): Result<U, E>;
448
1425
  /**
449
- * @deprecated
1426
+ * Returns `Err` containing the return value of the map function when this
1427
+ * result is `Err`.
1428
+ *
1429
+ * Otherwise, returns `Ok` containing the current ok value.
1430
+ *
1431
+ * @example
1432
+ *
1433
+ * ```ts
1434
+ * const result = Err("test");
1435
+ * assert.equal(
1436
+ * result.$mapErr((err) => `map-err:${err}`).$unwrapErr(),
1437
+ * "map-err:test",
1438
+ * );
1439
+ * ```
1440
+ *
1441
+ * @example
1442
+ *
1443
+ * ```ts
1444
+ * const result: Result<string, string> = Ok("test");
1445
+ * assert.equal(
1446
+ * result.$mapErr((err) => `map-err:${err}`).$unwrap(),
1447
+ * "test",
1448
+ * );
1449
+ * ```
1450
+ */
1451
+ $mapErr<F = E>(this: Result<T, E>, f: (err: E) => F): Result<T, F>;
1452
+ /**
1453
+ * Returns `Ok` containing the return value of the map function when this
1454
+ * result is `Ok`.
1455
+ *
1456
+ * Otherwise, returns `Ok` containing the default value.
1457
+ *
1458
+ * @example
1459
+ *
1460
+ * ```ts
1461
+ * const result: Result<string, string> = Ok("test");
1462
+ * assert.equal(
1463
+ * result.$mapOr("default", (val) => `map:${val}`).$unwrap(),
1464
+ * "map:test",
1465
+ * );
1466
+ * ```
1467
+ *
1468
+ * @example
1469
+ *
1470
+ * ```ts
1471
+ * const result: Result<string, string> = Err("test");
1472
+ * assert.equal(
1473
+ * result.$mapOr("default", (val) => `map:${val}`).$unwrap(),
1474
+ * "default",
1475
+ * );
1476
+ * ```
450
1477
  */
451
- pop(...args: unknown[]): any;
1478
+ $mapOr<U, V = U>(this: Result<T, E>, def: U, f: (val: T) => V): Result<U | V, E>;
452
1479
  /**
453
- * @deprecated
1480
+ * Returns `Ok` containing the return value of the map function when this
1481
+ * result is `Ok`.
1482
+ *
1483
+ * Otherwise, returns `Ok` containing the return value of the default
1484
+ * function.
1485
+ *
1486
+ * @example
1487
+ *
1488
+ * ```ts
1489
+ * const result: Result<string, string> = Ok("test");
1490
+ * assert.equal(
1491
+ * result.$mapOrElse(
1492
+ * (err) => `default:${err}`,
1493
+ * (val) => `map:${val}`,
1494
+ * ).$unwrap(),
1495
+ * "map:test",
1496
+ * );
1497
+ * ```
1498
+ *
1499
+ * @example
1500
+ *
1501
+ * ```ts
1502
+ * const result: Result<string, string> = Err("test");
1503
+ * assert.equal(
1504
+ * result.$mapOrElse(
1505
+ * (err) => `default:${err}`,
1506
+ * (val) => `map:${val}`,
1507
+ * ).$unwrap(),
1508
+ * "default:test",
1509
+ * );
1510
+ * ```
454
1511
  */
455
- push(...args: unknown[]): unknown;
1512
+ $mapOrElse<U, V = U>(this: Result<T, E>, def: (err: E) => U, f: (val: T) => V): Result<U | V, E>;
456
1513
  /**
457
- * @deprecated
1514
+ * Returns the or result, when this result is `Err`.
1515
+ *
1516
+ * Otherwise, returns `Ok` containing the current ok value.
1517
+ *
1518
+ * @example
1519
+ *
1520
+ * ```ts
1521
+ * const result = Err("test");
1522
+ * assert.equal(
1523
+ * result.$or(Ok("or-ok")).$unwrap(),
1524
+ * "or-ok",
1525
+ * );
1526
+ * ```
1527
+ *
1528
+ * @example
1529
+ *
1530
+ * ```ts
1531
+ * const result = Err("test");
1532
+ * assert.equal(
1533
+ * result.$or(Err("or-err")).$unwrapErr(),
1534
+ * "or-err",
1535
+ * );
1536
+ * ```
1537
+ *
1538
+ * @example
1539
+ *
1540
+ * ```ts
1541
+ * const result = Ok("test");
1542
+ * assert.equal(
1543
+ * result.$or(Ok("or-ok")).$unwrap(),
1544
+ * "test",
1545
+ * );
1546
+ * ```
458
1547
  */
459
- reduce(...args: unknown[]): unknown;
1548
+ $or<U = T, F = E>(this: Result<T, E>, or: Result<U, F>): Result<T | U, F>;
460
1549
  /**
461
- * @deprecated
1550
+ * Returns the result returned by the or function, when this result is `Err`.
1551
+ *
1552
+ * Otherwise, returns `Ok` containing the current ok value.
1553
+ *
1554
+ * @example
1555
+ *
1556
+ * ```ts
1557
+ * const result = Err("test");
1558
+ * assert.equal(
1559
+ * result.$orElse((err) => Ok(`or-ok:${err}`)).$unwrap(),
1560
+ * "or-ok:test",
1561
+ * );
1562
+ * ```
1563
+ *
1564
+ * @example
1565
+ *
1566
+ * ```ts
1567
+ * const result = Err("test");
1568
+ * assert.equal(
1569
+ * result.$orElse((err) => Err(`or-err:${err}`)).$unwrapErr(),
1570
+ * "or-err:test",
1571
+ * );
1572
+ * ```
1573
+ *
1574
+ * @example
1575
+ *
1576
+ * ```ts
1577
+ * const result: Result<string, string> = Ok("test");
1578
+ * assert.equal(
1579
+ * result.$orElse((err) => Ok(`or-ok:${err}`)).$unwrap(),
1580
+ * "test",
1581
+ * );
1582
+ * ```
1583
+ */
1584
+ $orElse<U = T, F = E>(this: Result<T, E>, f: (err: E) => Result<U, F>): Result<T | U, F>;
1585
+ /**
1586
+ * Returns a result based on the outcome of the safe function when this
1587
+ * result is `Err`.
1588
+ *
1589
+ * Otherwise, returns `Ok` containing the current ok value.
1590
+ *
1591
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1592
+ * `result.$or(Result.$safe(...))`.
462
1593
  */
463
- reduceRight(...args: unknown[]): unknown;
1594
+ $orSafe<U = T>(this: Result<T, E>, f: (err: E) => U): Result<T | U, Error>;
1595
+ $orSafe<U = T, F = E>(this: Result<T, E>, f: (err: E) => U, mapError: (err: unknown) => F): Result<T | U, F>;
464
1596
  /**
465
- * @deprecated
1597
+ * Returns the and result, when this result is `Ok`.
1598
+ *
1599
+ * Otherwise, returns `Err` containing the current error value.
1600
+ *
1601
+ * @example
1602
+ *
1603
+ * ```ts
1604
+ * const result = Ok("test");
1605
+ * assert.equal(
1606
+ * result.$and(Ok("and-ok")).$unwrap(),
1607
+ * "and-ok",
1608
+ * );
1609
+ * ```
1610
+ *
1611
+ * @example
1612
+ *
1613
+ * ```ts
1614
+ * const result = Ok("test");
1615
+ * assert.equal(
1616
+ * result.$and(Err("and-err")).$unwrapErr(),
1617
+ * "and-err",
1618
+ * );
1619
+ * ```
1620
+ *
1621
+ * @example
1622
+ *
1623
+ * ```ts
1624
+ * const result = Err("test");
1625
+ * assert.equal(
1626
+ * result.$and(Ok("and-ok")).$unwrapErr(),
1627
+ * "test",
1628
+ * );
1629
+ * ```
466
1630
  */
467
- reverse(...args: unknown[]): unknown;
1631
+ $and<U = T, F = E>(this: Result<T, E>, and: Result<U, F>): Result<U, E | F>;
468
1632
  /**
469
- * @deprecated
1633
+ * Returns the and result, when this result is `Ok`.
1634
+ *
1635
+ * Otherwise, returns `Err` containing the current error value.
1636
+ *
1637
+ * @example
1638
+ *
1639
+ * ```ts
1640
+ * const result = Ok("test");
1641
+ * assert.equal(
1642
+ * result.$and((val) => Ok(`and-ok:${val}`)).$unwrap(),
1643
+ * "and-ok:test",
1644
+ * );
1645
+ * ```
1646
+ *
1647
+ * @example
1648
+ *
1649
+ * ```ts
1650
+ * const result = Ok("test");
1651
+ * assert.equal(
1652
+ * result.$and((val) => Err(`and-err:${val}`)).$unwrapErr(),
1653
+ * "and-err:test",
1654
+ * );
1655
+ * ```
1656
+ *
1657
+ * @example
1658
+ *
1659
+ * ```ts
1660
+ * const result: Result<string, string> = Err("test");
1661
+ * assert.equal(
1662
+ * result.$and((val) => Ok(`and-ok:${val}`)).$unwrapErr(),
1663
+ * "test",
1664
+ * );
1665
+ * ```
470
1666
  */
471
- shift(...args: unknown[]): unknown;
1667
+ $andThen<U = T, F = E>(this: Result<T, E>, f: (val: T) => Result<U, F>): Result<U, E | F>;
472
1668
  /**
473
- * @deprecated
1669
+ * Calls the through function when this result is `Ok` and returns:
1670
+ *
1671
+ * - `Ok` containing the original ok value when the through function
1672
+ * returns `Ok`;
1673
+ * - the `Err` returned by the through function when it returns `Err`.
1674
+ *
1675
+ * Otherwise, returns `Err` containing the current error value.
1676
+ *
1677
+ * @example
1678
+ *
1679
+ * ```ts
1680
+ * const result = Ok("test");
1681
+ * assert.equal(
1682
+ * result.$andThrough((val) => Ok(`ok-through:${val}`)).$unwrap(),
1683
+ * "test",
1684
+ * );
1685
+ * ```
1686
+ *
1687
+ * @example
1688
+ *
1689
+ * ```ts
1690
+ * const result = Ok("test");
1691
+ * assert.equal(
1692
+ * result.$andThrough((val) => Err(`err-through:${val}`)).$unwrapErr(),
1693
+ * "err-through:test",
1694
+ * );
1695
+ * ```
1696
+ *
1697
+ * @example
1698
+ *
1699
+ * ```ts
1700
+ * const result: Result<string, string> = Err("test");
1701
+ * assert.equal(
1702
+ * result.$andThrough((val) => Ok(`ok-through:${val}`)).$unwrapErr(),
1703
+ * "test",
1704
+ * );
1705
+ * ```
1706
+ */
1707
+ $andThrough<F = E>(this: Result<T, E>, f: (val: T) => Result<any, F>): Result<T, E | F>;
1708
+ /**
1709
+ * Returns a result based on the outcome of the safe function when this
1710
+ * result is `Ok`.
1711
+ *
1712
+ * Otherwise, returns `Err` containing the current error value.
1713
+ *
1714
+ * Uses the same strategy as {@link Result.$safe}, equivalent to calling
1715
+ * `result.$and(Result.$safe(...))`.
474
1716
  */
475
- slice(...args: unknown[]): unknown;
1717
+ $andSafe<U = T>(this: Result<T, E>, f: (val: T) => U): Result<U, E | Error>;
1718
+ $andSafe<U = T, F = E>(this: Result<T, E>, f: (val: T) => U, mapError: (err: unknown) => F): Result<U, E | F>;
476
1719
  /**
477
- * @deprecated
1720
+ * Calls the peek function and returns `Result` equivalent to this result.
1721
+ *
1722
+ * @example
1723
+ *
1724
+ * ```ts
1725
+ * const result: Result<string, string> = Ok("test");
1726
+ * assert.equal(
1727
+ * result
1728
+ * .$peek((res) => {
1729
+ * const [err, value] = res;
1730
+ *
1731
+ * console.log("Err:", err); // Err: undefined
1732
+ * console.log("Value:", value); // Value: test
1733
+ * })
1734
+ * .$unwrap(),
1735
+ * "test",
1736
+ * );
1737
+ * ```
1738
+ *
1739
+ * @example
1740
+ *
1741
+ * ```ts
1742
+ * const result: Result<string, string> = Err("test");
1743
+ * assert.equal(
1744
+ * result
1745
+ * .$peek((res) => {
1746
+ * const [err, value] = res;
1747
+ *
1748
+ * console.log("Err:", err); // Err: test
1749
+ * console.log("Value:", value); // Value: undefined
1750
+ * })
1751
+ * .$unwrapErr(),
1752
+ * "test",
1753
+ * );
1754
+ * ```
478
1755
  */
479
- some(...args: unknown[]): unknown;
1756
+ $peek(this: Result<T, E>, f: (res: Result<T, E>) => void): Result<T, E>;
480
1757
  /**
481
- * @deprecated
1758
+ * Calls the tap function when this result is `Ok`, and returns `Ok`
1759
+ * containing the current ok value.
1760
+ *
1761
+ * @example
1762
+ *
1763
+ * ```ts
1764
+ * const result = Ok("test");
1765
+ * assert.equal(
1766
+ * result
1767
+ * .$tap((val) => console.log("Value:", val)) // Value: test
1768
+ * .$unwrap(),
1769
+ * "test",
1770
+ * );
1771
+ * ```
1772
+ *
1773
+ * @example
1774
+ *
1775
+ * ```ts
1776
+ * const result: Record<string, string> = Err("test");
1777
+ * assert.equal(
1778
+ * result
1779
+ * .$tap((val) => console.log("Value:", val)) // not executed
1780
+ * .$unwrapErr(),
1781
+ * "test",
1782
+ * );
1783
+ * ```
482
1784
  */
483
- sort(...args: unknown[]): unknown;
1785
+ $tap(this: Result<T, E>, f: (val: T) => any): Result<T, E>;
484
1786
  /**
485
- * @deprecated
1787
+ * Calls the tap error function when this result is `Err`, and returns `Err`
1788
+ * containing the current error value.
1789
+ *
1790
+ * @example
1791
+ *
1792
+ * ```ts
1793
+ * const result = Err("test");
1794
+ * assert.equal(
1795
+ * result
1796
+ * .$tapErr((err) => console.log("Err:", err)) // Err: test
1797
+ * .$unwrapErr(),
1798
+ * "test",
1799
+ * );
1800
+ * ```
1801
+ *
1802
+ * @example
1803
+ *
1804
+ * ```ts
1805
+ * const result: Record<string, string> = Ok("test");
1806
+ * assert.equal(
1807
+ * result
1808
+ * .$tapErr((err) => console.log("Err:", err)) // not executed
1809
+ * .$unwrap(),
1810
+ * "test",
1811
+ * );
1812
+ * ```
486
1813
  */
487
- splice(...args: unknown[]): unknown;
1814
+ $tapErr(this: Result<T, E>, f: (err: E) => void): Result<T, E>;
488
1815
  /**
489
- * @deprecated
1816
+ * Returns the contained `Result` when this result is `Ok`.
1817
+ *
1818
+ * Otherwise returns `Err` containing the current error value.
1819
+ *
1820
+ * This method should only be called when the `T` type is `Result`. This
1821
+ * is enforced with a type constraint. If the ok value is not
1822
+ * a result, `RetupleFlattenFailed` is thrown.
1823
+ *
1824
+ * @example
1825
+ *
1826
+ * ```ts
1827
+ * const result = Ok(Ok("test"));
1828
+ * assert.equal(result.$flatten().$unwrap(), "test");
1829
+ * ```
1830
+ *
1831
+ * @example
1832
+ *
1833
+ * ```ts
1834
+ * const result = Ok(Err("test"));
1835
+ * assert.equal(result.$flatten().$unwrapErr(), "test");
1836
+ * ```
1837
+ *
1838
+ * @example
1839
+ *
1840
+ * ```ts
1841
+ * const result = Err("test");
1842
+ * assert.equal(result.$flatten().$unwrapErr(), "test");
1843
+ * ```
490
1844
  */
491
- toString(...args: unknown[]): unknown;
1845
+ $flatten<U, F>(this: Result<Result<U, F>, E>): Result<U, E | F>;
492
1846
  /**
493
- * @deprecated
1847
+ * Returns an equivalent `ResultAsync`.
1848
+ *
1849
+ * @example
1850
+ *
1851
+ * ```ts
1852
+ * const result = Ok("test").$async();
1853
+ * assert.equal(await result.$unwrap(), "test");
1854
+ * ```
1855
+ * @example
1856
+ *
1857
+ * ```ts
1858
+ * const result = Err("test").$async();
1859
+ * assert.equal(await result.$unwrapErr(), "test");
1860
+ * ```
494
1861
  */
495
- toLocaleString(...args: unknown[]): unknown;
1862
+ $async(this: Result<T, E>): ResultAsync<T, E>;
496
1863
  /**
497
- * @deprecated
1864
+ * Returns a `Promise` which resolves to this result.
1865
+ *
1866
+ * @example
1867
+ *
1868
+ * ```ts
1869
+ * const result = Ok("test").$promise();
1870
+ * assert.equal(await result, result);
1871
+ * ```
1872
+ *
1873
+ * @example
1874
+ *
1875
+ * ```ts
1876
+ * const result = Err("test").$promise();
1877
+ * assert.equal(await result, result);
1878
+ * ```
498
1879
  */
499
- unshift(...args: unknown[]): unknown;
1880
+ $promise(this: Result<T, E>): Promise<Result<T, E>>;
500
1881
  /**
501
- * @deprecated
1882
+ * Returns a two-element, standard array tuple equivalent to this result.
1883
+ *
1884
+ * @example
1885
+ *
1886
+ * ```ts
1887
+ * const result = Ok("test");
1888
+ * assert.deepEqual(result.$tuple(), [undefined, "test"]);
1889
+ * ```
1890
+ *
1891
+ * @example
1892
+ *
1893
+ * ```ts
1894
+ * const result = Err("test");
1895
+ * assert.deepEqual(result.$tuple(), ["test", undefined]);
1896
+ * ```
502
1897
  */
503
- values(...args: unknown[]): unknown;
1898
+ $tuple(this: Result<T, E>): [err: E | undefined, value: T | undefined];
1899
+ /**
1900
+ * Returns an `IterableIterator` over the contained ok value, when this
1901
+ * result is `Ok`.
1902
+ *
1903
+ * Otherwise, returns an empty `IterableIterator`.
1904
+ *
1905
+ * This method should only be called when the `T` type is `Iterable`. This
1906
+ * is enforced with a type constraint. If the ok value is not iterable,
1907
+ * attempting to iterate over it will throw the built-in error.
1908
+ *
1909
+ * @example
1910
+ *
1911
+ * ```ts
1912
+ * const result = Ok([1, 2, 3]);
1913
+ *
1914
+ * for (const n of result.$iter()) {
1915
+ * console.log(n); // 1.. 2.. 3
1916
+ * }
1917
+ * ```
1918
+ *
1919
+ * @example
1920
+ *
1921
+ * ```ts
1922
+ * const result = Err([1, 2, 3]);
1923
+ *
1924
+ * for (const n of result.$iter()) {
1925
+ * console.log(n); // not executed, iterator is empty
1926
+ * }
1927
+ * ```
1928
+ *
1929
+ * @example
1930
+ *
1931
+ * ```ts
1932
+ * const result = Ok<any>(1);
1933
+ *
1934
+ * try {
1935
+ * for (const n of result.$iter()) {}
1936
+ * } catch (err) {
1937
+ * // err is 'TypeError: number 1 is not iterable' in V8
1938
+ * }
1939
+ * ```
1940
+ */
1941
+ $iter<U>(this: Result<Iterable<U>, E>): IterableIterator<U, undefined, unknown>;
504
1942
  }