@sapphire/result 2.6.7-pr-725.55b1ff7b.0 → 2.7.0-next.143375f7

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,8 @@
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;
3
6
  /**
4
7
  * A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
5
8
  * type union with the variants `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error
@@ -10,7 +13,15 @@ type Awaitable<T> = PromiseLike<T> | T;
10
13
  *
11
14
  * @see {@link https://doc.rust-lang.org/std/result/index.html}
12
15
  */
13
- interface IResult<T, E> {
16
+ declare class Result<T, E, const Success extends boolean = boolean> {
17
+ /**
18
+ * Branded value to ensure `Success` is typed correctly.
19
+ * @internal
20
+ */
21
+ protected __STATUS__: Success;
22
+ private readonly [ValueProperty$1];
23
+ private readonly [SuccessProperty];
24
+ private constructor();
14
25
  /**
15
26
  * Returns `true` if the result is `Ok`.
16
27
  *
@@ -27,7 +38,7 @@ interface IResult<T, E> {
27
38
  *
28
39
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
29
40
  */
30
- isOk(): this is ResultOk<T>;
41
+ isOk(): this is Ok<T, E>;
31
42
  /**
32
43
  * Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
33
44
  *
@@ -49,7 +60,8 @@ interface IResult<T, E> {
49
60
  *
50
61
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and}
51
62
  */
52
- isOkAnd<R extends boolean>(cb: (value: T) => R): this is ResultOk<T> & R;
63
+ isOkAnd<R extends T>(cb: (value: T) => value is R): this is Ok<R, E>;
64
+ isOkAnd<R extends boolean>(cb: (value: T) => R): this is Ok<T, E> & R;
53
65
  /**
54
66
  * Returns `true` if the result is `Err`.
55
67
  *
@@ -66,7 +78,7 @@ interface IResult<T, E> {
66
78
  *
67
79
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
68
80
  */
69
- isErr(): this is ResultErr<E>;
81
+ isErr(): this is Err<E, T>;
70
82
  /**
71
83
  * Returns `true` if the result is `Err` and the value inside of it matches a predicate.
72
84
  * @param cb The predicate.
@@ -89,7 +101,8 @@ interface IResult<T, E> {
89
101
  *
90
102
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err_and}
91
103
  */
92
- isErrAnd<R extends boolean>(cb: (error: E) => R): this is ResultErr<E> & R;
104
+ isErrAnd<R extends E>(cb: (error: E) => error is R): this is Err<R, T>;
105
+ isErrAnd<R extends boolean>(cb: (error: E) => R): this is Err<E, T> & R;
93
106
  /**
94
107
  * Converts from `Result<T, E>` to `Option<T>`.
95
108
  *
@@ -108,7 +121,7 @@ interface IResult<T, E> {
108
121
  *
109
122
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
110
123
  */
111
- ok(): Option<T>;
124
+ ok(): If<Success, Some<T>, None>;
112
125
  /**
113
126
  * Converts from `Result<T, E>` to `Option<E>`.
114
127
  *
@@ -127,7 +140,7 @@ interface IResult<T, E> {
127
140
  *
128
141
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
129
142
  */
130
- err(): Option<E>;
143
+ err(): If<Success, None, Some<E>>;
131
144
  /**
132
145
  * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
133
146
  * untouched.
@@ -146,7 +159,7 @@ interface IResult<T, E> {
146
159
  *
147
160
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
148
161
  */
149
- map<U>(cb: (value: T) => U): Result<U, E>;
162
+ map<OutputValue>(cb: (value: T) => OutputValue): If<Success, Ok<OutputValue, E>, Err<E>>;
150
163
  /**
151
164
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
152
165
  * untouched.
@@ -176,7 +189,7 @@ interface IResult<T, E> {
176
189
  *
177
190
  * @note This is an extension not supported in Rust
178
191
  */
179
- mapInto<IT, IE>(cb: (value: T) => Result<IT, IE>): Result<IT, E | IE>;
192
+ mapInto<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): If<Success, OutputResult, Err<E>>;
180
193
  /**
181
194
  * Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
182
195
  *
@@ -198,7 +211,7 @@ interface IResult<T, E> {
198
211
  *
199
212
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
200
213
  */
201
- mapOr<U>(defaultValue: U, cb: (value: T) => U): U;
214
+ mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: T) => MappedOutputValue): If<Success, MappedOutputValue, DefaultOutputValue>;
202
215
  /**
203
216
  * Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
204
217
  * to a contained `Ok` value.
@@ -220,7 +233,7 @@ interface IResult<T, E> {
220
233
  *
221
234
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
222
235
  */
223
- mapOrElse<U>(op: (error: E) => U, cb: (value: T) => U): U;
236
+ mapOrElse<OutputValue, OutputError>(op: (error: E) => OutputError, cb: (value: T) => OutputValue): If<Success, OutputValue, OutputError>;
224
237
  /**
225
238
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
226
239
  * untouched.
@@ -241,7 +254,7 @@ interface IResult<T, E> {
241
254
  *
242
255
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
243
256
  */
244
- mapErr<F>(cb: (error: E) => F): Result<T, F>;
257
+ mapErr<OutputError>(cb: (error: E) => OutputError): If<Success, Ok<T>, Err<OutputError>>;
245
258
  /**
246
259
  * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
247
260
  * untouched.
@@ -270,7 +283,7 @@ interface IResult<T, E> {
270
283
  *
271
284
  * @note This is an extension not supported in Rust
272
285
  */
273
- mapErrInto<IT, IE>(cb: (error: E) => Result<IT, IE>): Result<T | IT, IE>;
286
+ mapErrInto<OutputResult extends AnyResult>(cb: (error: E) => OutputResult): If<Success, Ok<T>, OutputResult>;
274
287
  /**
275
288
  * Calls the provided closure with a reference to the contained value (if `Ok`).
276
289
  * @param cb The predicate.
@@ -289,7 +302,7 @@ interface IResult<T, E> {
289
302
  *
290
303
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
291
304
  */
292
- inspect(cb: (value: T) => void): this;
305
+ inspect(cb: (value: T) => unknown): this;
293
306
  /**
294
307
  * Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
295
308
  * @param cb The predicate.
@@ -308,7 +321,7 @@ interface IResult<T, E> {
308
321
  *
309
322
  * @note This is an extension not supported in Rust
310
323
  */
311
- inspectAsync(cb: (value: T) => Awaitable<void>): Promise<this>;
324
+ inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
312
325
  /**
313
326
  * Calls the provided closure with a reference to the contained error (if `Err`).
314
327
  * @param cb The predicate.
@@ -327,7 +340,7 @@ interface IResult<T, E> {
327
340
  *
328
341
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
329
342
  */
330
- inspectErr(cb: (error: E) => void): this;
343
+ inspectErr(cb: (error: E) => unknown): this;
331
344
  /**
332
345
  * Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
333
346
  * @param cb The predicate.
@@ -346,7 +359,7 @@ interface IResult<T, E> {
346
359
  *
347
360
  * @note This is an extension not supported in Rust
348
361
  */
349
- inspectErrAsync(cb: (error: E) => Awaitable<void>): Promise<this>;
362
+ inspectErrAsync(cb: (error: E) => Awaitable<unknown>): Promise<this>;
350
363
  /**
351
364
  * Returns an iterator over the possibly contained value.
352
365
  *
@@ -395,7 +408,7 @@ interface IResult<T, E> {
395
408
  *
396
409
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
397
410
  */
398
- expect(message: string): T;
411
+ expect(message: string): If<Success, T, never>;
399
412
  /**
400
413
  * Returns the contained `Err` value.
401
414
  *
@@ -419,7 +432,7 @@ interface IResult<T, E> {
419
432
  *
420
433
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
421
434
  */
422
- expectErr(message: string): E;
435
+ expectErr(message: string): If<Success, never, E>;
423
436
  /**
424
437
  * Returns the contained `Ok` value.
425
438
  *
@@ -446,7 +459,7 @@ interface IResult<T, E> {
446
459
  *
447
460
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
448
461
  */
449
- unwrap(): T;
462
+ unwrap(): If<Success, T, never>;
450
463
  /**
451
464
  * Returns the contained `Err` value.
452
465
  *
@@ -473,7 +486,7 @@ interface IResult<T, E> {
473
486
  *
474
487
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
475
488
  */
476
- unwrapErr(): E;
489
+ unwrapErr(): If<Success, never, E>;
477
490
  /**
478
491
  * Returns the contained `Ok` value or the provided default.
479
492
  *
@@ -499,7 +512,7 @@ interface IResult<T, E> {
499
512
  *
500
513
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
501
514
  */
502
- unwrapOr<V>(defaultValue: V): T | V;
515
+ unwrapOr<OutputValue>(defaultValue: OutputValue): If<Success, T, OutputValue>;
503
516
  /**
504
517
  * Returns the contained `Ok` value or computes it from a closure.
505
518
  * @seealso {@link unwrap}
@@ -519,7 +532,7 @@ interface IResult<T, E> {
519
532
  *
520
533
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
521
534
  */
522
- unwrapOrElse<V>(op: (error: E) => V): T | V;
535
+ unwrapOrElse<OutputValue>(op: (error: E) => OutputValue): If<Success, T, OutputValue>;
523
536
  /**
524
537
  * Returns the contained `Ok` value.
525
538
  *
@@ -544,7 +557,7 @@ interface IResult<T, E> {
544
557
  * });
545
558
  * ```
546
559
  */
547
- unwrapRaw(): T | never;
560
+ unwrapRaw(): If<Success, T, never>;
548
561
  /**
549
562
  * Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
550
563
  * @param result The result to check.
@@ -570,7 +583,7 @@ interface IResult<T, E> {
570
583
  *
571
584
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
572
585
  */
573
- and<U>(result: Result<U, E>): Result<U, E>;
586
+ and<OutputResult extends AnyResult>(result: OutputResult): If<Success, OutputResult, Err<E>>;
574
587
  /**
575
588
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
576
589
  *
@@ -590,7 +603,7 @@ interface IResult<T, E> {
590
603
  *
591
604
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
592
605
  */
593
- andThen<U>(cb: (value: T) => Result<U, E>): Result<U, E>;
606
+ andThen<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): If<Success, OutputResult, Err<E>>;
594
607
  /**
595
608
  * Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
596
609
  *
@@ -625,7 +638,7 @@ interface IResult<T, E> {
625
638
  *
626
639
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
627
640
  */
628
- or<F>(result: Result<T, F>): Result<T, F>;
641
+ or<OutputResult extends AnyResult>(result: OutputResult): If<Success, Ok<T>, OutputResult>;
629
642
  /**
630
643
  * Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
631
644
  *
@@ -645,7 +658,7 @@ interface IResult<T, E> {
645
658
  *
646
659
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
647
660
  */
648
- orElse<F>(cb: (error: E) => Result<T, F>): Result<T, F>;
661
+ orElse<OutputResult extends AnyResult>(cb: (error: E) => OutputResult): If<Success, Ok<T>, OutputResult>;
649
662
  /**
650
663
  * Returns `true` if the result is an `Ok` and the given value strict equals it.
651
664
  * @param value The value to compare.
@@ -668,7 +681,8 @@ interface IResult<T, E> {
668
681
  *
669
682
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains}
670
683
  */
671
- contains(value: T): boolean;
684
+ contains<const Value extends T>(this: Ok<T>, value: Value): this is Ok<Value>;
685
+ contains(this: Err<E>, value: T): false;
672
686
  /**
673
687
  * Returns `true` if the result is an `Err` and the given error strict equals it.
674
688
  * @param error The error to compare.
@@ -691,7 +705,8 @@ interface IResult<T, E> {
691
705
  *
692
706
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains_err}
693
707
  */
694
- containsErr(error: E): boolean;
708
+ containsErr(this: Ok<T>, error: E): false;
709
+ containsErr<const Value extends E>(this: Err<E>, error: Value): this is Err<Value>;
695
710
  /**
696
711
  * Transposes a `Result` of an `Option` into an `Option` of a `Result`.
697
712
  *
@@ -706,7 +721,7 @@ interface IResult<T, E> {
706
721
  *
707
722
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
708
723
  */
709
- transpose<IT>(this: Result<Option<IT>, E>): Option<Result<IT, E>>;
724
+ transpose<InnerValue>(this: Result<Option<InnerValue>, E, Success>): If<Success, Option<Ok<InnerValue>>, Some<Err<E>>>;
710
725
  /**
711
726
  * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
712
727
  *
@@ -728,7 +743,7 @@ interface IResult<T, E> {
728
743
  *
729
744
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
730
745
  */
731
- flatten<IT>(this: Result<Result<IT, E>, E>): Result<IT, E>;
746
+ flatten<InnerResult extends AnyResult>(this: Result<InnerResult, E, Success>): If<Success, InnerResult, Err<E>>;
732
747
  /**
733
748
  * Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
734
749
  *
@@ -745,7 +760,7 @@ interface IResult<T, E> {
745
760
  *
746
761
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
747
762
  */
748
- intoOkOrErr(): T | E;
763
+ intoOkOrErr(): If<Success, T, E>;
749
764
  /**
750
765
  * Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
751
766
  *
@@ -757,14 +772,14 @@ interface IResult<T, E> {
757
772
  *
758
773
  * @note This is an extension not supported in Rust
759
774
  */
760
- intoPromise(): Promise<Result<Awaited<T>, Awaited<E>>>;
775
+ intoPromise(): If<Success, Promise<Ok<Awaited<T>>>, Promise<Err<Awaited<E>>>>;
761
776
  /**
762
777
  * Checks whether or not `other` equals with self.
763
778
  * @param other The other result to compare.
764
779
  *
765
780
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
766
781
  */
767
- eq(other: Result<T, E>): boolean;
782
+ eq<OtherValue extends T, OtherError extends E, OtherSuccess extends boolean>(other: Result<OtherValue, OtherError, OtherSuccess>): this is Result<OtherValue, OtherError, OtherSuccess>;
768
783
  /**
769
784
  * Checks whether or not `other` doesn't equal with self.
770
785
  * @param other The other result to compare.
@@ -794,9 +809,9 @@ interface IResult<T, E> {
794
809
  * ```
795
810
  */
796
811
  match<OkValue, ErrValue>(branches: {
797
- ok(value: T): OkValue;
798
- err(error: E): ErrValue;
799
- }): OkValue | ErrValue;
812
+ ok(this: Ok<T>, value: T): OkValue;
813
+ err(this: Err<E>, error: E): ErrValue;
814
+ }): If<Success, OkValue, ErrValue>;
800
815
  /**
801
816
  * Returns an iterator over the possibly contained value.
802
817
  *
@@ -823,126 +838,124 @@ interface IResult<T, E> {
823
838
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
824
839
  */
825
840
  [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>;
841
+ get [Symbol.toStringTag](): If<Success, 'Ok', 'Err'>;
842
+ static ok<T, E = any>(this: void, value: T): Ok<T, E>;
843
+ static err<E, T = any>(this: void, value: E): Err<E, T>;
844
+ /**
845
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object. This override
846
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
847
+ * library or from a different build.
848
+ *
849
+ * @param instance The instance to check.
850
+ * @returns Whether or not the instance is a `Result`.
851
+ *
852
+ * @example
853
+ * ```typescript
854
+ * import { Result } from '@sapphire/result';
855
+ * const { ok } = require('@sapphire/result');
856
+ *
857
+ * ok(2) instanceof Result; // true
858
+ * ```
859
+ */
860
+ static [Symbol.hasInstance](instance: unknown): boolean;
861
+ /**
862
+ * @deprecated Use {@link Result.isResult} instead.
863
+ *
864
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
865
+ *
866
+ * @param instance The instance to check.
867
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
868
+ *
869
+ * @example
870
+ * ```typescript
871
+ * import { Result } from '@sapphire/result';
872
+ * const { ok } = require('@sapphire/result');
873
+ *
874
+ * Result.isResult(ok(2)); // true
875
+ * ```
876
+ */
877
+ static is(instance: unknown): instance is AnyResult;
878
+ /**
879
+ * Checks if the `instance` object is an instance of `Result`, or if it is a `Result`-like object.
880
+ *
881
+ * @param instance The instance to check.
882
+ * @returns true if the instance is a `Result` or a `Result`-like object, false otherwise.
883
+ *
884
+ * @example
885
+ * ```typescript
886
+ * import { Result } from '@sapphire/result';
887
+ * const { ok } = require('@sapphire/result');
888
+ *
889
+ * Result.isResult(ok(2)); // true
890
+ * ```
891
+ */
892
+ static isResult(instance: unknown): instance is AnyResult;
906
893
  /**
907
894
  * Creates a {@link Result} out of a callback.
895
+ *
908
896
  * @typeparam T The result's type.
909
897
  * @typeparam E The error's type.
910
898
  */
911
- function from<T, E = unknown>(op: Resolvable<T, E> | (() => Resolvable<T, E>)): Result<T, E>;
899
+ static from<T, E = unknown>(this: void, op: ResultResolvable<T, E> | (() => ResultResolvable<T, E>)): Result<T, E>;
912
900
  /**
913
901
  * Creates a {@link Result} out of a promise or async callback.
902
+ *
914
903
  * @typeparam T The result's type.
915
904
  * @typeparam E The error's type.
916
905
  */
917
- function fromAsync<T, E = unknown>(op: Awaitable<Resolvable<T, E>> | (() => Awaitable<Resolvable<T, E>>)): Promise<Result<T, E>>;
906
+ static fromAsync<T, E = unknown>(this: void, op: Awaitable<ResultResolvable<T, E>> | (() => Awaitable<ResultResolvable<T, E>>)): Promise<Result<T, E>>;
918
907
  /**
919
908
  * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
920
909
  * {@link Err} encountered.
910
+ *
921
911
  * @param results An array of {@link Result}s.
922
912
  * @returns A new {@link Result}.
923
913
  */
924
- function all<T extends readonly Result<any, any>[]>(results: [...T]): Result<UnwrapOkArray<T>, UnwrapErrArray<T>[number]>;
914
+ static all<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOkArray<Entries>, UnwrapErrArray<Entries>[number]>;
925
915
  /**
926
916
  * Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
917
+ *
927
918
  * @param results An array of {@link Result}s.
928
919
  * @returns A new {@link Result}.
929
920
  */
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>[] | []> = {
921
+ static any<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOk<Entries[number]>, UnwrapErrArray<Entries>>;
922
+ }
923
+ declare namespace Result {
924
+ type Ok<T, E = any> = Result<T, E, true>;
925
+ type Err<E, T = any> = Result<T, E, false>;
926
+ type Any = Result<any, any>;
927
+ type Resolvable<T, E = any, Success extends boolean = boolean> = T | Result<T, E, Success>;
928
+ type UnwrapOk<T extends AnyResult> = T extends Ok<infer S> ? S : never;
929
+ type UnwrapErr<T extends AnyResult> = T extends Err<infer S> ? S : never;
930
+ type UnwrapOkArray<T extends readonly AnyResult[] | []> = {
938
931
  -readonly [P in keyof T]: UnwrapOk<T[P]>;
939
932
  };
940
- type UnwrapErrArray<T extends readonly Result<any, any>[] | []> = {
933
+ type UnwrapErrArray<T extends readonly AnyResult[] | []> = {
941
934
  -readonly [P in keyof T]: UnwrapErr<T[P]>;
942
935
  };
943
936
  }
937
+ declare const ok: typeof Result.ok;
938
+ declare const err: typeof Result.err;
939
+ type ResultResolvable<T, E = any, Success extends boolean = boolean> = Result.Resolvable<T, E, Success>;
940
+ type Ok<T, E = any> = Result.Ok<T, E>;
941
+ type Err<E, T = any> = Result.Err<E, T>;
942
+ type AnyResult = Result.Any;
943
+ type UnwrapOk<T extends AnyResult> = Result.UnwrapOk<T>;
944
+ type UnwrapErr<T extends AnyResult> = Result.UnwrapErr<T>;
945
+ type UnwrapOkArray<T extends readonly AnyResult[] | []> = Result.UnwrapOkArray<T>;
946
+ type UnwrapErrArray<T extends readonly AnyResult[] | []> = Result.UnwrapErrArray<T>;
944
947
 
945
- interface IOption<T> {
948
+ declare const ValueProperty: unique symbol;
949
+ declare const ExistsProperty: unique symbol;
950
+ declare class Option<T, Exists extends boolean = boolean> {
951
+ /**
952
+ * Branded value to ensure `Success` is typed correctly.
953
+ * @internal
954
+ */
955
+ protected __STATUS__: Exists;
956
+ private readonly [ValueProperty];
957
+ private readonly [ExistsProperty];
958
+ private constructor();
946
959
  /**
947
960
  * Returns `true` if the option is a `Some` value.
948
961
  *
@@ -959,7 +972,7 @@ interface IOption<T> {
959
972
  *
960
973
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
961
974
  */
962
- isSome(): this is OptionSome<T>;
975
+ isSome(): this is Some<T>;
963
976
  /**
964
977
  * Returns `true` if the option is a `Some` and the value inside of it matches a predicate.
965
978
  * @param cb The predicate.
@@ -982,7 +995,8 @@ interface IOption<T> {
982
995
  *
983
996
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and}
984
997
  */
985
- isSomeAnd(cb: (value: T) => boolean): boolean;
998
+ isSomeAnd<R extends T>(cb: (value: T) => value is R): this is Some<R>;
999
+ isSomeAnd<R extends boolean>(cb: (value: T) => R): this is Some<R> & R;
986
1000
  /**
987
1001
  * Returns `true` if the option is a `None` value.
988
1002
  *
@@ -999,7 +1013,7 @@ interface IOption<T> {
999
1013
  *
1000
1014
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
1001
1015
  */
1002
- isNone(): this is OptionNone;
1016
+ isNone(): this is None;
1003
1017
  /**
1004
1018
  * Returns the contained `Some` value.
1005
1019
  * @param message The message for the error.
@@ -1021,7 +1035,7 @@ interface IOption<T> {
1021
1035
  *
1022
1036
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
1023
1037
  */
1024
- expect(message: string): T;
1038
+ expect(message: string): If<Exists, T, never>;
1025
1039
  /**
1026
1040
  * Returns the contained `Some` value.
1027
1041
  *
@@ -1045,7 +1059,7 @@ interface IOption<T> {
1045
1059
  *
1046
1060
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
1047
1061
  */
1048
- unwrap(): T;
1062
+ unwrap(): If<Exists, T, never>;
1049
1063
  /**
1050
1064
  * Returns the contained `Some` value or a provided default.
1051
1065
  *
@@ -1063,7 +1077,7 @@ interface IOption<T> {
1063
1077
  *
1064
1078
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
1065
1079
  */
1066
- unwrapOr<V>(defaultValue: V): T | V;
1080
+ unwrapOr<OutputValue>(defaultValue: OutputValue): If<Exists, T, OutputValue>;
1067
1081
  /**
1068
1082
  * Returns the contained Some value or computes it from a closure.
1069
1083
  *
@@ -1078,7 +1092,7 @@ interface IOption<T> {
1078
1092
  *
1079
1093
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
1080
1094
  */
1081
- unwrapOrElse<V>(cb: () => V): T | V;
1095
+ unwrapOrElse<OutputValue>(cb: () => OutputValue): If<Exists, T, OutputValue>;
1082
1096
  /**
1083
1097
  * Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
1084
1098
  * @param cb The predicate.
@@ -1093,7 +1107,7 @@ interface IOption<T> {
1093
1107
  *
1094
1108
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
1095
1109
  */
1096
- map<U>(cb: (value: T) => U): Option<U>;
1110
+ map<U>(cb: (value: T) => U): If<Exists, Some<U>, None>;
1097
1111
  /**
1098
1112
  * Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
1099
1113
  * untouched.
@@ -1116,7 +1130,7 @@ interface IOption<T> {
1116
1130
  *
1117
1131
  * @note This is an extension not supported in Rust
1118
1132
  */
1119
- mapInto<Inner>(cb: (value: T) => Option<Inner>): Option<Inner>;
1133
+ mapInto<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None>;
1120
1134
  /**
1121
1135
  * Returns the provided default result (if none), or applies a function to the contained value (if any).
1122
1136
  *
@@ -1138,7 +1152,7 @@ interface IOption<T> {
1138
1152
  *
1139
1153
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
1140
1154
  */
1141
- mapOr<U>(defaultValue: U, cb: (value: T) => U): U;
1155
+ mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: T) => MappedOutputValue): If<Exists, MappedOutputValue, DefaultOutputValue>;
1142
1156
  /**
1143
1157
  * Computes a default function result (if none), or applies a different function to the contained value (if any).
1144
1158
  * @param defaultValue The default value.
@@ -1157,7 +1171,7 @@ interface IOption<T> {
1157
1171
  *
1158
1172
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
1159
1173
  */
1160
- mapOrElse<U>(defaultValue: () => U, cb: (value: T) => U): U;
1174
+ mapOrElse<OutputValue, OutputNone>(defaultValue: () => OutputNone, cb: (value: T) => OutputValue): If<Exists, OutputValue, OutputNone>;
1161
1175
  /**
1162
1176
  * Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
1163
1177
  * untouched.
@@ -1180,7 +1194,7 @@ interface IOption<T> {
1180
1194
  *
1181
1195
  * @note This is an extension not supported in Rust
1182
1196
  */
1183
- mapNoneInto<Inner>(cb: () => Option<Inner>): Option<T | Inner>;
1197
+ mapNoneInto<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
1184
1198
  /**
1185
1199
  * Calls the provided closure with a reference to the contained value (if `Some`).
1186
1200
  * @param cb The predicate.
@@ -1218,7 +1232,7 @@ interface IOption<T> {
1218
1232
  *
1219
1233
  * @note This is an extension not supported in Rust
1220
1234
  */
1221
- inspectAsync(cb: (value: T) => Awaitable<void>): Promise<this>;
1235
+ inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
1222
1236
  /**
1223
1237
  * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
1224
1238
  *
@@ -1239,7 +1253,7 @@ interface IOption<T> {
1239
1253
  *
1240
1254
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
1241
1255
  */
1242
- okOr<E>(err: E): Result<T, E>;
1256
+ okOr<ErrorValue>(error: ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
1243
1257
  /**
1244
1258
  * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
1245
1259
  * @param cb The error to be used.
@@ -1257,7 +1271,7 @@ interface IOption<T> {
1257
1271
  *
1258
1272
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
1259
1273
  */
1260
- okOrElse<E>(cb: () => E): Result<T, E>;
1274
+ okOrElse<ErrorValue>(cb: () => ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
1261
1275
  /**
1262
1276
  * Returns an iterator over the possibly contained value.
1263
1277
  *
@@ -1315,7 +1329,7 @@ interface IOption<T> {
1315
1329
  *
1316
1330
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
1317
1331
  */
1318
- and<U>(option: Option<U>): Option<U>;
1332
+ and<OutputOption extends AnyOption>(option: OutputOption): If<Exists, OutputOption, None>;
1319
1333
  /**
1320
1334
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1321
1335
  *
@@ -1335,7 +1349,7 @@ interface IOption<T> {
1335
1349
  *
1336
1350
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
1337
1351
  */
1338
- andThen<U>(cb: (value: T) => Option<U>): Option<U>;
1352
+ andThen<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): If<Exists, OutputOption, None>;
1339
1353
  /**
1340
1354
  * Returns the option if it contains a value, otherwise returns `option`.
1341
1355
  * @param option The option.
@@ -1367,7 +1381,7 @@ interface IOption<T> {
1367
1381
  *
1368
1382
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
1369
1383
  */
1370
- or(option: Option<T>): Option<T>;
1384
+ or<OutputOption extends AnyOption>(option: OutputOption): If<Exists, Some<T>, OutputOption>;
1371
1385
  /**
1372
1386
  * Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
1373
1387
  *
@@ -1386,7 +1400,7 @@ interface IOption<T> {
1386
1400
  *
1387
1401
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
1388
1402
  */
1389
- orElse(cb: () => Option<T>): Option<T>;
1403
+ orElse<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
1390
1404
  /**
1391
1405
  * Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
1392
1406
  * @param option The option to compare.
@@ -1418,7 +1432,7 @@ interface IOption<T> {
1418
1432
  *
1419
1433
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
1420
1434
  */
1421
- xor(option: Option<T>): Option<T>;
1435
+ xor<OtherValue, OtherExists extends boolean>(option: Option<OtherValue, OtherExists>): If<Exists, If<OtherExists, None, Some<T>>, Option<OtherValue, OtherExists>>;
1422
1436
  /**
1423
1437
  * Returns None if the option is None, otherwise calls `predicate` with the wrapped value and returns:
1424
1438
  *
@@ -1439,6 +1453,7 @@ interface IOption<T> {
1439
1453
  *
1440
1454
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.filter}
1441
1455
  */
1456
+ filter<R extends T>(predicate: (value: T) => value is R): Option<R>;
1442
1457
  filter(predicate: (value: T) => boolean): Option<T>;
1443
1458
  /**
1444
1459
  * Returns `true` if the option is a `Some` value containing the given value.
@@ -1462,7 +1477,7 @@ interface IOption<T> {
1462
1477
  *
1463
1478
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
1464
1479
  */
1465
- contains(value: T): boolean;
1480
+ contains<const Value extends T>(value: If<Exists, Value, unknown>): this is Some<Value>;
1466
1481
  /**
1467
1482
  * Zips self with another `Option`.
1468
1483
  *
@@ -1481,7 +1496,7 @@ interface IOption<T> {
1481
1496
  *
1482
1497
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
1483
1498
  */
1484
- zip<U>(other: Option<U>): Option<[T, U]>;
1499
+ zip<OtherValue, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): Option<[T, OtherValue], If<Exists, OtherExists, false>>;
1485
1500
  /**
1486
1501
  * Zips self and another `Option` with function `f`.
1487
1502
  *
@@ -1510,11 +1525,11 @@ interface IOption<T> {
1510
1525
  *
1511
1526
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
1512
1527
  */
1513
- zipWith<U, R>(other: Option<U>, f: (s: T, o: U) => R): Option<R>;
1528
+ zipWith<OtherValue, OtherExists extends boolean, ReturnValue>(other: Option<OtherValue, OtherExists>, f: (value0: T, value1: OtherValue) => ReturnValue): Option<ReturnValue, If<Exists, OtherExists, false>>;
1514
1529
  /**
1515
1530
  * Unzips an option containing a tuple of two options.
1516
1531
  *
1517
- * If self is `Some((a, b))` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1532
+ * If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
1518
1533
  *
1519
1534
  * @example
1520
1535
  * ```typescript
@@ -1529,7 +1544,7 @@ interface IOption<T> {
1529
1544
  *
1530
1545
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
1531
1546
  */
1532
- unzip<Inner, U>(this: Option<readonly [Inner, U]>): [Option<Inner>, Option<U>];
1547
+ unzip<Value0, Value1, Exists extends boolean>(this: Option<readonly [Value0, Value1], Exists>): [Option<Value0, Exists>, Option<Value1, Exists>];
1533
1548
  /**
1534
1549
  * Transposes an `Option` of a `Result` into a `Result` of an `Option`.
1535
1550
  *
@@ -1544,7 +1559,7 @@ interface IOption<T> {
1544
1559
  *
1545
1560
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
1546
1561
  */
1547
- transpose<IT, E>(this: Option<Result<IT, E>>): Result<Option<IT>, E>;
1562
+ 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
1563
  /**
1549
1564
  * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
1550
1565
  *
@@ -1566,7 +1581,7 @@ interface IOption<T> {
1566
1581
  *
1567
1582
  * @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
1568
1583
  */
1569
- flatten<IT>(this: Option<Option<IT>>): Option<IT>;
1584
+ flatten<InnerOption extends AnyOption, Exists extends boolean>(this: Option<InnerOption, Exists>): If<Exists, InnerOption, None>;
1570
1585
  /**
1571
1586
  * Returns a `Promise` object with the awaited value (if `Some`).
1572
1587
  *
@@ -1578,21 +1593,21 @@ interface IOption<T> {
1578
1593
  *
1579
1594
  * @note This is an extension not supported in Rust
1580
1595
  */
1581
- intoPromise(): Promise<Option<Awaited<T>>>;
1596
+ intoPromise(): Promise<Option<Awaited<T>, Exists>>;
1582
1597
  /**
1583
1598
  * Checks whether or not `other` equals with self.
1584
1599
  * @param other The other option to compare.
1585
1600
  *
1586
1601
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
1587
1602
  */
1588
- eq(other: Option<T>): boolean;
1603
+ eq<OtherValue extends T, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): this is Option<OtherValue, OtherExists>;
1589
1604
  /**
1590
1605
  * Checks whether or not `other` doesn't equal with self.
1591
1606
  * @param other The other option to compare.
1592
1607
  *
1593
1608
  * @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
1594
1609
  */
1595
- ne(other: Option<T>): boolean;
1610
+ ne(other: Option<T, boolean>): boolean;
1596
1611
  /**
1597
1612
  * Runs `ok` function if self is `Ok`, otherwise runs `err` function.
1598
1613
  * @param branches The branches to match.
@@ -1615,9 +1630,9 @@ interface IOption<T> {
1615
1630
  * ```
1616
1631
  */
1617
1632
  match<SomeValue, NoneValue>(branches: {
1618
- some(value: T): SomeValue;
1619
- none(): NoneValue;
1620
- }): SomeValue | NoneValue;
1633
+ some(this: Some<T>, value: T): SomeValue;
1634
+ none(this: None): NoneValue;
1635
+ }): If<Exists, SomeValue, NoneValue>;
1621
1636
  /**
1622
1637
  * Returns an iterator over the possibly contained value.
1623
1638
  *
@@ -1644,214 +1659,115 @@ interface IOption<T> {
1644
1659
  * @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
1645
1660
  */
1646
1661
  [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>;
1662
+ get [Symbol.toStringTag](): If<Exists, 'Some', 'None'>;
1663
+ static readonly none: Option<any, false>;
1664
+ static some<T>(this: void, value: T): Some<T>;
1665
+ /**
1666
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object. This override
1667
+ * exists to interoperate with other versions of this class, such as the one coming from another version of this
1668
+ * library or from a different build.
1669
+ *
1670
+ * @param instance The instance to check.
1671
+ * @returns Whether or not the instance is a `Option`.
1672
+ *
1673
+ * @example
1674
+ * ```typescript
1675
+ * import { Option } from '@sapphire/result';
1676
+ * const { some } = require('@sapphire/result');
1677
+ *
1678
+ * some(2) instanceof Option; // true
1679
+ * ```
1680
+ */
1681
+ static [Symbol.hasInstance](instance: unknown): boolean;
1824
1682
  /**
1825
- * Creates an {@link Option} out of a value or callback.
1683
+ * @deprecated Use {@link Option.isOption} instead.
1684
+ *
1685
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1686
+ *
1687
+ * @param instance The instance to check.
1688
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1689
+ *
1690
+ * @example
1691
+ * ```typescript
1692
+ * import { Option } from '@sapphire/result';
1693
+ * const { some } = require('@sapphire/result');
1694
+ *
1695
+ * Option.isOption(some(2)); // true
1696
+ * ```
1697
+ */
1698
+ static is(instance: unknown): instance is AnyOption;
1699
+ /**
1700
+ * Checks if the `instance` object is an instance of `Option`, or if it is a `Option`-like object.
1701
+ *
1702
+ * @param instance The instance to check.
1703
+ * @returns true if the instance is a `Option` or a `Option`-like object, false otherwise.
1704
+ *
1705
+ * @example
1706
+ * ```typescript
1707
+ * import { Option } from '@sapphire/result';
1708
+ * const { some } = require('@sapphire/result');
1709
+ *
1710
+ * Option.isOption(some(2)); // true
1711
+ * ```
1712
+ */
1713
+ static isOption(instance: unknown): instance is AnyOption;
1714
+ /**
1715
+ * Creates a {@link Result} out of a callback.
1716
+ *
1826
1717
  * @typeparam T The result's type.
1718
+ * @typeparam E The error's type.
1827
1719
  */
1828
- function from<T>(op: Resolvable<T> | (() => Resolvable<T>)): Option<T>;
1720
+ static from<T>(this: void, op: OptionResolvable<T> | (() => OptionResolvable<T>)): Option<T>;
1829
1721
  /**
1830
- * Creates an {@link Option} out of a value or callback.
1722
+ * Creates a {@link Result} out of a promise or async callback.
1723
+ *
1831
1724
  * @typeparam T The result's type.
1725
+ * @typeparam E The error's type.
1832
1726
  */
1833
- function fromAsync<T>(op: Awaitable<Resolvable<T>> | (() => Awaitable<Resolvable<T>>)): Promise<Option<T>>;
1727
+ static fromAsync<T>(this: void, op: Awaitable<OptionResolvable<T>> | (() => Awaitable<OptionResolvable<T>>)): Promise<Option<T>>;
1834
1728
  /**
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}.
1729
+ * Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
1730
+ * {@link Err} encountered.
1731
+ *
1732
+ * @param results An array of {@link Result}s.
1733
+ * @returns A new {@link Result}.
1839
1734
  */
1840
- function all<T extends readonly Option<any>[]>(options: [...T]): Option<UnwrapSomeArray<T>>;
1735
+ static all<const Entries extends readonly AnyOption[]>(this: void, results: Entries): Option<UnwrapSomeArray<Entries>>;
1841
1736
  /**
1842
1737
  * Returns the first encountered {@link Some}, or a {@link None} if none was found.
1738
+ *
1843
1739
  * @param options An array of {@link Option}s.
1844
1740
  * @returns A new {@link Option}.
1845
1741
  */
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>[] | []> = {
1742
+ static any<const Entries extends readonly AnyOption[]>(this: void, results: Entries): Option<UnwrapSome<Entries[number]>>;
1743
+ }
1744
+ declare namespace Option {
1745
+ type Some<T> = Option<T, true>;
1746
+ type None<T = any> = Option<T, false>;
1747
+ type Any = Option<any>;
1748
+ type Resolvable<T, Exists extends boolean = boolean> = T | null | undefined | Option<T, Exists>;
1749
+ type UnwrapSome<T extends AnyOption> = T extends Some<infer S> ? S : never;
1750
+ type UnwrapSomeArray<T extends readonly AnyOption[] | []> = {
1853
1751
  -readonly [P in keyof T]: UnwrapSome<T[P]>;
1854
1752
  };
1855
1753
  }
1754
+ declare const some: typeof Option.some;
1755
+ declare const none: Option<any, false>;
1756
+ type OptionResolvable<T, Exists extends boolean = boolean> = Option.Resolvable<T, Exists>;
1757
+ type Some<T> = Option.Some<T>;
1758
+ type None<T = any> = Option.None<T>;
1759
+ type AnyOption = Option.Any;
1760
+ type UnwrapSome<T extends AnyOption> = Option.UnwrapSome<T>;
1761
+ type UnwrapSomeArray<T extends readonly AnyOption[] | []> = Option.UnwrapSomeArray<T>;
1762
+
1763
+ declare class OptionError extends Error {
1764
+ get name(): string;
1765
+ }
1766
+
1767
+ declare class ResultError<E> extends Error {
1768
+ readonly value: E;
1769
+ constructor(message: string, value: E);
1770
+ get name(): string;
1771
+ }
1856
1772
 
1857
- export { ResultErr as Err, type IOption, type 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 };
1773
+ export { type AnyOption, type AnyResult, type Err, type None, type Ok, Option, OptionError, type OptionResolvable, Result, ResultError, type ResultResolvable, type Some, type UnwrapErr, type UnwrapErrArray, type UnwrapOk, type UnwrapOkArray, type UnwrapSome, type UnwrapSomeArray, err, none, ok, some };