@sapphire/result 3.0.0-pr-589.aa473f9.0 → 3.0.0-pr-935.7da5c8bb

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.
@@ -1,5 +1,9 @@
1
1
  type Awaitable<T> = PromiseLike<T> | T;
2
+ type If<Value extends boolean, TrueResult, FalseResult> = Value extends true ? TrueResult : Value extends false ? FalseResult : TrueResult | FalseResult;
2
3
 
4
+ declare const ValueProperty$1: unique symbol;
5
+ declare const SuccessProperty: unique symbol;
6
+ declare const UnwrapSafeProperty: unique symbol;
3
7
  /**
4
8
  * A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
5
9
  * type union with the variants `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error
@@ -10,7 +14,15 @@ type Awaitable<T> = PromiseLike<T> | T;
10
14
  *
11
15
  * @see {@link https://doc.rust-lang.org/std/result/index.html}
12
16
  */
13
- interface IResult<T, E> {
17
+ declare class Result<T, E, const Success extends boolean = boolean> {
18
+ /**
19
+ * Branded value to ensure `Success` is typed correctly.
20
+ * @internal
21
+ */
22
+ protected __STATUS__: Success;
23
+ private readonly [ValueProperty$1];
24
+ private readonly [SuccessProperty];
25
+ private constructor();
14
26
  /**
15
27
  * Returns `true` if the result is `Ok`.
16
28
  *
@@ -27,7 +39,7 @@ interface IResult<T, E> {
27
39
  *
28
40
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
29
41
  */
30
- isOk(): this is ResultOk<T>;
42
+ isOk(): this is Ok<T, E>;
31
43
  /**
32
44
  * Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
33
45
  *
@@ -49,7 +61,8 @@ interface IResult<T, E> {
49
61
  *
50
62
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and}
51
63
  */
52
- isOkAnd<R extends boolean>(cb: (value: T) => R): this is ResultOk<T> & R;
64
+ isOkAnd<R extends T>(cb: (value: T) => value is R): this is Ok<R, E>;
65
+ isOkAnd<R extends boolean>(cb: (value: T) => R): this is Ok<T, E> & R;
53
66
  /**
54
67
  * Returns `true` if the result is `Err`.
55
68
  *
@@ -66,7 +79,7 @@ interface IResult<T, E> {
66
79
  *
67
80
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
68
81
  */
69
- isErr(): this is ResultErr<E>;
82
+ isErr(): this is Err<E, T>;
70
83
  /**
71
84
  * Returns `true` if the result is `Err` and the value inside of it matches a predicate.
72
85
  * @param cb The predicate.
@@ -89,7 +102,8 @@ interface IResult<T, E> {
89
102
  *
90
103
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err_and}
91
104
  */
92
- isErrAnd<R extends boolean>(cb: (error: E) => R): this is ResultErr<E> & R;
105
+ isErrAnd<R extends E>(cb: (error: E) => error is R): this is Err<R, T>;
106
+ isErrAnd<R extends boolean>(cb: (error: E) => R): this is Err<E, T> & R;
93
107
  /**
94
108
  * Converts from `Result<T, E>` to `Option<T>`.
95
109
  *
@@ -108,7 +122,7 @@ interface IResult<T, E> {
108
122
  *
109
123
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
110
124
  */
111
- ok(): Option<T>;
125
+ ok(): If<Success, Some<T>, None>;
112
126
  /**
113
127
  * Converts from `Result<T, E>` to `Option<E>`.
114
128
  *
@@ -127,7 +141,7 @@ interface IResult<T, E> {
127
141
  *
128
142
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
129
143
  */
130
- err(): Option<E>;
144
+ err(): If<Success, None, Some<E>>;
131
145
  /**
132
146
  * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
133
147
  * untouched.
@@ -146,7 +160,7 @@ interface IResult<T, E> {
146
160
  *
147
161
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
148
162
  */
149
- map<U>(cb: (value: T) => U): Result<U, E>;
163
+ map<OutputValue>(cb: (value: If<Success, T, never>) => OutputValue): Result<OutputValue, E, Success>;
150
164
  /**
151
165
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
152
166
  * untouched.
@@ -176,7 +190,7 @@ interface IResult<T, E> {
176
190
  *
177
191
  * @note This is an extension not supported in Rust
178
192
  */
179
- mapInto<IT, IE>(cb: (value: T) => Result<IT, IE>): Result<IT, E | IE>;
193
+ mapInto<OutputResult extends AnyResult>(cb: (value: If<Success, T, never>) => OutputResult): If<Success, OutputResult, Err<E>>;
180
194
  /**
181
195
  * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
182
196
  *
@@ -198,7 +212,7 @@ interface IResult<T, E> {
198
212
  *
199
213
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
200
214
  */
201
- mapOr<U>(defaultValue: U, cb: (value: T) => U): U;
215
+ mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: If<Success, T, never>) => MappedOutputValue): If<Success, MappedOutputValue, DefaultOutputValue>;
202
216
  /**
203
217
  * Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
204
218
  * to a contained `Ok` value.
@@ -220,7 +234,7 @@ interface IResult<T, E> {
220
234
  *
221
235
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
222
236
  */
223
- mapOrElse<U>(op: (error: E) => U, cb: (value: T) => U): U;
237
+ mapOrElse<OutputValue, OutputError>(op: (error: If<Success, never, E>) => OutputError, cb: (value: If<Success, T, never>) => OutputValue): If<Success, OutputValue, OutputError>;
224
238
  /**
225
239
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
226
240
  * untouched.
@@ -241,7 +255,7 @@ interface IResult<T, E> {
241
255
  *
242
256
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
243
257
  */
244
- mapErr<F>(cb: (error: E) => F): Result<T, F>;
258
+ mapErr<OutputError>(cb: (error: If<Success, never, E>) => OutputError): Result<T, OutputError, Success>;
245
259
  /**
246
260
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
247
261
  * untouched.
@@ -270,7 +284,7 @@ interface IResult<T, E> {
270
284
  *
271
285
  * @note This is an extension not supported in Rust
272
286
  */
273
- mapErrInto<IT, IE>(cb: (error: E) => Result<IT, IE>): Result<T | IT, IE>;
287
+ mapErrInto<OutputResult extends AnyResult>(cb: (error: If<Success, never, E>) => OutputResult): If<Success, Ok<T>, OutputResult>;
274
288
  /**
275
289
  * Calls the provided closure with a reference to the contained value (if `Ok`).
276
290
  * @param cb The predicate.
@@ -289,7 +303,7 @@ interface IResult<T, E> {
289
303
  *
290
304
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
291
305
  */
292
- inspect(cb: (value: T) => void): this;
306
+ inspect(cb: (value: T) => unknown): this;
293
307
  /**
294
308
  * Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
295
309
  * @param cb The predicate.
@@ -308,7 +322,7 @@ interface IResult<T, E> {
308
322
  *
309
323
  * @note This is an extension not supported in Rust
310
324
  */
311
- inspectAsync(cb: (value: T) => Awaitable<void>): Promise<this>;
325
+ inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
312
326
  /**
313
327
  * Calls the provided closure with a reference to the contained error (if `Err`).
314
328
  * @param cb The predicate.
@@ -327,7 +341,7 @@ interface IResult<T, E> {
327
341
  *
328
342
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
329
343
  */
330
- inspectErr(cb: (error: E) => void): this;
344
+ inspectErr(cb: (error: E) => unknown): this;
331
345
  /**
332
346
  * Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
333
347
  * @param cb The predicate.
@@ -346,7 +360,7 @@ interface IResult<T, E> {
346
360
  *
347
361
  * @note This is an extension not supported in Rust
348
362
  */
349
- inspectErrAsync(cb: (error: E) => Awaitable<void>): Promise<this>;
363
+ inspectErrAsync(cb: (error: E) => Awaitable<unknown>): Promise<this>;
350
364
  /**
351
365
  * Returns an iterator over the possibly contained value.
352
366
  *
@@ -395,7 +409,7 @@ interface IResult<T, E> {
395
409
  *
396
410
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
397
411
  */
398
- expect(message: string): T;
412
+ expect(message: string): If<Success, T, never>;
399
413
  /**
400
414
  * Returns the contained `Err` value.
401
415
  *
@@ -419,7 +433,7 @@ interface IResult<T, E> {
419
433
  *
420
434
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
421
435
  */
422
- expectErr(message: string): E;
436
+ expectErr(message: string): If<Success, never, E>;
423
437
  /**
424
438
  * Returns the contained `Ok` value.
425
439
  *
@@ -428,6 +442,7 @@ interface IResult<T, E> {
428
442
  * @seealso {@link unwrapOrElse}
429
443
  * @seealso {@link unwrapErr}
430
444
  * @seealso {@link unwrapRaw}
445
+ * @seealso {@link unwrapSafe}
431
446
  *
432
447
  * @example
433
448
  * ```typescript
@@ -446,7 +461,7 @@ interface IResult<T, E> {
446
461
  *
447
462
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
448
463
  */
449
- unwrap(): T;
464
+ unwrap(): If<Success, T, never>;
450
465
  /**
451
466
  * Returns the contained `Err` value.
452
467
  *
@@ -455,6 +470,7 @@ interface IResult<T, E> {
455
470
  * @seealso {@link unwrapOr}
456
471
  * @seealso {@link unwrapOrElse}
457
472
  * @seealso {@link unwrapRaw}
473
+ * @seealso {@link unwrapSafe}
458
474
  *
459
475
  * @example
460
476
  * ```typescript
@@ -473,7 +489,7 @@ interface IResult<T, E> {
473
489
  *
474
490
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
475
491
  */
476
- unwrapErr(): E;
492
+ unwrapErr(): If<Success, never, E>;
477
493
  /**
478
494
  * Returns the contained `Ok` value or the provided default.
479
495
  *
@@ -483,6 +499,7 @@ interface IResult<T, E> {
483
499
  * @seealso {@link unwrapOrElse}
484
500
  * @seealso {@link unwrapErr}
485
501
  * @seealso {@link unwrapRaw}
502
+ * @seealso {@link unwrapSafe}
486
503
  *
487
504
  * @param defaultValue The default value.
488
505
  *
@@ -499,13 +516,14 @@ interface IResult<T, E> {
499
516
  *
500
517
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
501
518
  */
502
- unwrapOr<V>(defaultValue: V): T | V;
519
+ unwrapOr<OutputValue>(defaultValue: OutputValue): If<Success, T, OutputValue>;
503
520
  /**
504
521
  * Returns the contained `Ok` value or computes it from a closure.
505
522
  * @seealso {@link unwrap}
506
523
  * @seealso {@link unwrapOr}
507
524
  * @seealso {@link unwrapErr}
508
525
  * @seealso {@link unwrapRaw}
526
+ * @seealso {@link unwrapSafe}
509
527
  *
510
528
  * @param op The predicate.
511
529
  *
@@ -519,7 +537,7 @@ interface IResult<T, E> {
519
537
  *
520
538
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
521
539
  */
522
- unwrapOrElse<V>(op: (error: E) => V): T | V;
540
+ unwrapOrElse<OutputValue>(op: (error: E) => OutputValue): If<Success, T, OutputValue>;
523
541
  /**
524
542
  * Returns the contained `Ok` value.
525
543
  *
@@ -528,6 +546,7 @@ interface IResult<T, E> {
528
546
  * @seealso {@link unwrapOr}
529
547
  * @seealso {@link unwrapOrElse}
530
548
  * @seealso {@link unwrapErr}
549
+ * @seealso {@link unwrapSafe}
531
550
  *
532
551
  * @example
533
552
  * ```typescript
@@ -544,7 +563,21 @@ interface IResult<T, E> {
544
563
  * });
545
564
  * ```
546
565
  */
547
- unwrapRaw(): T | never;
566
+ unwrapRaw(): If<Success, T, never>;
567
+ /**
568
+ * Returns the contained `Ok` value or yelds the contained `Err` value.
569
+ * Emulates Rust's `?` operator in `safeTry`'s body. See also {@link Result.safeTry}.
570
+ *
571
+ * If used outside of a `safeTry`'s' body, throws the contained error.
572
+ * @seealso {@link unwrap}
573
+ * @seealso {@link unwrapOr}
574
+ * @seealso {@link unwrapErr}
575
+ * @seealso {@link unwrapRaw}
576
+ * @seealso {@link unwrapSafe}
577
+ *
578
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_safe}
579
+ */
580
+ unwrapSafe(): Generator<Err<E, T>, T>;
548
581
  /**
549
582
  * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
550
583
  * @param result The result to check.
@@ -570,7 +603,7 @@ interface IResult<T, E> {
570
603
  *
571
604
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
572
605
  */
573
- and<U>(result: Result<U, E>): Result<U, E>;
606
+ and<OutputResult extends AnyResult>(result: OutputResult): If<Success, OutputResult, Err<E>>;
574
607
  /**
575
608
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
576
609
  *
@@ -590,7 +623,7 @@ interface IResult<T, E> {
590
623
  *
591
624
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
592
625
  */
593
- andThen<U>(cb: (value: T) => Result<U, E>): Result<U, E>;
626
+ andThen<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): OutputResult;
594
627
  /**
595
628
  * Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
596
629
  *
@@ -625,7 +658,7 @@ interface IResult<T, E> {
625
658
  *
626
659
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
627
660
  */
628
- or<F>(result: Result<T, F>): Result<T, F>;
661
+ or<OutputResult extends AnyResult>(result: OutputResult): If<Success, Ok<T>, OutputResult>;
629
662
  /**
630
663
  * Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
631
664
  *
@@ -645,7 +678,7 @@ interface IResult<T, E> {
645
678
  *
646
679
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
647
680
  */
648
- orElse<F>(cb: (error: E) => Result<T, F>): Result<T, F>;
681
+ orElse<OutputResult extends AnyResult>(cb: (error: E) => OutputResult): If<Success, Ok<T>, OutputResult>;
649
682
  /**
650
683
  * Returns `true` if the result is an `Ok` and the given value strict equals it.
651
684
  * @param value The value to compare.
@@ -668,7 +701,8 @@ interface IResult<T, E> {
668
701
  *
669
702
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains}
670
703
  */
671
- contains(value: T): boolean;
704
+ contains<const Value extends T>(this: Ok<T>, value: Value): this is Ok<Value>;
705
+ contains(this: Err<E>, value: T): false;
672
706
  /**
673
707
  * Returns `true` if the result is an `Err` and the given error strict equals it.
674
708
  * @param error The error to compare.
@@ -691,7 +725,8 @@ interface IResult<T, E> {
691
725
  *
692
726
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains_err}
693
727
  */
694
- containsErr(error: E): boolean;
728
+ containsErr(this: Ok<T>, error: E): false;
729
+ containsErr<const Value extends E>(this: Err<E>, error: Value): this is Err<Value>;
695
730
  /**
696
731
  * Transposes a `Result` of an `Option` into an `Option` of a `Result`.
697
732
  *
@@ -706,7 +741,7 @@ interface IResult<T, E> {
706
741
  *
707
742
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
708
743
  */
709
- transpose<IT>(this: Result<Option<IT>, E>): Option<Result<IT, E>>;
744
+ transpose<InnerValue>(this: Result<Option<InnerValue>, E, Success>): If<Success, Option<Ok<InnerValue>>, Some<Err<E>>>;
710
745
  /**
711
746
  * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
712
747
  *
@@ -728,7 +763,7 @@ interface IResult<T, E> {
728
763
  *
729
764
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
730
765
  */
731
- flatten<IT>(this: Result<Result<IT, E>, E>): Result<IT, E>;
766
+ flatten<InnerResult extends AnyResult>(this: Result<InnerResult, E, Success>): If<Success, InnerResult, Err<E>>;
732
767
  /**
733
768
  * Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
734
769
  *
@@ -745,7 +780,7 @@ interface IResult<T, E> {
745
780
  *
746
781
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
747
782
  */
748
- intoOkOrErr(): T | E;
783
+ intoOkOrErr(): If<Success, T, E>;
749
784
  /**
750
785
  * Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
751
786
  *
@@ -757,14 +792,14 @@ interface IResult<T, E> {
757
792
  *
758
793
  * @note This is an extension not supported in Rust
759
794
  */
760
- intoPromise(): Promise<Result<Awaited<T>, Awaited<E>>>;
795
+ intoPromise(): Promise<If<Success, Ok<Awaited<T>>, Err<Awaited<E>>>>;
761
796
  /**
762
797
  * Checks whether or not `other` equals with self.
763
798
  * @param other The other result to compare.
764
799
  *
765
800
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
766
801
  */
767
- eq(other: Result<T, E>): boolean;
802
+ eq<OtherValue extends T, OtherError extends E, OtherSuccess extends boolean>(other: Result<OtherValue, OtherError, OtherSuccess>): this is Result<OtherValue, OtherError, OtherSuccess>;
768
803
  /**
769
804
  * Checks whether or not `other` doesn't equal with self.
770
805
  * @param other The other result to compare.
@@ -794,9 +829,9 @@ interface IResult<T, E> {
794
829
  * ```
795
830
  */
796
831
  match<OkValue, ErrValue>(branches: {
797
- ok(value: T): OkValue;
798
- err(error: E): ErrValue;
799
- }): OkValue | ErrValue;
832
+ ok(this: Ok<T>, value: If<Success, T, never>): OkValue;
833
+ err(this: Err<E>, error: If<Success, never, E>): ErrValue;
834
+ }): If<Success, OkValue, ErrValue>;
800
835
  /**
801
836
  * Returns an iterator over the possibly contained value.
802
837
  *
@@ -823,126 +858,218 @@ interface IResult<T, E> {
823
858
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
824
859
  */
825
860
  [Symbol.iterator](): Generator<T>;
826
- }
827
-
828
- declare class ResultOk<T> implements IResult<T, any> {
829
- private readonly value;
830
- constructor(value: T);
831
- isOk(): this is ResultOk<T>;
832
- isOkAnd<R extends boolean>(cb: (value: T) => R): R;
833
- isErr(): false;
834
- isErrAnd(cb?: (error: never) => boolean): false;
835
- ok(): OptionSome<T>;
836
- err(): OptionNone;
837
- map<U>(cb: (value: T) => U): ResultOk<U>;
838
- mapInto<R extends Result<any, any>>(cb: (value: T) => R): R;
839
- mapOr<U>(_: U, cb: (value: T) => U): U;
840
- mapOrElse<U>(_: (error: never) => U, cb: (value: T) => U): U;
841
- mapErr(cb?: (error: never) => any): this;
842
- mapErrInto(cb: (error: never) => Result<any, any>): this;
843
- inspect(cb: (value: T) => void): this;
844
- inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
845
- inspectErr(cb?: (error: never) => void): this;
846
- inspectErrAsync(cb?: (error: never) => Awaitable<unknown>): Promise<this>;
847
- iter(): Generator<T>;
848
- expect(message?: string): T;
849
- expectErr(message: string): never;
850
- unwrap(): T;
851
- unwrapErr(): never;
852
- unwrapOr(defaultValue: unknown): T;
853
- unwrapOrElse(op: (error: any) => unknown): T;
854
- unwrapRaw(): T;
855
- and<R extends Result<any, any>>(result: R): R;
856
- andThen<R extends Result<any, any>>(cb: (value: T) => R): R;
857
- or(result: Result<T, any>): this;
858
- orElse(cb: (error: never) => Result<T, any>): this;
859
- contains(value: T): boolean;
860
- containsErr(error?: unknown): false;
861
- transpose(this: ResultOk<OptionNone>): OptionNone;
862
- transpose<Inner>(this: ResultOk<OptionSome<Inner>>): OptionSome<ResultOk<Inner>>;
863
- transpose<Inner>(this: ResultOk<Option<Inner>>): Option<ResultOk<Inner>>;
864
- flatten<Inner extends Result<any, any>>(this: ResultOk<Inner>): Inner;
865
- intoOkOrErr(): T;
866
- intoPromise(): Promise<ResultOk<Awaited<T>>>;
867
- eq(other: ResultErr<any>): false;
868
- eq(other: Result<T, any>): boolean;
869
- ne(other: ResultErr<any>): true;
870
- ne(other: Result<T, any>): boolean;
871
- match<OkValue, ErrValue>(branches: {
872
- ok(value: T): OkValue;
873
- err(error: never): ErrValue;
874
- }): OkValue;
875
- [Symbol.iterator](): Generator<T>;
876
- }
877
- /**
878
- * Creates an Ok with no value.
879
- * @return A successful Result.
880
- */
881
- declare function createOk(): ResultOk<unknown>;
882
- /**
883
- * Creates an Ok.
884
- * @typeparam T The result's type.
885
- * @param x Value to use.
886
- * @return A successful Result.
887
- */
888
- declare function createOk<T>(x: T): ResultOk<T>;
889
-
890
- declare class ResultError<E> extends Error {
891
- readonly value: E;
892
- constructor(message: string, value: E);
893
- get name(): string;
894
- }
895
-
896
- /**
897
- * The union of the two variations of `Result`.
898
- * @typeparam T The result's type.
899
- * @typeparam E The error's type.
900
- */
901
- type Result<T, E> = Result.Ok<T> | Result.Err<E>;
902
- declare namespace Result {
903
- type Resolvable<T, E> = T | Result<T, E>;
904
- function is<T, E>(value: Result<T, E>): true;
905
- function is(value: any): value is Result<unknown, unknown>;
861
+ get [Symbol.toStringTag](): If<Success, 'Ok', 'Err'>;
862
+ /**
863
+ * This function, in combination with `[$]`, is intended to emulate
864
+ * Rust's ? operator.
865
+ *
866
+ * @see {@link Result.safeTry}
867
+ * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.safeTry}
868
+ */
869
+ get [UnwrapSafeProperty](): Generator<Err<E, T>, T>;
870
+ static ok<T = undefined, E = any>(this: void, value?: T): Ok<T, E>;
871
+ static err<E = undefined, T = any>(this: void, value?: E): Err<E, T>;
872
+ /**
873
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object. This override
874
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
875
+ * library or from a different build.
876
+ *
877
+ * @param instance The instance to check.
878
+ * @returns Whether or not the instance is a `Result`.
879
+ *
880
+ * @example
881
+ * ```typescript
882
+ * import { Result } from '@sapphire/result';
883
+ * const { ok } = require('@sapphire/result');
884
+ *
885
+ * ok(2) instanceof Result; // true
886
+ * ```
887
+ */
888
+ static [Symbol.hasInstance](instance: unknown): boolean;
889
+ /**
890
+ * @deprecated Use {@link Result.isResult} instead.
891
+ *
892
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
893
+ *
894
+ * @param instance The instance to check.
895
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
896
+ *
897
+ * @example
898
+ * ```typescript
899
+ * import { Result } from '@sapphire/result';
900
+ * const { ok } = require('@sapphire/result');
901
+ *
902
+ * Result.isResult(ok(2)); // true
903
+ * ```
904
+ */
905
+ static is(instance: unknown): instance is AnyResult;
906
+ /**
907
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
908
+ *
909
+ * @param instance The instance to check.
910
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
911
+ *
912
+ * @example
913
+ * ```typescript
914
+ * import { Result } from '@sapphire/result';
915
+ * const { ok } = require('@sapphire/result');
916
+ *
917
+ * Result.isResult(ok(2)); // true
918
+ * ```
919
+ */
920
+ static isResult(instance: unknown): instance is AnyResult;
906
921
  /**
907
922
  * Creates a {@link Result} out of a callback.
923
+ *
908
924
  * @typeparam T The result's type.
909
925
  * @typeparam E The error's type.
910
926
  */
911
- function from<T, E = unknown>(op: Resolvable<T, E> | (() => Resolvable<T, E>)): Result<T, E>;
927
+ static from<T, E = unknown>(this: void, op: ResultResolvable<T, E> | (() => ResultResolvable<T, E>)): Result<T, E>;
912
928
  /**
913
929
  * Creates a {@link Result} out of a promise or async callback.
930
+ *
914
931
  * @typeparam T The result's type.
915
932
  * @typeparam E The error's type.
916
933
  */
917
- function fromAsync<T, E = unknown>(op: Awaitable<Resolvable<T, E>> | (() => Awaitable<Resolvable<T, E>>)): Promise<Result<T, E>>;
934
+ static fromAsync<T, E = unknown>(this: void, op: Awaitable<ResultResolvable<T, E>> | (() => Awaitable<ResultResolvable<T, E>>)): Promise<Result<T, E>>;
918
935
  /**
919
936
  * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
920
937
  * {@link Err} encountered.
938
+ *
921
939
  * @param results An array of {@link Result}s.
922
940
  * @returns A new {@link Result}.
923
941
  */
924
- function all<T extends readonly Result<any, any>[]>(results: [...T]): Result<UnwrapOkArray<T>, UnwrapErrArray<T>[number]>;
942
+ static all<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOkArray<Entries>, UnwrapErrArray<Entries>[number]>;
925
943
  /**
926
944
  * Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
945
+ *
927
946
  * @param results An array of {@link Result}s.
928
947
  * @returns A new {@link Result}.
929
948
  */
930
- function any<T extends readonly Result<any, any>[]>(results: [...T]): Result<UnwrapOkArray<T>[number], UnwrapErrArray<T>>;
931
- const err: typeof createErr;
932
- const ok: typeof createOk;
933
- type Err<E> = ResultErr<E>;
934
- type Ok<T> = ResultOk<T>;
935
- type UnwrapOk<T extends Result<any, any>> = T extends Ok<infer S> ? S : never;
936
- type UnwrapErr<T extends Result<any, any>> = T extends Err<infer S> ? S : never;
937
- type UnwrapOkArray<T extends readonly Result<any, any>[] | []> = {
949
+ static any<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>;
950
+ /**
951
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
952
+ * whichever comes first.
953
+ *
954
+ * This function, in combination with `[$]`, is intended to emulate
955
+ * Rust's ? operator.
956
+ *
957
+ * @example
958
+ * ```typescript
959
+ * const result = Result.safeTry(function* ({ $ }) {
960
+ * const first = yield* ok(1)[$];
961
+ * const second = yield* ok(1)[$];
962
+ *
963
+ * return ok(first + second);
964
+ * });
965
+ *
966
+ * result.match({
967
+ * ok: (value) => value, // 2
968
+ * err: (error) => {}
969
+ * });
970
+ *```
971
+ *
972
+ * @example
973
+ * ```typescript
974
+ * const resultAsync = Result.safeTry(async function* ({ $async }) {
975
+ * const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
976
+ * const second = yield* ok(1)[$];
977
+ *
978
+ * return ok(first + second);
979
+ * });
980
+ *
981
+ * resultAsync.match({
982
+ * ok: (value) => value, // 2
983
+ * err: (error) => {}
984
+ * });
985
+ * ```
986
+ * @param body - What is evaluated. In body, `yield* result[$]` works as
987
+ * Rust's `result?` expression.
988
+ * @returns The first occurence of either an yielded Err or a returned Result.
989
+ */
990
+ static safeTry<T, E>(body: (options: SafeTryOptions) => Generator<Err<E>, Result<T, E>>): Result<T, E>;
991
+ /**
992
+ * Evaluates the given generator to a Result returned or an Err yielded from it,
993
+ * whichever comes first.
994
+ *
995
+ * This function, in combination with `[$]`, is intended to emulate
996
+ * Rust's ? operator.
997
+ *
998
+ * @example
999
+ * ```typescript
1000
+ * const result = Result.safeTry(function* ({ $ }) {
1001
+ * const first = yield* ok(1)[$];
1002
+ * const second = yield* ok(1)[$];
1003
+ *
1004
+ * return ok(first + second);
1005
+ * });
1006
+ *
1007
+ * result.match({
1008
+ * ok: (value) => value, // 2
1009
+ * err: (error) => {}
1010
+ * });
1011
+ *```
1012
+ *
1013
+ * @example
1014
+ * ```typescript
1015
+ * const resultAsync = Result.safeTry(async function* ({ $async }) {
1016
+ * const first = yield* $async(Result.fromAsync(() => Promise.resolve(1)));
1017
+ * const second = yield* ok(1)[$];
1018
+ *
1019
+ * return ok(first + second);
1020
+ * });
1021
+ *
1022
+ * resultAsync.match({
1023
+ * ok: (value) => value, // 2
1024
+ * err: (error) => {}
1025
+ * });
1026
+ * ```
1027
+ * @param body - What is evaluated. In body, `yield* result[$]` works as
1028
+ * Rust's `result?` expression.
1029
+ * @returns The first occurence of either an yielded Err or a returned Result.
1030
+ */
1031
+ static safeTry<T, E>(body: (options: SafeTryOptions) => AsyncGenerator<Err<E>, Result<T, E>>): Promise<Result<T, E>>;
1032
+ }
1033
+ declare namespace Result {
1034
+ type Ok<T, E = any> = Result<T, E, true>;
1035
+ type Err<E, T = any> = Result<T, E, false>;
1036
+ type Any = Result<any, any>;
1037
+ type Resolvable<T, E = any, Success extends boolean = boolean> = T | Result<T, E, Success>;
1038
+ type UnwrapOk<T extends AnyResult> = T extends Ok<infer S> ? S : never;
1039
+ type UnwrapErr<T extends AnyResult> = T extends Err<infer S> ? S : never;
1040
+ type UnwrapOkArray<T extends readonly AnyResult[] | []> = {
938
1041
  -readonly [P in keyof T]: UnwrapOk<T[P]>;
939
1042
  };
940
- type UnwrapErrArray<T extends readonly Result<any, any>[] | []> = {
1043
+ type UnwrapErrArray<T extends readonly AnyResult[] | []> = {
941
1044
  -readonly [P in keyof T]: UnwrapErr<T[P]>;
942
1045
  };
943
1046
  }
1047
+ declare const ok: typeof Result.ok;
1048
+ declare const err: typeof Result.err;
1049
+ type ResultResolvable<T, E = any, Success extends boolean = boolean> = Result.Resolvable<T, E, Success>;
1050
+ type Ok<T, E = any> = Result.Ok<T, E>;
1051
+ type Err<E, T = any> = Result.Err<E, T>;
1052
+ type AnyResult = Result.Any;
1053
+ type UnwrapOk<T extends AnyResult> = Result.UnwrapOk<T>;
1054
+ type UnwrapErr<T extends AnyResult> = Result.UnwrapErr<T>;
1055
+ type UnwrapOkArray<T extends readonly AnyResult[] | []> = Result.UnwrapOkArray<T>;
1056
+ type UnwrapErrArray<T extends readonly AnyResult[] | []> = Result.UnwrapErrArray<T>;
1057
+ interface SafeTryOptions {
1058
+ $: typeof UnwrapSafeProperty;
1059
+ $async: <T, E>(result: Promise<Result<T, E>>) => AsyncGenerator<Err<E>, T>;
1060
+ }
944
1061
 
945
- interface IOption<T> {
1062
+ declare const ValueProperty: unique symbol;
1063
+ declare const ExistsProperty: unique symbol;
1064
+ declare class Option<T, Exists extends boolean = boolean> {
1065
+ /**
1066
+ * Branded value to ensure `Success` is typed correctly.
1067
+ * @internal
1068
+ */
1069
+ protected __STATUS__: Exists;
1070
+ private readonly [ValueProperty];
1071
+ private readonly [ExistsProperty];
1072
+ private constructor();
946
1073
  /**
947
1074
  * Returns `true` if the option is a `Some` value.
948
1075
  *
@@ -959,7 +1086,7 @@ interface IOption<T> {
959
1086
  *
960
1087
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
961
1088
  */
962
- isSome(): this is OptionSome<T>;
1089
+ isSome(): this is Some<T>;
963
1090
  /**
964
1091
  * Returns `true` if the option is a `Some` and the value inside of it matches a predicate.
965
1092
  * @param cb The predicate.
@@ -982,7 +1109,8 @@ interface IOption<T> {
982
1109
  *
983
1110
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and}
984
1111
  */
985
- isSomeAnd(cb: (value: T) => boolean): boolean;
1112
+ isSomeAnd<R extends T>(cb: (value: T) => value is R): this is Some<R>;
1113
+ isSomeAnd<R extends boolean>(cb: (value: T) => R): this is Some<R> & R;
986
1114
  /**
987
1115
  * Returns `true` if the option is a `None` value.
988
1116
  *
@@ -999,7 +1127,30 @@ interface IOption<T> {
999
1127
  *
1000
1128
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
1001
1129
  */
1002
- isNone(): this is OptionNone;
1130
+ isNone(): this is None;
1131
+ /**
1132
+ * Returns `true` if the option is a `None` value or the value inside of it matches a predicate.
1133
+ *
1134
+ * @example
1135
+ * ```typescript
1136
+ * const x: Option<number> = some(2);
1137
+ * assert.equal(x.isNoneOr((x) => x > 1), true);
1138
+ * ```
1139
+ * @example
1140
+ * ```typescript
1141
+ * const x: Option<number> = some(0);
1142
+ * assert.equal(x.isNoneOr((x) => x > 1), false);
1143
+ * ```
1144
+ * @example
1145
+ * ```typescript
1146
+ * const x: Option<number> = none;
1147
+ * assert.equal(x.isNoneOr((x) => x > 1), true);
1148
+ * ```
1149
+ *
1150
+ * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none_or}
1151
+ */
1152
+ isNoneOr<R extends T>(cb: (value: T) => value is R): this is None | Some<R>;
1153
+ isNoneOr<R extends boolean>(cb: (value: T) => R): If<Exists, R, true>;
1003
1154
  /**
1004
1155
  * Returns the contained `Some` value.
1005
1156
  * @param message The message for the error.
@@ -1021,7 +1172,7 @@ interface IOption<T> {
1021
1172
  *
1022
1173
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
1023
1174
  */
1024
- expect(message: string): T;
1175
+ expect(message: string): If<Exists, T, never>;
1025
1176
  /**
1026
1177
  * Returns the contained `Some` value.
1027
1178
  *
@@ -1045,7 +1196,7 @@ interface IOption<T> {
1045
1196
  *
1046
1197
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
1047
1198
  */
1048
- unwrap(): T;
1199
+ unwrap(): If<Exists, T, never>;
1049
1200
  /**
1050
1201
  * Returns the contained `Some` value or a provided default.
1051
1202
  *
@@ -1063,7 +1214,7 @@ interface IOption<T> {
1063
1214
  *
1064
1215
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
1065
1216
  */
1066
- unwrapOr<V>(defaultValue: V): T | V;
1217
+ unwrapOr<OutputValue>(defaultValue: OutputValue): If<Exists, T, OutputValue>;
1067
1218
  /**
1068
1219
  * Returns the contained Some value or computes it from a closure.
1069
1220
  *
@@ -1078,7 +1229,7 @@ interface IOption<T> {
1078
1229
  *
1079
1230
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
1080
1231
  */
1081
- unwrapOrElse<V>(cb: () => V): T | V;
1232
+ unwrapOrElse<OutputValue>(cb: () => OutputValue): If<Exists, T, OutputValue>;
1082
1233
  /**
1083
1234
  * Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
1084
1235
  * @param cb The predicate.
@@ -1093,7 +1244,7 @@ interface IOption<T> {
1093
1244
  *
1094
1245
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
1095
1246
  */
1096
- map<U>(cb: (value: T) => U): Option<U>;
1247
+ map<U>(cb: (value: T) => U): Option<U, Exists>;
1097
1248
  /**
1098
1249
  * Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
1099
1250
  * untouched.
@@ -1116,7 +1267,7 @@ interface IOption<T> {
1116
1267
  *
1117
1268
  * @note This is an extension not supported in Rust
1118
1269
  */
1119
- mapInto<Inner>(cb: (value: T) => Option<Inner>): Option<Inner>;
1270
+ mapInto<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption;
1120
1271
  /**
1121
1272
  * Returns the provided default result (if none), or applies a function to the contained value (if any).
1122
1273
  *
@@ -1138,7 +1289,7 @@ interface IOption<T> {
1138
1289
  *
1139
1290
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
1140
1291
  */
1141
- mapOr<U>(defaultValue: U, cb: (value: T) => U): U;
1292
+ mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: T) => MappedOutputValue): If<Exists, MappedOutputValue, DefaultOutputValue>;
1142
1293
  /**
1143
1294
  * Computes a default function result (if none), or applies a different function to the contained value (if any).
1144
1295
  * @param defaultValue The default value.
@@ -1157,7 +1308,7 @@ interface IOption<T> {
1157
1308
  *
1158
1309
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
1159
1310
  */
1160
- mapOrElse<U>(defaultValue: () => U, cb: (value: T) => U): U;
1311
+ mapOrElse<OutputValue, OutputNone>(defaultValue: () => OutputNone, cb: (value: T) => OutputValue): If<Exists, OutputValue, OutputNone>;
1161
1312
  /**
1162
1313
  * Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
1163
1314
  * untouched.
@@ -1180,7 +1331,7 @@ interface IOption<T> {
1180
1331
  *
1181
1332
  * @note This is an extension not supported in Rust
1182
1333
  */
1183
- mapNoneInto<Inner>(cb: () => Option<Inner>): Option<T | Inner>;
1334
+ mapNoneInto<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
1184
1335
  /**
1185
1336
  * Calls the provided closure with a reference to the contained value (if `Some`).
1186
1337
  * @param cb The predicate.
@@ -1218,7 +1369,7 @@ interface IOption<T> {
1218
1369
  *
1219
1370
  * @note This is an extension not supported in Rust
1220
1371
  */
1221
- inspectAsync(cb: (value: T) => Awaitable<void>): Promise<this>;
1372
+ inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
1222
1373
  /**
1223
1374
  * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
1224
1375
  *
@@ -1239,7 +1390,7 @@ interface IOption<T> {
1239
1390
  *
1240
1391
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
1241
1392
  */
1242
- okOr<E>(err: E): Result<T, E>;
1393
+ okOr<ErrorValue>(error: ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
1243
1394
  /**
1244
1395
  * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
1245
1396
  * @param cb The error to be used.
@@ -1257,7 +1408,7 @@ interface IOption<T> {
1257
1408
  *
1258
1409
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
1259
1410
  */
1260
- okOrElse<E>(cb: () => E): Result<T, E>;
1411
+ okOrElse<ErrorValue>(cb: () => ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
1261
1412
  /**
1262
1413
  * Returns an iterator over the possibly contained value.
1263
1414
  *
@@ -1315,7 +1466,7 @@ interface IOption<T> {
1315
1466
  *
1316
1467
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
1317
1468
  */
1318
- and<U>(option: Option<U>): Option<U>;
1469
+ and<OutputOption extends AnyOption>(option: OutputOption): If<Exists, OutputOption, None>;
1319
1470
  /**
1320
1471
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1321
1472
  *
@@ -1335,7 +1486,7 @@ interface IOption<T> {
1335
1486
  *
1336
1487
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
1337
1488
  */
1338
- andThen<U>(cb: (value: T) => Option<U>): Option<U>;
1489
+ andThen<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption;
1339
1490
  /**
1340
1491
  * Returns the option if it contains a value, otherwise returns `option`.
1341
1492
  * @param option The option.
@@ -1367,7 +1518,7 @@ interface IOption<T> {
1367
1518
  *
1368
1519
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
1369
1520
  */
1370
- or(option: Option<T>): Option<T>;
1521
+ or<OutputOption extends AnyOption>(option: OutputOption): If<Exists, Some<T>, OutputOption>;
1371
1522
  /**
1372
1523
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1373
1524
  *
@@ -1386,7 +1537,7 @@ interface IOption<T> {
1386
1537
  *
1387
1538
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
1388
1539
  */
1389
- orElse(cb: () => Option<T>): Option<T>;
1540
+ orElse<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
1390
1541
  /**
1391
1542
  * Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
1392
1543
  * @param option The option to compare.
@@ -1418,7 +1569,7 @@ interface IOption<T> {
1418
1569
  *
1419
1570
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
1420
1571
  */
1421
- xor(option: Option<T>): Option<T>;
1572
+ xor<OtherValue, OtherExists extends boolean>(option: Option<OtherValue, OtherExists>): If<Exists, If<OtherExists, None, Some<T>>, Option<OtherValue, OtherExists>>;
1422
1573
  /**
1423
1574
  * Returns None if the option is None, otherwise calls `predicate` with the wrapped value and returns:
1424
1575
  *
@@ -1439,6 +1590,7 @@ interface IOption<T> {
1439
1590
  *
1440
1591
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.filter}
1441
1592
  */
1593
+ filter<R extends T>(predicate: (value: T) => value is R): Option<R>;
1442
1594
  filter(predicate: (value: T) => boolean): Option<T>;
1443
1595
  /**
1444
1596
  * Returns `true` if the option is a `Some` value containing the given value.
@@ -1462,7 +1614,7 @@ interface IOption<T> {
1462
1614
  *
1463
1615
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
1464
1616
  */
1465
- contains(value: T): boolean;
1617
+ contains<const Value extends T>(value: If<Exists, Value, unknown>): this is Some<Value>;
1466
1618
  /**
1467
1619
  * Zips self with another `Option`.
1468
1620
  *
@@ -1481,7 +1633,7 @@ interface IOption<T> {
1481
1633
  *
1482
1634
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
1483
1635
  */
1484
- zip<U>(other: Option<U>): Option<[T, U]>;
1636
+ zip<OtherValue, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): Option<[T, OtherValue], If<Exists, OtherExists, false>>;
1485
1637
  /**
1486
1638
  * Zips self and another `Option` with function `f`.
1487
1639
  *
@@ -1510,11 +1662,11 @@ interface IOption<T> {
1510
1662
  *
1511
1663
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
1512
1664
  */
1513
- zipWith<U, R>(other: Option<U>, f: (s: T, o: U) => R): Option<R>;
1665
+ zipWith<OtherValue, OtherExists extends boolean, ReturnValue>(other: Option<OtherValue, OtherExists>, f: (value0: T, value1: OtherValue) => ReturnValue): Option<ReturnValue, If<Exists, OtherExists, false>>;
1514
1666
  /**
1515
1667
  * Unzips an option containing a tuple of two options.
1516
1668
  *
1517
- * If self is `Some((a, b))` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1669
+ * If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1518
1670
  *
1519
1671
  * @example
1520
1672
  * ```typescript
@@ -1529,7 +1681,7 @@ interface IOption<T> {
1529
1681
  *
1530
1682
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
1531
1683
  */
1532
- unzip<Inner, U>(this: Option<readonly [Inner, U]>): [Option<Inner>, Option<U>];
1684
+ unzip<Value0, Value1, Exists extends boolean>(this: Option<readonly [Value0, Value1], Exists>): [Option<Value0, Exists>, Option<Value1, Exists>];
1533
1685
  /**
1534
1686
  * Transposes an `Option` of a `Result` into a `Result` of an `Option`.
1535
1687
  *
@@ -1544,7 +1696,7 @@ interface IOption<T> {
1544
1696
  *
1545
1697
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
1546
1698
  */
1547
- transpose<IT, E>(this: Option<Result<IT, E>>): Result<Option<IT>, E>;
1699
+ transpose<ResultValue, ResultError, ResultSuccess extends boolean, Exists extends boolean>(this: Option<Result<ResultValue, ResultError, ResultSuccess>, Exists>): If<Exists, Result<Some<ResultValue>, ResultError, ResultSuccess>, Ok<None>>;
1548
1700
  /**
1549
1701
  * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
1550
1702
  *
@@ -1566,7 +1718,7 @@ interface IOption<T> {
1566
1718
  *
1567
1719
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
1568
1720
  */
1569
- flatten<IT>(this: Option<Option<IT>>): Option<IT>;
1721
+ flatten<InnerOption extends AnyOption, Exists extends boolean>(this: Option<InnerOption, Exists>): If<Exists, InnerOption, None>;
1570
1722
  /**
1571
1723
  * Returns a `Promise` object with the awaited value (if `Some`).
1572
1724
  *
@@ -1578,21 +1730,21 @@ interface IOption<T> {
1578
1730
  *
1579
1731
  * @note This is an extension not supported in Rust
1580
1732
  */
1581
- intoPromise(): Promise<Option<Awaited<T>>>;
1733
+ intoPromise(): Promise<Option<Awaited<T>, Exists>>;
1582
1734
  /**
1583
1735
  * Checks whether or not `other` equals with self.
1584
1736
  * @param other The other option to compare.
1585
1737
  *
1586
1738
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
1587
1739
  */
1588
- eq(other: Option<T>): boolean;
1740
+ eq<OtherValue extends T, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): this is Option<OtherValue, OtherExists>;
1589
1741
  /**
1590
1742
  * Checks whether or not `other` doesn't equal with self.
1591
1743
  * @param other The other option to compare.
1592
1744
  *
1593
1745
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
1594
1746
  */
1595
- ne(other: Option<T>): boolean;
1747
+ ne(other: Option<T, boolean>): boolean;
1596
1748
  /**
1597
1749
  * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
1598
1750
  * @param branches The branches to match.
@@ -1615,9 +1767,9 @@ interface IOption<T> {
1615
1767
  * ```
1616
1768
  */
1617
1769
  match<SomeValue, NoneValue>(branches: {
1618
- some(value: T): SomeValue;
1619
- none(): NoneValue;
1620
- }): SomeValue | NoneValue;
1770
+ some(this: Some<T>, value: T): SomeValue;
1771
+ none(this: None): NoneValue;
1772
+ }): If<Exists, SomeValue, NoneValue>;
1621
1773
  /**
1622
1774
  * Returns an iterator over the possibly contained value.
1623
1775
  *
@@ -1644,214 +1796,115 @@ interface IOption<T> {
1644
1796
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1645
1797
  */
1646
1798
  [Symbol.iterator](): Generator<T>;
1647
- }
1648
-
1649
- declare class OptionSome<T> implements IOption<T> {
1650
- private readonly value;
1651
- constructor(value: T);
1652
- isSome(): this is OptionSome<T>;
1653
- isSomeAnd<R extends boolean>(cb: (value: T) => R): R;
1654
- isNone(): false;
1655
- expect(message: string): T;
1656
- unwrap(): T;
1657
- unwrapOr(defaultValue: unknown): T;
1658
- unwrapOrElse(cb: () => unknown): T;
1659
- map<U>(cb: (value: T) => U): OptionSome<U>;
1660
- mapInto<R extends Option<any>>(cb: (value: T) => R): R;
1661
- mapOr<U>(_: U, cb: (value: T) => U): U;
1662
- mapOrElse<U>(_: () => U, cb: (value: T) => U): U;
1663
- mapNoneInto(cb: () => Option<any>): this;
1664
- inspect(cb: (value: T) => void): this;
1665
- inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
1666
- okOr(err?: any): ResultOk<T>;
1667
- okOrElse(cb: () => any): ResultOk<T>;
1668
- iter(): Generator<T>;
1669
- and<R extends Option<any>>(option: R): R;
1670
- andThen<R extends Option<any>>(cb: (value: T) => R): R;
1671
- or(option: Option<any>): this;
1672
- orElse(cb?: () => Option<any>): this;
1673
- xor(option: OptionSome<T>): OptionNone;
1674
- xor(option: OptionNone): this;
1675
- xor(option: Option<T>): this | OptionNone;
1676
- filter(predicate: (value: T) => true): this;
1677
- filter(predicate: (value: T) => false): OptionNone;
1678
- filter(predicate: (value: T) => boolean): this | OptionNone;
1679
- contains(value: T): boolean;
1680
- zip(other: OptionNone): OptionNone;
1681
- zip<U>(other: OptionSome<U>): OptionSome<[T, U]>;
1682
- zip<U>(other: Option<U>): Option<[T, U]>;
1683
- zipWith<U, R>(other: OptionNone, f: (s: T, o: U) => R): OptionNone;
1684
- zipWith<U, R>(other: OptionSome<U>, f: (s: T, o: U) => R): OptionSome<R>;
1685
- zipWith<U, R>(other: Option<U>, f: (s: T, o: U) => R): Option<R>;
1686
- unzip<I, U>(this: OptionSome<readonly [I, U]>): [OptionSome<I>, OptionSome<U>];
1687
- transpose<Inner>(this: OptionSome<ResultOk<Inner>>): ResultOk<OptionSome<Inner>>;
1688
- transpose<Inner>(this: OptionSome<ResultErr<Inner>>): ResultErr<OptionSome<Inner>>;
1689
- transpose<IT, E>(this: OptionSome<Result<IT, E>>): Result<OptionSome<IT>, E>;
1690
- flatten<Inner extends Option<any>>(this: OptionSome<Inner>): Inner;
1691
- intoPromise(): Promise<OptionSome<Awaited<T>>>;
1692
- eq(other: OptionNone): false;
1693
- eq(other: Option<T>): boolean;
1694
- ne(other: OptionNone): true;
1695
- ne(other: Option<T>): boolean;
1696
- match<SomeValue, NoneValue>(branches: {
1697
- some(value: T): SomeValue;
1698
- none(): NoneValue;
1699
- }): SomeValue;
1700
- [Symbol.iterator](): Generator<T>;
1701
- }
1702
- declare function createSome<T>(value: T): OptionSome<T>;
1703
-
1704
- declare class ResultErr<E> implements IResult<any, E> {
1705
- private readonly error;
1706
- constructor(error: E);
1707
- isOk(): false;
1708
- isOkAnd(cb?: (value: never) => boolean): false;
1709
- isErr(): this is ResultErr<E>;
1710
- isErrAnd(cb: (error: E) => boolean): boolean;
1711
- ok(): OptionNone;
1712
- err(): OptionSome<E>;
1713
- map(cb?: (value: never) => unknown): this;
1714
- mapInto(cb: (value: never) => Result<any, any>): this;
1715
- mapOr<U>(defaultValue: U, cb?: (value: never) => U): U;
1716
- mapOrElse<U>(op: (error: E) => U, cb?: (value: never) => U): U;
1717
- mapErr<F>(cb: (error: E) => F): ResultErr<F>;
1718
- mapErrInto<R extends Result<any, any>>(cb: (error: E) => R): R;
1719
- inspect(cb?: (value: never) => void): this;
1720
- inspectAsync(cb?: (value: never) => Awaitable<unknown>): Promise<this>;
1721
- inspectErr(cb: (error: E) => void): this;
1722
- inspectErrAsync(cb: (error: E) => Awaitable<unknown>): Promise<this>;
1723
- iter(): Generator<never>;
1724
- expect(message: string): never;
1725
- expectErr(message?: string): E;
1726
- unwrap(): never;
1727
- unwrapErr(): E;
1728
- unwrapOr<T>(defaultValue: T): T;
1729
- unwrapOrElse<T>(op: (error: E) => T): T;
1730
- unwrapRaw(): never;
1731
- and(result?: Result<any, E>): this;
1732
- andThen(cb?: (value: never) => Result<any, E>): this;
1733
- or<R extends Result<any, any>>(result: R): R;
1734
- orElse<R extends Result<any, any>>(cb: (error: E) => R): R;
1735
- contains(value?: any): false;
1736
- containsErr(error: E): boolean;
1737
- transpose(): OptionSome<this>;
1738
- flatten(): this;
1739
- intoOkOrErr(): E;
1740
- intoPromise(): Promise<ResultErr<Awaited<E>>>;
1741
- eq(other: ResultOk<any>): false;
1742
- eq(other: Result<any, E>): boolean;
1743
- ne(other: ResultOk<any>): true;
1744
- ne(other: Result<any, E>): boolean;
1745
- match<OkValue, ErrValue>(branches: {
1746
- ok(value: never): OkValue;
1747
- err(error: E): ErrValue;
1748
- }): ErrValue;
1749
- [Symbol.iterator](): Generator<never>;
1750
- }
1751
- /**
1752
- * Creates an Err with no error.
1753
- * @return An erroneous Result.
1754
- */
1755
- declare function createErr(): ResultErr<unknown>;
1756
- /**
1757
- * Creates an Err.
1758
- * @typeparam E The error's type.
1759
- * @param x Value to use.
1760
- * @return An erroneous Result.
1761
- */
1762
- declare function createErr<E>(x: E): ResultErr<E>;
1763
-
1764
- declare class OptionNone implements IOption<any> {
1765
- isSome(): false;
1766
- isSomeAnd(cb?: (value: never) => boolean): false;
1767
- isNone(): this is OptionNone;
1768
- expect(message: string): never;
1769
- unwrap(): never;
1770
- unwrapOr<R>(defaultValue: R): R;
1771
- unwrapOrElse<R>(cb: () => R): R;
1772
- map(cb: (value: never) => any): this;
1773
- mapInto(cb: (value: never) => Option<any>): this;
1774
- mapOr<R>(defaultValue: R, cb?: (value: never) => R): R;
1775
- mapOrElse<R>(defaultValue: () => R, cb?: (value: never) => R): R;
1776
- mapNoneInto<R extends Option<any>>(cb: () => R): R;
1777
- inspect(cb?: (value: never) => void): this;
1778
- inspectAsync(cb?: (value: never) => Awaitable<unknown>): Promise<this>;
1779
- okOr<E>(error: E): ResultErr<E>;
1780
- okOrElse<E>(cb: () => E): ResultErr<E>;
1781
- iter(): Generator<never>;
1782
- and(option: Option<any>): this;
1783
- andThen(cb: (value: never) => Option<any>): this;
1784
- or<R extends Option<any>>(option: R): R;
1785
- orElse<R extends Option<any>>(cb: () => R): R;
1786
- xor<T>(option: OptionNone): OptionNone;
1787
- xor<T>(option: OptionSome<T>): OptionSome<T>;
1788
- xor<T>(option: Option<T>): OptionSome<T> | OptionNone;
1789
- filter(predicate: (value: never) => boolean): OptionNone;
1790
- contains(value?: any): false;
1791
- zip(other: Option<any>): OptionNone;
1792
- zipWith(other: Option<any>, f: (s: never, o: never) => any): OptionNone;
1793
- unzip(): [OptionNone, OptionNone];
1794
- transpose(): ResultOk<OptionNone>;
1795
- flatten(): OptionNone;
1796
- intoPromise(): Promise<OptionNone>;
1797
- eq(other: OptionNone): true;
1798
- eq(other: OptionSome<any>): false;
1799
- eq(other: Option<any>): boolean;
1800
- ne(other: OptionNone): false;
1801
- ne(other: OptionSome<any>): true;
1802
- ne(other: Option<any>): boolean;
1803
- match<SomeValue, NoneValue>(branches: {
1804
- some(value: never): SomeValue;
1805
- none(): NoneValue;
1806
- }): NoneValue;
1807
- [Symbol.iterator](): Generator<never>;
1808
- }
1809
- declare const createNone: OptionNone;
1810
-
1811
- declare class OptionError extends Error {
1812
- get name(): string;
1813
- }
1814
-
1815
- /**
1816
- * The union of the two variations of `Option`.
1817
- * @typeparam T The value's type.
1818
- */
1819
- type Option<T> = Option.Some<T> | Option.None;
1820
- declare namespace Option {
1821
- type Resolvable<T> = T | null | undefined | Option<T>;
1822
- function is<T>(value: Option<T>): true;
1823
- function is(value: any): value is Option<unknown>;
1799
+ get [Symbol.toStringTag](): If<Exists, 'Some', 'None'>;
1800
+ static readonly none: Option<any, false>;
1801
+ static some<T = undefined>(this: void, value?: T): Some<T>;
1824
1802
  /**
1825
- * Creates an {@link Option} out of a value or callback.
1803
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object. This override
1804
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
1805
+ * library or from a different build.
1806
+ *
1807
+ * @param instance The instance to check.
1808
+ * @returns Whether or not the instance is a `Option`.
1809
+ *
1810
+ * @example
1811
+ * ```typescript
1812
+ * import { Option } from '@sapphire/result';
1813
+ * const { some } = require('@sapphire/result');
1814
+ *
1815
+ * some(2) instanceof Option; // true
1816
+ * ```
1817
+ */
1818
+ static [Symbol.hasInstance](instance: unknown): boolean;
1819
+ /**
1820
+ * @deprecated Use {@link Option.isOption} instead.
1821
+ *
1822
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1823
+ *
1824
+ * @param instance The instance to check.
1825
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1826
+ *
1827
+ * @example
1828
+ * ```typescript
1829
+ * import { Option } from '@sapphire/result';
1830
+ * const { some } = require('@sapphire/result');
1831
+ *
1832
+ * Option.isOption(some(2)); // true
1833
+ * ```
1834
+ */
1835
+ static is(instance: unknown): instance is AnyOption;
1836
+ /**
1837
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1838
+ *
1839
+ * @param instance The instance to check.
1840
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1841
+ *
1842
+ * @example
1843
+ * ```typescript
1844
+ * import { Option } from '@sapphire/result';
1845
+ * const { some } = require('@sapphire/result');
1846
+ *
1847
+ * Option.isOption(some(2)); // true
1848
+ * ```
1849
+ */
1850
+ static isOption(instance: unknown): instance is AnyOption;
1851
+ /**
1852
+ * Creates a {@link Result} out of a callback.
1853
+ *
1826
1854
  * @typeparam T The result's type.
1855
+ * @typeparam E The error's type.
1827
1856
  */
1828
- function from<T>(op: Resolvable<T> | (() => Resolvable<T>)): Option<T>;
1857
+ static from<T>(this: void, op: OptionResolvable<T> | (() => OptionResolvable<T>)): Option<T>;
1829
1858
  /**
1830
- * Creates an {@link Option} out of a value or callback.
1859
+ * Creates a {@link Result} out of a promise or async callback.
1860
+ *
1831
1861
  * @typeparam T The result's type.
1862
+ * @typeparam E The error's type.
1832
1863
  */
1833
- function fromAsync<T>(op: Awaitable<Resolvable<T>> | (() => Awaitable<Resolvable<T>>)): Promise<Option<T>>;
1864
+ static fromAsync<T>(this: void, op: Awaitable<OptionResolvable<T>> | (() => Awaitable<OptionResolvable<T>>)): Promise<Option<T>>;
1834
1865
  /**
1835
- * Creates a {@link Some} that is the combination of all collected {@link Some} values as an array, or the first
1836
- * {@link None} encountered.
1837
- * @param options An array of {@link Option}s.
1838
- * @returns A new {@link Option}.
1866
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
1867
+ * {@link Err} encountered.
1868
+ *
1869
+ * @param results An array of {@link Result}s.
1870
+ * @returns A new {@link Result}.
1839
1871
  */
1840
- function all<T extends readonly Option<any>[]>(options: [...T]): Option<UnwrapSomeArray<T>>;
1872
+ static all<const Entries extends readonly AnyOption[]>(this: void, results: Entries): Option<UnwrapSomeArray<Entries>>;
1841
1873
  /**
1842
1874
  * Returns the first encountered {@link Some}, or a {@link None} if none was found.
1875
+ *
1843
1876
  * @param options An array of {@link Option}s.
1844
1877
  * @returns A new {@link Option}.
1845
1878
  */
1846
- function any<T extends readonly Option<any>[]>(options: [...T]): Option<UnwrapSomeArray<T>[number]>;
1847
- const none: OptionNone;
1848
- const some: typeof createSome;
1849
- type Some<T> = OptionSome<T>;
1850
- type None = OptionNone;
1851
- type UnwrapSome<T extends Option<any>> = T extends Some<infer S> ? S : never;
1852
- type UnwrapSomeArray<T extends readonly Option<any>[] | []> = {
1879
+ static any<const Entries extends readonly AnyOption[]>(this: void, results: Entries): Option<UnwrapSome<Entries[number]>>;
1880
+ }
1881
+ declare namespace Option {
1882
+ type Some<T> = Option<T, true>;
1883
+ type None<T = any> = Option<T, false>;
1884
+ type Any = Option<any>;
1885
+ type Resolvable<T, Exists extends boolean = boolean> = T | null | undefined | Option<T, Exists>;
1886
+ type UnwrapSome<T extends AnyOption> = T extends Some<infer S> ? S : never;
1887
+ type UnwrapSomeArray<T extends readonly AnyOption[] | []> = {
1853
1888
  -readonly [P in keyof T]: UnwrapSome<T[P]>;
1854
1889
  };
1855
1890
  }
1891
+ declare const some: typeof Option.some;
1892
+ declare const none: Option<any, false>;
1893
+ type OptionResolvable<T, Exists extends boolean = boolean> = Option.Resolvable<T, Exists>;
1894
+ type Some<T> = Option.Some<T>;
1895
+ type None<T = any> = Option.None<T>;
1896
+ type AnyOption = Option.Any;
1897
+ type UnwrapSome<T extends AnyOption> = Option.UnwrapSome<T>;
1898
+ type UnwrapSomeArray<T extends readonly AnyOption[] | []> = Option.UnwrapSomeArray<T>;
1899
+
1900
+ declare class OptionError extends Error {
1901
+ get name(): string;
1902
+ }
1903
+
1904
+ declare class ResultError<E> extends Error {
1905
+ readonly value: E;
1906
+ constructor(message: string, value: E);
1907
+ get name(): string;
1908
+ }
1856
1909
 
1857
- export { ResultErr as Err, IOption, IResult, OptionNone as None, ResultOk as Ok, Option, OptionError, Result, ResultError, OptionSome as Some, createErr as err, createNone as none, createOk as ok, createSome as some };
1910
+ export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type SafeTryOptions, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };