@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.
- package/CHANGELOG.md +69 -0
- package/README.md +3 -2
- package/dist/cjs/index.cjs +1947 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{index.d.ts → cjs/index.d.cts} +411 -358
- package/dist/esm/index.d.mts +1910 -0
- package/dist/esm/index.mjs +1938 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/iife/index.global.js +1952 -0
- package/dist/iife/index.global.js.map +1 -0
- package/package.json +28 -24
- package/dist/index.global.js +0 -660
- package/dist/index.global.js.map +0 -1
- package/dist/index.js +0 -657
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -650
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,1910 @@
|
|
|
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;
|
|
3
|
+
|
|
4
|
+
declare const ValueProperty$1: unique symbol;
|
|
5
|
+
declare const SuccessProperty: unique symbol;
|
|
6
|
+
declare const UnwrapSafeProperty: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
|
|
9
|
+
* type union with the variants `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error
|
|
10
|
+
* and containing an error value.
|
|
11
|
+
*
|
|
12
|
+
* @typeparam T The result's type.
|
|
13
|
+
* @typeparam E The error's type.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://doc.rust-lang.org/std/result/index.html}
|
|
16
|
+
*/
|
|
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();
|
|
26
|
+
/**
|
|
27
|
+
* Returns `true` if the result is `Ok`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const x = ok(-3);
|
|
32
|
+
* assert.equal(x.isOk(), true);
|
|
33
|
+
* ```
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const x = err('Some error message');
|
|
37
|
+
* assert.equal(x.isOk(), false);
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok}
|
|
41
|
+
*/
|
|
42
|
+
isOk(): this is Ok<T, E>;
|
|
43
|
+
/**
|
|
44
|
+
* Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const x = ok(2);
|
|
49
|
+
* assert.equal(x.isOkAnd((value) => value > 1), true);
|
|
50
|
+
* ```
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const x = ok(0);
|
|
54
|
+
* assert.equal(x.isOkAnd((value) => value > 1), false);
|
|
55
|
+
* ```
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const x = err('Some error message');
|
|
59
|
+
* assert.equal(x.isOkAnd((value) => value > 1), false);
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and}
|
|
63
|
+
*/
|
|
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;
|
|
66
|
+
/**
|
|
67
|
+
* Returns `true` if the result is `Err`.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const x = ok(-3);
|
|
72
|
+
* assert.equal(x.isErr(), false);
|
|
73
|
+
* ```
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const x = err('Some error message');
|
|
77
|
+
* assert.equal(x.isErr(), true);
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err}
|
|
81
|
+
*/
|
|
82
|
+
isErr(): this is Err<E, T>;
|
|
83
|
+
/**
|
|
84
|
+
* Returns `true` if the result is `Err` and the value inside of it matches a predicate.
|
|
85
|
+
* @param cb The predicate.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const x = ok(2);
|
|
90
|
+
* assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
|
|
91
|
+
* ```
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const x = err(new Error('Some error message'));
|
|
95
|
+
* assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
|
|
96
|
+
* ```
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const x = err(new TypeError('Some error message'));
|
|
100
|
+
* assert.equal(x.isErrAnd((error) => error instanceof TypeError), true);
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err_and}
|
|
104
|
+
*/
|
|
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;
|
|
107
|
+
/**
|
|
108
|
+
* Converts from `Result<T, E>` to `Option<T>`.
|
|
109
|
+
*
|
|
110
|
+
* Converts itself into an `Option<T>`, and discarding the error, if any.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const x: Result<number, string> = ok(2);
|
|
115
|
+
* assert.equal(x.ok(), some(2));
|
|
116
|
+
* ```
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const x: Result<number, string> = err('Some error message');
|
|
120
|
+
* assert.equal(x.ok(), none);
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.ok}
|
|
124
|
+
*/
|
|
125
|
+
ok(): If<Success, Some<T>, None>;
|
|
126
|
+
/**
|
|
127
|
+
* Converts from `Result<T, E>` to `Option<E>`.
|
|
128
|
+
*
|
|
129
|
+
* Converts itself into an `Option<E>`, and discarding the successful value, if any.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const x: Result<number, string> = ok(2);
|
|
134
|
+
* assert.equal(x.err(), none);
|
|
135
|
+
* ```
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const x: Result<number, string> = err('Some error message');
|
|
139
|
+
* assert.equal(x.err(), 'Some error message');
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.err}
|
|
143
|
+
*/
|
|
144
|
+
err(): If<Success, None, Some<E>>;
|
|
145
|
+
/**
|
|
146
|
+
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
147
|
+
* untouched.
|
|
148
|
+
* @param cb The predicate.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const x: Result<number, string> = ok(2);
|
|
153
|
+
* assert.equal(x.map((value) => value * 2), ok(4));
|
|
154
|
+
* ```
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const x: Result<number, string> = err('Some error message');
|
|
158
|
+
* assert.equal(x.map((value) => value * 2), err('Some error message'));
|
|
159
|
+
* ```
|
|
160
|
+
*
|
|
161
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map}
|
|
162
|
+
*/
|
|
163
|
+
map<OutputValue>(cb: (value: If<Success, T, never>) => OutputValue): Result<OutputValue, E, Success>;
|
|
164
|
+
/**
|
|
165
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
166
|
+
* untouched.
|
|
167
|
+
*
|
|
168
|
+
* Unlike {@link map}, this method does not wrap the returned value inside `Ok`, but instead, it returns the
|
|
169
|
+
* returned value.
|
|
170
|
+
* @param cb The predicate.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const x: Result<number, string> = ok(2);
|
|
175
|
+
* assert.equal(x.mapInto((value) => ok(value * value)), ok(4));
|
|
176
|
+
* ```
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const x: Result<number, string> = ok(0);
|
|
180
|
+
* assert.equal(
|
|
181
|
+
* x.mapInto((value) => (value === 0 ? err('zero is not divisible') : ok(1 / value))),
|
|
182
|
+
* err('zero is not divisible')
|
|
183
|
+
* );
|
|
184
|
+
* ```
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const x: Result<number, string> = err('Some error message');
|
|
188
|
+
* assert.equal(x.mapInto((value) => ok(4)), err('Some error message'));
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @note This is an extension not supported in Rust
|
|
192
|
+
*/
|
|
193
|
+
mapInto<OutputResult extends AnyResult>(cb: (value: If<Success, T, never>) => OutputResult): If<Success, OutputResult, Err<E>>;
|
|
194
|
+
/**
|
|
195
|
+
* Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`),
|
|
196
|
+
*
|
|
197
|
+
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
198
|
+
* recommended to use `mapOrElse`, which is lazily evaluated.
|
|
199
|
+
* @param defaultValue The default value to use.
|
|
200
|
+
* @param cb The predicate.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```typescript
|
|
204
|
+
* const x = ok('hello');
|
|
205
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 5);
|
|
206
|
+
* ```
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const x = err('Some error message');
|
|
210
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 42);
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or}
|
|
214
|
+
*/
|
|
215
|
+
mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: If<Success, T, never>) => MappedOutputValue): If<Success, MappedOutputValue, DefaultOutputValue>;
|
|
216
|
+
/**
|
|
217
|
+
* Maps a `Result<T, E>` to `U` by applying fallback function default to a contained `Err` value, or function `cb`
|
|
218
|
+
* to a contained `Ok` value.
|
|
219
|
+
*
|
|
220
|
+
* This function can be used to unpack a successful result while handling an error.
|
|
221
|
+
* @param op The predicate that is run on `Err`.
|
|
222
|
+
* @param cb The predicate that is run on `Ok`.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* const x: Result<string, string> = ok('hello');
|
|
227
|
+
* assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 5);
|
|
228
|
+
* ```
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const x: Result<string, string> = err('Some error message');
|
|
232
|
+
* assert.equal(x.mapOrElse((error) => error.length, (value) => value.length), 18);
|
|
233
|
+
* ```
|
|
234
|
+
*
|
|
235
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else}
|
|
236
|
+
*/
|
|
237
|
+
mapOrElse<OutputValue, OutputError>(op: (error: If<Success, never, E>) => OutputError, cb: (value: If<Success, T, never>) => OutputValue): If<Success, OutputValue, OutputError>;
|
|
238
|
+
/**
|
|
239
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
|
|
240
|
+
* untouched.
|
|
241
|
+
*
|
|
242
|
+
* This function can be used to pass through a successful result while handling an error.
|
|
243
|
+
* @param cb The predicate.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* const x: Result<number, Error> = ok(2);
|
|
248
|
+
* assert.equal(x.mapErr((error) => error.message), ok(2));
|
|
249
|
+
* ```
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
253
|
+
* assert.equal(x.mapErr((error) => error.message), err('Some error message'));
|
|
254
|
+
* ```
|
|
255
|
+
*
|
|
256
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err}
|
|
257
|
+
*/
|
|
258
|
+
mapErr<OutputError>(cb: (error: If<Success, never, E>) => OutputError): Result<T, OutputError, Success>;
|
|
259
|
+
/**
|
|
260
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value
|
|
261
|
+
* untouched.
|
|
262
|
+
*
|
|
263
|
+
* This function can be used to pass through a successful result while handling an error.
|
|
264
|
+
*
|
|
265
|
+
* Unlike {@link mapErr}, this method does not wrap the returned value inside `Err`, but instead, it returns the
|
|
266
|
+
* returned value.
|
|
267
|
+
* @param cb The predicate.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```typescript
|
|
271
|
+
* const x: Result<number, Error> = ok(2);
|
|
272
|
+
* assert.equal(x.mapErrInto((error) => err(error.message)), ok(2));
|
|
273
|
+
* ```
|
|
274
|
+
* @example
|
|
275
|
+
* ```typescript
|
|
276
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
277
|
+
* assert.equal(x.mapErrInto((error) => err(error.message)), err('Some error message'));
|
|
278
|
+
* ```
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* const x: Result<number, Error> = err(new Error('Some error message'));
|
|
282
|
+
* assert.equal(x.mapErrInto((error) => ok(4)), ok(4));
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* @note This is an extension not supported in Rust
|
|
286
|
+
*/
|
|
287
|
+
mapErrInto<OutputResult extends AnyResult>(cb: (error: If<Success, never, E>) => OutputResult): If<Success, Ok<T>, OutputResult>;
|
|
288
|
+
/**
|
|
289
|
+
* Calls the provided closure with a reference to the contained value (if `Ok`).
|
|
290
|
+
* @param cb The predicate.
|
|
291
|
+
* @seealso {@link inspectAsync} for the awaitable version.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* ok(2).inspect(console.log);
|
|
296
|
+
* // Logs: 2
|
|
297
|
+
* ```
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* err('Some error message').inspect(console.log);
|
|
301
|
+
* // Doesn't log
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect}
|
|
305
|
+
*/
|
|
306
|
+
inspect(cb: (value: T) => unknown): this;
|
|
307
|
+
/**
|
|
308
|
+
* Calls the provided closure with a reference to the contained value (if `Ok`) and awaits it.
|
|
309
|
+
* @param cb The predicate.
|
|
310
|
+
* @seealso {@link inspect} for the sync version.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```typescript
|
|
314
|
+
* await ok(2).inspectAsync(console.log);
|
|
315
|
+
* // Logs: 2
|
|
316
|
+
* ```
|
|
317
|
+
* @example
|
|
318
|
+
* ```typescript
|
|
319
|
+
* await err('Some error message').inspectAsync(console.log);
|
|
320
|
+
* // Doesn't log
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
323
|
+
* @note This is an extension not supported in Rust
|
|
324
|
+
*/
|
|
325
|
+
inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
|
|
326
|
+
/**
|
|
327
|
+
* Calls the provided closure with a reference to the contained error (if `Err`).
|
|
328
|
+
* @param cb The predicate.
|
|
329
|
+
* @seealso {@link inspectErrAsync} for the awaitable version.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* ok(2).inspectErr(console.log);
|
|
334
|
+
* // Doesn't log
|
|
335
|
+
* ```
|
|
336
|
+
* @example
|
|
337
|
+
* ```typescript
|
|
338
|
+
* err('Some error message').inspectErr(console.log);
|
|
339
|
+
* // Logs: Some error message
|
|
340
|
+
* ```
|
|
341
|
+
*
|
|
342
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err}
|
|
343
|
+
*/
|
|
344
|
+
inspectErr(cb: (error: E) => unknown): this;
|
|
345
|
+
/**
|
|
346
|
+
* Calls the provided closure with a reference to the contained error (if `Err`) and awaits it.
|
|
347
|
+
* @param cb The predicate.
|
|
348
|
+
* @seealso {@link inspectErr} for the sync version.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```typescript
|
|
352
|
+
* await ok(2).inspectErrAsync(console.log);
|
|
353
|
+
* // Doesn't log
|
|
354
|
+
* ```
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* await err('Some error message').inspectErrAsync(console.log);
|
|
358
|
+
* // Logs: Some error message
|
|
359
|
+
* ```
|
|
360
|
+
*
|
|
361
|
+
* @note This is an extension not supported in Rust
|
|
362
|
+
*/
|
|
363
|
+
inspectErrAsync(cb: (error: E) => Awaitable<unknown>): Promise<this>;
|
|
364
|
+
/**
|
|
365
|
+
* Returns an iterator over the possibly contained value.
|
|
366
|
+
*
|
|
367
|
+
* The iterator yields one value if the result is `Ok`, otherwise none.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```typescript
|
|
371
|
+
* const x = ok(7);
|
|
372
|
+
* for (const value of x.iter()) {
|
|
373
|
+
* console.log(value);
|
|
374
|
+
* }
|
|
375
|
+
* // Logs 7
|
|
376
|
+
* ```
|
|
377
|
+
* @example
|
|
378
|
+
* ```typescript
|
|
379
|
+
* const x = err('Nothing!');
|
|
380
|
+
* for (const value of x.iter()) {
|
|
381
|
+
* console.log(value);
|
|
382
|
+
* }
|
|
383
|
+
* // Doesn't log
|
|
384
|
+
* ```
|
|
385
|
+
*
|
|
386
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
|
|
387
|
+
*/
|
|
388
|
+
iter(): Generator<T>;
|
|
389
|
+
/**
|
|
390
|
+
* Returns the contained `Ok` value.
|
|
391
|
+
*
|
|
392
|
+
* If the value is an `Err`, it throws a {@link ResultError} with the given message and the content of the `Err`.
|
|
393
|
+
* @param message The message for the error.
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```typescript
|
|
397
|
+
* const x = ok(2);
|
|
398
|
+
* assert.equal(x.expect('Whoops!'), 2);
|
|
399
|
+
* ```
|
|
400
|
+
* @example
|
|
401
|
+
* ```typescript
|
|
402
|
+
* const x = err('Emergency failure');
|
|
403
|
+
* assert.throws(() => x.expect('Whoops!'), {
|
|
404
|
+
* name: 'ResultError',
|
|
405
|
+
* message: 'Whoops',
|
|
406
|
+
* value: 'Emergency failure'
|
|
407
|
+
* });
|
|
408
|
+
* ```
|
|
409
|
+
*
|
|
410
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect}
|
|
411
|
+
*/
|
|
412
|
+
expect(message: string): If<Success, T, never>;
|
|
413
|
+
/**
|
|
414
|
+
* Returns the contained `Err` value.
|
|
415
|
+
*
|
|
416
|
+
* If the value is an `Ok`, it throws a {@link ResultError} with the given message and the content of the `Ok`.
|
|
417
|
+
* @param message The message for the error.
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const x = ok(2);
|
|
422
|
+
* assert.throws(() => x.expectErr('Whoops!'), {
|
|
423
|
+
* name: 'ResultError',
|
|
424
|
+
* message: 'Whoops',
|
|
425
|
+
* value: 2
|
|
426
|
+
* });
|
|
427
|
+
* ```
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* const x = err('Emergency failure');
|
|
431
|
+
* assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
|
|
432
|
+
* ```
|
|
433
|
+
*
|
|
434
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err}
|
|
435
|
+
*/
|
|
436
|
+
expectErr(message: string): If<Success, never, E>;
|
|
437
|
+
/**
|
|
438
|
+
* Returns the contained `Ok` value.
|
|
439
|
+
*
|
|
440
|
+
* If the value is an `Err`, it throws a {@link ResultError} with the message, and the content of the `Err`.
|
|
441
|
+
* @seealso {@link unwrapOr}
|
|
442
|
+
* @seealso {@link unwrapOrElse}
|
|
443
|
+
* @seealso {@link unwrapErr}
|
|
444
|
+
* @seealso {@link unwrapRaw}
|
|
445
|
+
* @seealso {@link unwrapSafe}
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```typescript
|
|
449
|
+
* const x = ok(2);
|
|
450
|
+
* assert.equal(x.unwrap(), 2);
|
|
451
|
+
* ```
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* const x = err('Emergency failure');
|
|
455
|
+
* assert.throws(() => x.unwrap(), {
|
|
456
|
+
* name: 'ResultError',
|
|
457
|
+
* message: 'Unwrap failed',
|
|
458
|
+
* value: 'Emergency failure'
|
|
459
|
+
* });
|
|
460
|
+
* ```
|
|
461
|
+
*
|
|
462
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap}
|
|
463
|
+
*/
|
|
464
|
+
unwrap(): If<Success, T, never>;
|
|
465
|
+
/**
|
|
466
|
+
* Returns the contained `Err` value.
|
|
467
|
+
*
|
|
468
|
+
* If the value is an `Ok`, it throws a {@link ResultError} with the message, and the content of the `Ok`.
|
|
469
|
+
* @seealso {@link unwrap}
|
|
470
|
+
* @seealso {@link unwrapOr}
|
|
471
|
+
* @seealso {@link unwrapOrElse}
|
|
472
|
+
* @seealso {@link unwrapRaw}
|
|
473
|
+
* @seealso {@link unwrapSafe}
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* const x = ok(2);
|
|
478
|
+
* assert.throws(() => x.unwrapErr(), {
|
|
479
|
+
* name: 'ResultError',
|
|
480
|
+
* message: 'Unwrap failed',
|
|
481
|
+
* value: 2
|
|
482
|
+
* });
|
|
483
|
+
* ```
|
|
484
|
+
* @example
|
|
485
|
+
* ```typescript
|
|
486
|
+
* const x = err('Emergency failure');
|
|
487
|
+
* assert.equal(x.unwrapErr(), 'Emergency failure');
|
|
488
|
+
* ```
|
|
489
|
+
*
|
|
490
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err}
|
|
491
|
+
*/
|
|
492
|
+
unwrapErr(): If<Success, never, E>;
|
|
493
|
+
/**
|
|
494
|
+
* Returns the contained `Ok` value or the provided default.
|
|
495
|
+
*
|
|
496
|
+
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
497
|
+
* recommended to use {@link unwrapOrElse}, which is lazily evaluated.
|
|
498
|
+
* @seealso {@link unwrap}
|
|
499
|
+
* @seealso {@link unwrapOrElse}
|
|
500
|
+
* @seealso {@link unwrapErr}
|
|
501
|
+
* @seealso {@link unwrapRaw}
|
|
502
|
+
* @seealso {@link unwrapSafe}
|
|
503
|
+
*
|
|
504
|
+
* @param defaultValue The default value.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```typescript
|
|
508
|
+
* const x: Result<number, string> = ok(9);
|
|
509
|
+
* assert.equal(x.unwrapOr(2), 9);
|
|
510
|
+
* ```
|
|
511
|
+
* @example
|
|
512
|
+
* ```typescript
|
|
513
|
+
* const x: Result<number, string> = err('Error');
|
|
514
|
+
* assert.equal(x.unwrapOr(2), 2);
|
|
515
|
+
* ```
|
|
516
|
+
*
|
|
517
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or}
|
|
518
|
+
*/
|
|
519
|
+
unwrapOr<OutputValue>(defaultValue: OutputValue): If<Success, T, OutputValue>;
|
|
520
|
+
/**
|
|
521
|
+
* Returns the contained `Ok` value or computes it from a closure.
|
|
522
|
+
* @seealso {@link unwrap}
|
|
523
|
+
* @seealso {@link unwrapOr}
|
|
524
|
+
* @seealso {@link unwrapErr}
|
|
525
|
+
* @seealso {@link unwrapRaw}
|
|
526
|
+
* @seealso {@link unwrapSafe}
|
|
527
|
+
*
|
|
528
|
+
* @param op The predicate.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```typescript
|
|
532
|
+
* const count = (x: string) => x.length;
|
|
533
|
+
*
|
|
534
|
+
* assert.equal(ok(2).unwrapOrElse(count), 2);
|
|
535
|
+
* assert.equal(err('hello').unwrapOrElse(count), 5);
|
|
536
|
+
* ```
|
|
537
|
+
*
|
|
538
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else}
|
|
539
|
+
*/
|
|
540
|
+
unwrapOrElse<OutputValue>(op: (error: E) => OutputValue): If<Success, T, OutputValue>;
|
|
541
|
+
/**
|
|
542
|
+
* Returns the contained `Ok` value.
|
|
543
|
+
*
|
|
544
|
+
* If the value is an `Err`, it throws the contained error.
|
|
545
|
+
* @seealso {@link unwrap}
|
|
546
|
+
* @seealso {@link unwrapOr}
|
|
547
|
+
* @seealso {@link unwrapOrElse}
|
|
548
|
+
* @seealso {@link unwrapErr}
|
|
549
|
+
* @seealso {@link unwrapSafe}
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```typescript
|
|
553
|
+
* const x = ok(2);
|
|
554
|
+
* assert.equal(x.unwrapRaw(), 2);
|
|
555
|
+
* ```
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const x = err('Emergency failure');
|
|
559
|
+
* assert.throws(() => x.unwrapRaw(), {
|
|
560
|
+
* name: 'Error',
|
|
561
|
+
* message: 'Unwrap failed',
|
|
562
|
+
* value: 'Emergency failure'
|
|
563
|
+
* });
|
|
564
|
+
* ```
|
|
565
|
+
*/
|
|
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>;
|
|
581
|
+
/**
|
|
582
|
+
* Returns `result` if the result is `Ok`, otherwise returns the `Err` value of itself.
|
|
583
|
+
* @param result The result to check.
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* ```typescript
|
|
587
|
+
* const x: Result<number, string> = ok(2);
|
|
588
|
+
* const y: Result<string, string> = err('Late error');
|
|
589
|
+
* assert.equal(x.and(y), err('Late error'));
|
|
590
|
+
* ```
|
|
591
|
+
* @example
|
|
592
|
+
* ```typescript
|
|
593
|
+
* const x: Result<number, string> = err('Early error');
|
|
594
|
+
* const y: Result<string, string> = err('Late error');
|
|
595
|
+
* assert.equal(x.and(y), err('Early error'));
|
|
596
|
+
* ```
|
|
597
|
+
* @example
|
|
598
|
+
* ```typescript
|
|
599
|
+
* const x: Result<number, string> = ok(2);
|
|
600
|
+
* const y: Result<string, string> = ok('Hello');
|
|
601
|
+
* assert.equal(x.and(y), ok('Hello'));
|
|
602
|
+
* ```
|
|
603
|
+
*
|
|
604
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and}
|
|
605
|
+
*/
|
|
606
|
+
and<OutputResult extends AnyResult>(result: OutputResult): If<Success, OutputResult, Err<E>>;
|
|
607
|
+
/**
|
|
608
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
609
|
+
*
|
|
610
|
+
* This function can be used for control flow based on `Result` values.
|
|
611
|
+
* @param cb The predicate.
|
|
612
|
+
*
|
|
613
|
+
* @example
|
|
614
|
+
* ```typescript
|
|
615
|
+
* function fractionOf4(value: number) {
|
|
616
|
+
* return value === 0 ? err('overflowed') : ok(4 / value);
|
|
617
|
+
* }
|
|
618
|
+
*
|
|
619
|
+
* assert.equal(ok(2).andThen(fractionOf4), ok(4));
|
|
620
|
+
* assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
|
|
621
|
+
* assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
|
|
622
|
+
* ```
|
|
623
|
+
*
|
|
624
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
|
|
625
|
+
*/
|
|
626
|
+
andThen<OutputResult extends AnyResult>(cb: (value: T) => OutputResult): OutputResult;
|
|
627
|
+
/**
|
|
628
|
+
* Return `result` if the result is `Err`, otherwise returns the `Ok` value of self.
|
|
629
|
+
*
|
|
630
|
+
* Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended
|
|
631
|
+
* to use {@link orElse}, which is lazily evaluated.
|
|
632
|
+
* @param result The result to check.
|
|
633
|
+
*
|
|
634
|
+
* @example
|
|
635
|
+
* ```typescript
|
|
636
|
+
* const x: Result<number, string> = ok(2);
|
|
637
|
+
* const y: Result<number, string> = err('Late error');
|
|
638
|
+
* assert.equal(x.or(y), ok(2));
|
|
639
|
+
* ```
|
|
640
|
+
* @example
|
|
641
|
+
* ```typescript
|
|
642
|
+
* const x: Result<number, string> = err('Early error');
|
|
643
|
+
* const y: Result<number, string> = ok(2);
|
|
644
|
+
* assert.equal(x.or(y), ok(2));
|
|
645
|
+
* ```
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* const x: Result<number, string> = err('Early error');
|
|
649
|
+
* const y: Result<number, string> = err('Late error');
|
|
650
|
+
* assert.equal(x.or(y), err('Late error'));
|
|
651
|
+
* ```
|
|
652
|
+
* @example
|
|
653
|
+
* ```typescript
|
|
654
|
+
* const x: Result<number, string> = ok(2);
|
|
655
|
+
* const y: Result<number, string> = ok(100);
|
|
656
|
+
* assert.equal(x.or(y), ok(2));
|
|
657
|
+
* ```
|
|
658
|
+
*
|
|
659
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or}
|
|
660
|
+
*/
|
|
661
|
+
or<OutputResult extends AnyResult>(result: OutputResult): If<Success, Ok<T>, OutputResult>;
|
|
662
|
+
/**
|
|
663
|
+
* Calls `cb` if the result is `Err`, otherwise returns the `Ok` value of self.
|
|
664
|
+
*
|
|
665
|
+
* This function can be used for control flow based on result values.
|
|
666
|
+
* @param cb The predicate.
|
|
667
|
+
*
|
|
668
|
+
* @example
|
|
669
|
+
* ```typescript
|
|
670
|
+
* const square = (x: number): Result<number, string> => ok(x * x);
|
|
671
|
+
* const wrapErr = (x: number): Result<number, string> => err(x);
|
|
672
|
+
*
|
|
673
|
+
* assert.equal(ok(2).orElse(square).orElse(square), ok(2));
|
|
674
|
+
* assert.equal(ok(2).orElse(wrapErr).orElse(square), ok(2));
|
|
675
|
+
* assert.equal(err(3).orElse(square).orElse(wrapErr), ok(9));
|
|
676
|
+
* assert.equal(err(3).orElse(wrapErr).orElse(wrapErr), err(3));
|
|
677
|
+
* ```
|
|
678
|
+
*
|
|
679
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else}
|
|
680
|
+
*/
|
|
681
|
+
orElse<OutputResult extends AnyResult>(cb: (error: E) => OutputResult): If<Success, Ok<T>, OutputResult>;
|
|
682
|
+
/**
|
|
683
|
+
* Returns `true` if the result is an `Ok` and the given value strict equals it.
|
|
684
|
+
* @param value The value to compare.
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```typescript
|
|
688
|
+
* const x: Result<number, string> = ok(2);
|
|
689
|
+
* assert.equal(x.contains(2), true);
|
|
690
|
+
* ```
|
|
691
|
+
* @example
|
|
692
|
+
* ```typescript
|
|
693
|
+
* const x: Result<number, string> = ok(3);
|
|
694
|
+
* assert.equal(x.contains(2), false);
|
|
695
|
+
* ```
|
|
696
|
+
* @example
|
|
697
|
+
* ```typescript
|
|
698
|
+
* const x: Result<number, string> = err('Some error message');
|
|
699
|
+
* assert.equal(x.contains(2), false);
|
|
700
|
+
* ```
|
|
701
|
+
*
|
|
702
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains}
|
|
703
|
+
*/
|
|
704
|
+
contains<const Value extends T>(this: Ok<T>, value: Value): this is Ok<Value>;
|
|
705
|
+
contains(this: Err<E>, value: T): false;
|
|
706
|
+
/**
|
|
707
|
+
* Returns `true` if the result is an `Err` and the given error strict equals it.
|
|
708
|
+
* @param error The error to compare.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```typescript
|
|
712
|
+
* const x: Result<number, string> = ok(2);
|
|
713
|
+
* assert.equal(x.containsErr('Some error message'), false);
|
|
714
|
+
* ```
|
|
715
|
+
* @example
|
|
716
|
+
* ```typescript
|
|
717
|
+
* const x: Result<number, string> = err('Some error message');
|
|
718
|
+
* assert.equal(x.containsErr('Some error message'), true);
|
|
719
|
+
* ```
|
|
720
|
+
* @example
|
|
721
|
+
* ```typescript
|
|
722
|
+
* const x: Result<number, string> = err('Some other error message');
|
|
723
|
+
* assert.equal(x.containsErr('Some error message'), false);
|
|
724
|
+
* ```
|
|
725
|
+
*
|
|
726
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.contains_err}
|
|
727
|
+
*/
|
|
728
|
+
containsErr(this: Ok<T>, error: E): false;
|
|
729
|
+
containsErr<const Value extends E>(this: Err<E>, error: Value): this is Err<Value>;
|
|
730
|
+
/**
|
|
731
|
+
* Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
|
732
|
+
*
|
|
733
|
+
* `ok(none)` will be mapped to `none`. `ok(some(v))` and `err(e)` will be mapped to `some(ok(v))` and `some(err(e))`.
|
|
734
|
+
*
|
|
735
|
+
* @example
|
|
736
|
+
* ```typescript
|
|
737
|
+
* const x: Result<Option<number>, Error> = ok(some(5));
|
|
738
|
+
* const y: Option<Result<number, Error>> = some(ok(5));
|
|
739
|
+
* assert.equal(x.transpose(), y);
|
|
740
|
+
* ```
|
|
741
|
+
*
|
|
742
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.transpose}
|
|
743
|
+
*/
|
|
744
|
+
transpose<InnerValue>(this: Result<Option<InnerValue>, E, Success>): If<Success, Option<Ok<InnerValue>>, Some<Err<E>>>;
|
|
745
|
+
/**
|
|
746
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
747
|
+
*
|
|
748
|
+
* @example
|
|
749
|
+
* ```typescript
|
|
750
|
+
* const x: Result<Result<string, number>, number> = ok(ok('Hello'));
|
|
751
|
+
* assert.equal(x.flatten(), ok('Hello'));
|
|
752
|
+
* ```
|
|
753
|
+
* @example
|
|
754
|
+
* ```typescript
|
|
755
|
+
* const x: Result<Result<string, number>, number> = ok(err(6));
|
|
756
|
+
* assert.equal(x.flatten(), err(6));
|
|
757
|
+
* ```
|
|
758
|
+
* @example
|
|
759
|
+
* ```typescript
|
|
760
|
+
* const x: Result<Result<string, number>, number> = err(6);
|
|
761
|
+
* assert.equal(x.flatten(), err(6));
|
|
762
|
+
* ```
|
|
763
|
+
*
|
|
764
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
|
|
765
|
+
*/
|
|
766
|
+
flatten<InnerResult extends AnyResult>(this: Result<InnerResult, E, Success>): If<Success, InnerResult, Err<E>>;
|
|
767
|
+
/**
|
|
768
|
+
* Returns the `Ok` value if self is `Ok`, and the `Err` value if self is `Err`.
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* ```typescript
|
|
772
|
+
* let x: Result<number, number> = ok(3);
|
|
773
|
+
* assert.equal(x.intoOkOrErr(), 3);
|
|
774
|
+
* ```
|
|
775
|
+
* @example
|
|
776
|
+
* ```typescript
|
|
777
|
+
* let x: Result<number, number> = err(4);
|
|
778
|
+
* assert.equal(x.intoOkOrErr(), 4);
|
|
779
|
+
* ```
|
|
780
|
+
*
|
|
781
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err}
|
|
782
|
+
*/
|
|
783
|
+
intoOkOrErr(): If<Success, T, E>;
|
|
784
|
+
/**
|
|
785
|
+
* Returns a `Promise` object with the awaited value (if `Ok`) or the awaited error (if `Err`).
|
|
786
|
+
*
|
|
787
|
+
* @example
|
|
788
|
+
* ```typescript
|
|
789
|
+
* let x = ok(Promise.resolve(3));
|
|
790
|
+
* assert.equal(await x.intoPromise(), ok(3));
|
|
791
|
+
* ```
|
|
792
|
+
*
|
|
793
|
+
* @note This is an extension not supported in Rust
|
|
794
|
+
*/
|
|
795
|
+
intoPromise(): Promise<If<Success, Ok<Awaited<T>>, Err<Awaited<E>>>>;
|
|
796
|
+
/**
|
|
797
|
+
* Checks whether or not `other` equals with self.
|
|
798
|
+
* @param other The other result to compare.
|
|
799
|
+
*
|
|
800
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
|
|
801
|
+
*/
|
|
802
|
+
eq<OtherValue extends T, OtherError extends E, OtherSuccess extends boolean>(other: Result<OtherValue, OtherError, OtherSuccess>): this is Result<OtherValue, OtherError, OtherSuccess>;
|
|
803
|
+
/**
|
|
804
|
+
* Checks whether or not `other` doesn't equal with self.
|
|
805
|
+
* @param other The other result to compare.
|
|
806
|
+
*
|
|
807
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
|
|
808
|
+
*/
|
|
809
|
+
ne(other: Result<T, E>): boolean;
|
|
810
|
+
/**
|
|
811
|
+
* Runs `ok` function if self is `Ok`, otherwise runs `err` function.
|
|
812
|
+
* @param branches The branches to match.
|
|
813
|
+
*
|
|
814
|
+
* @example
|
|
815
|
+
* ```typescript
|
|
816
|
+
* const result = ok(4).match({
|
|
817
|
+
* ok: (v) => v,
|
|
818
|
+
* err: () => 0
|
|
819
|
+
* });
|
|
820
|
+
* assert.equal(result, 4);
|
|
821
|
+
* ```
|
|
822
|
+
* @example
|
|
823
|
+
* ```typescript
|
|
824
|
+
* const result = err('Hello').match({
|
|
825
|
+
* ok: (v) => v,
|
|
826
|
+
* err: () => 0
|
|
827
|
+
* });
|
|
828
|
+
* assert.equal(result, 0);
|
|
829
|
+
* ```
|
|
830
|
+
*/
|
|
831
|
+
match<OkValue, ErrValue>(branches: {
|
|
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>;
|
|
835
|
+
/**
|
|
836
|
+
* Returns an iterator over the possibly contained value.
|
|
837
|
+
*
|
|
838
|
+
* The iterator yields one value if the result is `Ok`, otherwise none.
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```typescript
|
|
842
|
+
* const x = ok(7);
|
|
843
|
+
* for (const value of x) {
|
|
844
|
+
* console.log(value);
|
|
845
|
+
* }
|
|
846
|
+
* // Logs 7
|
|
847
|
+
* ```
|
|
848
|
+
* @example
|
|
849
|
+
* ```typescript
|
|
850
|
+
* const x = err('Nothing!');
|
|
851
|
+
* for (const value of x) {
|
|
852
|
+
* console.log(value);
|
|
853
|
+
* }
|
|
854
|
+
* // Doesn't log
|
|
855
|
+
* ```
|
|
856
|
+
*
|
|
857
|
+
* @see {@link IResult.iter}
|
|
858
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.iter}
|
|
859
|
+
*/
|
|
860
|
+
[Symbol.iterator](): Generator<T>;
|
|
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;
|
|
921
|
+
/**
|
|
922
|
+
* Creates a {@link Result} out of a callback.
|
|
923
|
+
*
|
|
924
|
+
* @typeparam T The result's type.
|
|
925
|
+
* @typeparam E The error's type.
|
|
926
|
+
*/
|
|
927
|
+
static from<T, E = unknown>(this: void, op: ResultResolvable<T, E> | (() => ResultResolvable<T, E>)): Result<T, E>;
|
|
928
|
+
/**
|
|
929
|
+
* Creates a {@link Result} out of a promise or async callback.
|
|
930
|
+
*
|
|
931
|
+
* @typeparam T The result's type.
|
|
932
|
+
* @typeparam E The error's type.
|
|
933
|
+
*/
|
|
934
|
+
static fromAsync<T, E = unknown>(this: void, op: Awaitable<ResultResolvable<T, E>> | (() => Awaitable<ResultResolvable<T, E>>)): Promise<Result<T, E>>;
|
|
935
|
+
/**
|
|
936
|
+
* Creates an {@link Ok} that is the combination of all collected {@link Ok} values as an array, or the first
|
|
937
|
+
* {@link Err} encountered.
|
|
938
|
+
*
|
|
939
|
+
* @param results An array of {@link Result}s.
|
|
940
|
+
* @returns A new {@link Result}.
|
|
941
|
+
*/
|
|
942
|
+
static all<const Entries extends readonly AnyResult[]>(this: void, results: Entries): Result<UnwrapOkArray<Entries>, UnwrapErrArray<Entries>[number]>;
|
|
943
|
+
/**
|
|
944
|
+
* Returns the first encountered {@link Ok}, or an {@link Err} that is the combination of all collected error values.
|
|
945
|
+
*
|
|
946
|
+
* @param results An array of {@link Result}s.
|
|
947
|
+
* @returns A new {@link Result}.
|
|
948
|
+
*/
|
|
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[] | []> = {
|
|
1041
|
+
-readonly [P in keyof T]: UnwrapOk<T[P]>;
|
|
1042
|
+
};
|
|
1043
|
+
type UnwrapErrArray<T extends readonly AnyResult[] | []> = {
|
|
1044
|
+
-readonly [P in keyof T]: UnwrapErr<T[P]>;
|
|
1045
|
+
};
|
|
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
|
+
}
|
|
1061
|
+
|
|
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();
|
|
1073
|
+
/**
|
|
1074
|
+
* Returns `true` if the option is a `Some` value.
|
|
1075
|
+
*
|
|
1076
|
+
* @example
|
|
1077
|
+
* ```typescript
|
|
1078
|
+
* const x: Option<number> = some(2);
|
|
1079
|
+
* assert.equal(x.isSome(), true);
|
|
1080
|
+
* ```
|
|
1081
|
+
* @example
|
|
1082
|
+
* ```typescript
|
|
1083
|
+
* const x: Option<number> = none;
|
|
1084
|
+
* assert.equal(x.isSome(), false);
|
|
1085
|
+
* ```
|
|
1086
|
+
*
|
|
1087
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some}
|
|
1088
|
+
*/
|
|
1089
|
+
isSome(): this is Some<T>;
|
|
1090
|
+
/**
|
|
1091
|
+
* Returns `true` if the option is a `Some` and the value inside of it matches a predicate.
|
|
1092
|
+
* @param cb The predicate.
|
|
1093
|
+
*
|
|
1094
|
+
* @example
|
|
1095
|
+
* ```typescript
|
|
1096
|
+
* const x: Option<number> = some(2);
|
|
1097
|
+
* assert.equal(x.isSomeAnd((x) => x > 1), true);
|
|
1098
|
+
* ```
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```typescript
|
|
1101
|
+
* const x: Option<number> = some(0);
|
|
1102
|
+
* assert.equal(x.isSomeAnd((x) => x > 1), false);
|
|
1103
|
+
* ```
|
|
1104
|
+
* @example
|
|
1105
|
+
* ```typescript
|
|
1106
|
+
* const x: Option<number> = none;
|
|
1107
|
+
* assert.equal(x.isSomeAnd((x) => x > 1), false);
|
|
1108
|
+
* ```
|
|
1109
|
+
*
|
|
1110
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some_and}
|
|
1111
|
+
*/
|
|
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;
|
|
1114
|
+
/**
|
|
1115
|
+
* Returns `true` if the option is a `None` value.
|
|
1116
|
+
*
|
|
1117
|
+
* @example
|
|
1118
|
+
* ```typescript
|
|
1119
|
+
* const x: Option<number> = some(2);
|
|
1120
|
+
* assert.equal(x.isNone(), false);
|
|
1121
|
+
* ```
|
|
1122
|
+
* @example
|
|
1123
|
+
* ```typescript
|
|
1124
|
+
* const x: Option<number> = none;
|
|
1125
|
+
* assert.equal(x.isNone(), true);
|
|
1126
|
+
* ```
|
|
1127
|
+
*
|
|
1128
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.is_none}
|
|
1129
|
+
*/
|
|
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>;
|
|
1154
|
+
/**
|
|
1155
|
+
* Returns the contained `Some` value.
|
|
1156
|
+
* @param message The message for the error.
|
|
1157
|
+
* If the value is an `Err`, it throws an {@link OptionError} with the given message.
|
|
1158
|
+
*
|
|
1159
|
+
* @example
|
|
1160
|
+
* ```typescript
|
|
1161
|
+
* const x: Option<string> = some(2);
|
|
1162
|
+
* assert.equal(x.expect('Whoops!'), 2);
|
|
1163
|
+
* ```
|
|
1164
|
+
* @example
|
|
1165
|
+
* ```typescript
|
|
1166
|
+
* const x: Option<string> = none;
|
|
1167
|
+
* assert.throws(() => x.expect('Whoops!'), {
|
|
1168
|
+
* name: 'OptionError',
|
|
1169
|
+
* message: 'Whoops'
|
|
1170
|
+
* });
|
|
1171
|
+
* ```
|
|
1172
|
+
*
|
|
1173
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.expect}
|
|
1174
|
+
*/
|
|
1175
|
+
expect(message: string): If<Exists, T, never>;
|
|
1176
|
+
/**
|
|
1177
|
+
* Returns the contained `Some` value.
|
|
1178
|
+
*
|
|
1179
|
+
* If the value is an `Err`, it throws an {@link OptionError} with the message.
|
|
1180
|
+
* @seealso {@link unwrapOr}
|
|
1181
|
+
* @seealso {@link unwrapOrElse}
|
|
1182
|
+
*
|
|
1183
|
+
* @example
|
|
1184
|
+
* ```typescript
|
|
1185
|
+
* const x: Option<string> = some(2);
|
|
1186
|
+
* assert.equal(x.unwrap(), 2);
|
|
1187
|
+
* ```
|
|
1188
|
+
* @example
|
|
1189
|
+
* ```typescript
|
|
1190
|
+
* const x: Option<string> = none;
|
|
1191
|
+
* assert.throws(() => x.unwrap(), {
|
|
1192
|
+
* name: 'OptionError',
|
|
1193
|
+
* message: 'Unwrap failed'
|
|
1194
|
+
* });
|
|
1195
|
+
* ```
|
|
1196
|
+
*
|
|
1197
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap}
|
|
1198
|
+
*/
|
|
1199
|
+
unwrap(): If<Exists, T, never>;
|
|
1200
|
+
/**
|
|
1201
|
+
* Returns the contained `Some` value or a provided default.
|
|
1202
|
+
*
|
|
1203
|
+
* Arguments passed to `unwrapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1204
|
+
* recommended to use {@link unwrapOrElse}, which is lazily evaluated.
|
|
1205
|
+
*
|
|
1206
|
+
* @example
|
|
1207
|
+
* ```typescript
|
|
1208
|
+
* assert.equal(some(2).unwrapOr(0), 2);
|
|
1209
|
+
* ```
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```typescript
|
|
1212
|
+
* assert.equal(none.unwrapOr(0), 0);
|
|
1213
|
+
* ```
|
|
1214
|
+
*
|
|
1215
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or}
|
|
1216
|
+
*/
|
|
1217
|
+
unwrapOr<OutputValue>(defaultValue: OutputValue): If<Exists, T, OutputValue>;
|
|
1218
|
+
/**
|
|
1219
|
+
* Returns the contained Some value or computes it from a closure.
|
|
1220
|
+
*
|
|
1221
|
+
* @example
|
|
1222
|
+
* ```typescript
|
|
1223
|
+
* assert.equal(some(2).unwrapOrElse(() => 0), 2);
|
|
1224
|
+
* ```
|
|
1225
|
+
* @example
|
|
1226
|
+
* ```typescript
|
|
1227
|
+
* assert.equal(none.unwrapOrElse(() => 0), 0);
|
|
1228
|
+
* ```
|
|
1229
|
+
*
|
|
1230
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap_or_else}
|
|
1231
|
+
*/
|
|
1232
|
+
unwrapOrElse<OutputValue>(cb: () => OutputValue): If<Exists, T, OutputValue>;
|
|
1233
|
+
/**
|
|
1234
|
+
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
1235
|
+
* @param cb The predicate.
|
|
1236
|
+
*
|
|
1237
|
+
* @example
|
|
1238
|
+
* ```typescript
|
|
1239
|
+
* const maybeSomeString = some('Hello, world!');
|
|
1240
|
+
* const maybeSomeLength = maybeSomeString.map((value) => value.length);
|
|
1241
|
+
*
|
|
1242
|
+
* assert.equal(maybeSomeLength, some(13));
|
|
1243
|
+
* ```
|
|
1244
|
+
*
|
|
1245
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map}
|
|
1246
|
+
*/
|
|
1247
|
+
map<U>(cb: (value: T) => U): Option<U, Exists>;
|
|
1248
|
+
/**
|
|
1249
|
+
* Maps a `Some<T>` to the returned `Option<U>` by applying a function to a contained value, leaving `None`
|
|
1250
|
+
* untouched.
|
|
1251
|
+
* @param cb The predicate.
|
|
1252
|
+
*
|
|
1253
|
+
* @example
|
|
1254
|
+
* ```typescript
|
|
1255
|
+
* const input: Option<string> = some('Hello, world!');
|
|
1256
|
+
* const result = input.mapInto((value) => some(value.length));
|
|
1257
|
+
*
|
|
1258
|
+
* assert.equal(result, some(13));
|
|
1259
|
+
* ```
|
|
1260
|
+
* @example
|
|
1261
|
+
* ```typescript
|
|
1262
|
+
* const input: Option<string> = none;
|
|
1263
|
+
* const result = input.mapInto((value) => some(value.length));
|
|
1264
|
+
*
|
|
1265
|
+
* assert.equal(result, none);
|
|
1266
|
+
* ```
|
|
1267
|
+
*
|
|
1268
|
+
* @note This is an extension not supported in Rust
|
|
1269
|
+
*/
|
|
1270
|
+
mapInto<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption;
|
|
1271
|
+
/**
|
|
1272
|
+
* Returns the provided default result (if none), or applies a function to the contained value (if any).
|
|
1273
|
+
*
|
|
1274
|
+
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1275
|
+
* recommended to use {@link mapOrElse}, which is lazily evaluated.
|
|
1276
|
+
* @param defaultValue The default value.
|
|
1277
|
+
* @param cb The predicate.
|
|
1278
|
+
*
|
|
1279
|
+
* @example
|
|
1280
|
+
* ```typescript
|
|
1281
|
+
* const x: Option<string> = some('hello');
|
|
1282
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 5);
|
|
1283
|
+
* ```
|
|
1284
|
+
* @example
|
|
1285
|
+
* ```typescript
|
|
1286
|
+
* const x: Option<string> = none;
|
|
1287
|
+
* assert.equal(x.mapOr(42, (value) => value.length), 42);
|
|
1288
|
+
* ```
|
|
1289
|
+
*
|
|
1290
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or}
|
|
1291
|
+
*/
|
|
1292
|
+
mapOr<MappedOutputValue, DefaultOutputValue>(defaultValue: DefaultOutputValue, cb: (value: T) => MappedOutputValue): If<Exists, MappedOutputValue, DefaultOutputValue>;
|
|
1293
|
+
/**
|
|
1294
|
+
* Computes a default function result (if none), or applies a different function to the contained value (if any).
|
|
1295
|
+
* @param defaultValue The default value.
|
|
1296
|
+
* @param cb The predicate.
|
|
1297
|
+
*
|
|
1298
|
+
* @example
|
|
1299
|
+
* ```typescript
|
|
1300
|
+
* const x: Option<string> = some('hello');
|
|
1301
|
+
* assert.equal(x.mapOrElse(() => 42, (value) => value.length), 5);
|
|
1302
|
+
* ```
|
|
1303
|
+
* @example
|
|
1304
|
+
* ```typescript
|
|
1305
|
+
* const x: Option<string> = none;
|
|
1306
|
+
* assert.equal(x.mapOrElse(() => 42, (value) => value.length), 42);
|
|
1307
|
+
* ```
|
|
1308
|
+
*
|
|
1309
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.map_or_else}
|
|
1310
|
+
*/
|
|
1311
|
+
mapOrElse<OutputValue, OutputNone>(defaultValue: () => OutputNone, cb: (value: T) => OutputValue): If<Exists, OutputValue, OutputNone>;
|
|
1312
|
+
/**
|
|
1313
|
+
* Maps a `None` to the returned `Option<U>` by applying a function to a contained value, leaving `Some<T>`
|
|
1314
|
+
* untouched.
|
|
1315
|
+
* @param cb The predicate.
|
|
1316
|
+
*
|
|
1317
|
+
* @example
|
|
1318
|
+
* ```typescript
|
|
1319
|
+
* const input: Option<string> = some('Hello, world!');
|
|
1320
|
+
* const result = input.mapNoneInto(() => some(13));
|
|
1321
|
+
*
|
|
1322
|
+
* assert.equal(result, some('Hello, world!'));
|
|
1323
|
+
* ```
|
|
1324
|
+
* @example
|
|
1325
|
+
* ```typescript
|
|
1326
|
+
* const input: Option<string> = none;
|
|
1327
|
+
* const result = input.mapNoneInto(() => some(13));
|
|
1328
|
+
*
|
|
1329
|
+
* assert.equal(result, some(13));
|
|
1330
|
+
* ```
|
|
1331
|
+
*
|
|
1332
|
+
* @note This is an extension not supported in Rust
|
|
1333
|
+
*/
|
|
1334
|
+
mapNoneInto<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
|
|
1335
|
+
/**
|
|
1336
|
+
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
1337
|
+
* @param cb The predicate.
|
|
1338
|
+
* @seealso {@link inspectAsync} for the awaitable version.
|
|
1339
|
+
*
|
|
1340
|
+
* @example
|
|
1341
|
+
* ```typescript
|
|
1342
|
+
* some(2).inspect(console.log);
|
|
1343
|
+
* // Logs: 2
|
|
1344
|
+
* ```
|
|
1345
|
+
* @example
|
|
1346
|
+
* ```typescript
|
|
1347
|
+
* none.inspect(console.log);
|
|
1348
|
+
* // Doesn't log
|
|
1349
|
+
* ```
|
|
1350
|
+
*
|
|
1351
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.inspect}
|
|
1352
|
+
*/
|
|
1353
|
+
inspect(cb: (value: T) => void): this;
|
|
1354
|
+
/**
|
|
1355
|
+
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
1356
|
+
* @param cb The predicate.
|
|
1357
|
+
* @seealso {@link inspect} for the sync version.
|
|
1358
|
+
*
|
|
1359
|
+
* @example
|
|
1360
|
+
* ```typescript
|
|
1361
|
+
* await some(2).inspectAsync(console.log);
|
|
1362
|
+
* // Logs: 2
|
|
1363
|
+
* ```
|
|
1364
|
+
* @example
|
|
1365
|
+
* ```typescript
|
|
1366
|
+
* await none.inspectAsync(console.log);
|
|
1367
|
+
* // Doesn't log
|
|
1368
|
+
* ```
|
|
1369
|
+
*
|
|
1370
|
+
* @note This is an extension not supported in Rust
|
|
1371
|
+
*/
|
|
1372
|
+
inspectAsync(cb: (value: T) => Awaitable<unknown>): Promise<this>;
|
|
1373
|
+
/**
|
|
1374
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
1375
|
+
*
|
|
1376
|
+
* Arguments passed to `okOr` are eagerly evaluated; if you are passing the result of a function call, it is
|
|
1377
|
+
* recommended to use {@link okOrElse}, which is lazily evaluated.
|
|
1378
|
+
* @param err The error to be used.
|
|
1379
|
+
*
|
|
1380
|
+
* @example
|
|
1381
|
+
* ```typescript
|
|
1382
|
+
* const x: Option<string> = some('hello');
|
|
1383
|
+
* assert.equal(x.okOr(0), ok('hello'));
|
|
1384
|
+
* ```
|
|
1385
|
+
* @example
|
|
1386
|
+
* ```typescript
|
|
1387
|
+
* const x: Option<string> = none;
|
|
1388
|
+
* assert.equal(x.okOr(0), err(0));
|
|
1389
|
+
* ```
|
|
1390
|
+
*
|
|
1391
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or}
|
|
1392
|
+
*/
|
|
1393
|
+
okOr<ErrorValue>(error: ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
|
|
1394
|
+
/**
|
|
1395
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
1396
|
+
* @param cb The error to be used.
|
|
1397
|
+
*
|
|
1398
|
+
* @example
|
|
1399
|
+
* ```typescript
|
|
1400
|
+
* const x: Option<string> = some('hello');
|
|
1401
|
+
* assert.equal(x.okOrElse(() => 0), ok('hello'));
|
|
1402
|
+
* ```
|
|
1403
|
+
* @example
|
|
1404
|
+
* ```typescript
|
|
1405
|
+
* const x: Option<string> = none;
|
|
1406
|
+
* assert.equal(x.okOrElse(() => 0), err(0));
|
|
1407
|
+
* ```
|
|
1408
|
+
*
|
|
1409
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else}
|
|
1410
|
+
*/
|
|
1411
|
+
okOrElse<ErrorValue>(cb: () => ErrorValue): If<Exists, Ok<T>, Err<ErrorValue>>;
|
|
1412
|
+
/**
|
|
1413
|
+
* Returns an iterator over the possibly contained value.
|
|
1414
|
+
*
|
|
1415
|
+
* The iterator yields one value if the result is `Some`, otherwise none.
|
|
1416
|
+
*
|
|
1417
|
+
* @example
|
|
1418
|
+
* ```typescript
|
|
1419
|
+
* const x = some(7);
|
|
1420
|
+
* for (const value of x) {
|
|
1421
|
+
* console.log(value);
|
|
1422
|
+
* }
|
|
1423
|
+
* // Logs 7
|
|
1424
|
+
* ```
|
|
1425
|
+
* @example
|
|
1426
|
+
* ```typescript
|
|
1427
|
+
* const x = none;
|
|
1428
|
+
* for (const value of x) {
|
|
1429
|
+
* console.log(value);
|
|
1430
|
+
* }
|
|
1431
|
+
* // Doesn't log
|
|
1432
|
+
* ```
|
|
1433
|
+
*
|
|
1434
|
+
* @see {@link Option.iter}
|
|
1435
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
|
|
1436
|
+
*/
|
|
1437
|
+
iter(): Generator<T>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Returns `None` if the option is `None`, otherwise returns `option`.
|
|
1440
|
+
* @param option The option.
|
|
1441
|
+
*
|
|
1442
|
+
* @example
|
|
1443
|
+
* ```typescript
|
|
1444
|
+
* const x: Option<number> = some(2);
|
|
1445
|
+
* const y: Option<string> = none;
|
|
1446
|
+
* assert.equal(x.and(y), none);
|
|
1447
|
+
* ```
|
|
1448
|
+
* @example
|
|
1449
|
+
* ```typescript
|
|
1450
|
+
* const x: Option<number> = none;
|
|
1451
|
+
* const y: Option<string> = some('foo');
|
|
1452
|
+
* assert.equal(x.and(y), none);
|
|
1453
|
+
* ```
|
|
1454
|
+
* @example
|
|
1455
|
+
* ```typescript
|
|
1456
|
+
* const x: Option<number> = some(2);
|
|
1457
|
+
* const y: Option<string> = some('foo');
|
|
1458
|
+
* assert.equal(x.and(y), some('foo'));
|
|
1459
|
+
* ```
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```typescript
|
|
1462
|
+
* const x: Option<number> = none;
|
|
1463
|
+
* const y: Option<string> = none;
|
|
1464
|
+
* assert.equal(x.and(y), none);
|
|
1465
|
+
* ```
|
|
1466
|
+
*
|
|
1467
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.and}
|
|
1468
|
+
*/
|
|
1469
|
+
and<OutputOption extends AnyOption>(option: OutputOption): If<Exists, OutputOption, None>;
|
|
1470
|
+
/**
|
|
1471
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
1472
|
+
*
|
|
1473
|
+
* This function can be used for control flow based on `Result` values.
|
|
1474
|
+
* @param cb The predicate.
|
|
1475
|
+
*
|
|
1476
|
+
* @example
|
|
1477
|
+
* ```typescript
|
|
1478
|
+
* function fractionOf4(value: number) {
|
|
1479
|
+
* return value === 0 ? none : some(4 / value);
|
|
1480
|
+
* }
|
|
1481
|
+
*
|
|
1482
|
+
* assert.equal(some(2).andThen(fractionOf4), some(4));
|
|
1483
|
+
* assert.equal(some(0).andThen(fractionOf4), none);
|
|
1484
|
+
* assert.equal(none.andThen(fractionOf4), none);
|
|
1485
|
+
* ```
|
|
1486
|
+
*
|
|
1487
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then}
|
|
1488
|
+
*/
|
|
1489
|
+
andThen<OutputOption extends AnyOption>(cb: (value: T) => OutputOption): OutputOption;
|
|
1490
|
+
/**
|
|
1491
|
+
* Returns the option if it contains a value, otherwise returns `option`.
|
|
1492
|
+
* @param option The option.
|
|
1493
|
+
*
|
|
1494
|
+
* @example
|
|
1495
|
+
* ```typescript
|
|
1496
|
+
* const x: Option<number> = some(2);
|
|
1497
|
+
* const y: Option<number> = none;
|
|
1498
|
+
* assert.equal(x.or(y), some(2));
|
|
1499
|
+
* ```
|
|
1500
|
+
* @example
|
|
1501
|
+
* ```typescript
|
|
1502
|
+
* const x: Option<number> = none;
|
|
1503
|
+
* const y: Option<number> = some(100);
|
|
1504
|
+
* assert.equal(x.or(y), some(100));
|
|
1505
|
+
* ```
|
|
1506
|
+
* @example
|
|
1507
|
+
* ```typescript
|
|
1508
|
+
* const x: Option<number> = some(2);
|
|
1509
|
+
* const y: Option<number> = some(100);
|
|
1510
|
+
* assert.equal(x.or(y), some(2));
|
|
1511
|
+
* ```
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```typescript
|
|
1514
|
+
* const x: Option<number> = none;
|
|
1515
|
+
* const y: Option<number> = none;
|
|
1516
|
+
* assert.equal(x.or(y), none);
|
|
1517
|
+
* ```
|
|
1518
|
+
*
|
|
1519
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or}
|
|
1520
|
+
*/
|
|
1521
|
+
or<OutputOption extends AnyOption>(option: OutputOption): If<Exists, Some<T>, OutputOption>;
|
|
1522
|
+
/**
|
|
1523
|
+
* Calls `cb` if the result is `Ok`, otherwise returns the `Err` value of self.
|
|
1524
|
+
*
|
|
1525
|
+
* This function can be used for control flow based on `Result` values.
|
|
1526
|
+
* @param cb The predicate.
|
|
1527
|
+
*
|
|
1528
|
+
* @example
|
|
1529
|
+
* ```typescript
|
|
1530
|
+
* const nobody = (): Option<string> => none;
|
|
1531
|
+
* const vikings = (): Option<string> => some('vikings');
|
|
1532
|
+
*
|
|
1533
|
+
* assert.equal(some('barbarians').orElse(vikings), some('barbarians'));
|
|
1534
|
+
* assert.equal(none.orElse(vikings), some('vikings'));
|
|
1535
|
+
* assert.equal(none.orElse(nobody), none);
|
|
1536
|
+
* ```
|
|
1537
|
+
*
|
|
1538
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.or_else}
|
|
1539
|
+
*/
|
|
1540
|
+
orElse<OutputOption extends AnyOption>(cb: () => OutputOption): If<Exists, Some<T>, OutputOption>;
|
|
1541
|
+
/**
|
|
1542
|
+
* Returns `Some` if exactly one of self or `option` is `Some`, otherwise returns `None`.
|
|
1543
|
+
* @param option The option to compare.
|
|
1544
|
+
*
|
|
1545
|
+
* @example
|
|
1546
|
+
* ```typescript
|
|
1547
|
+
* const x: Option<number> = some(2);
|
|
1548
|
+
* const y: Option<number> = none;
|
|
1549
|
+
* assert.equal(x.xor(y), some(2));
|
|
1550
|
+
* ```
|
|
1551
|
+
* @example
|
|
1552
|
+
* ```typescript
|
|
1553
|
+
* const x: Option<number> = none;
|
|
1554
|
+
* const y: Option<number> = some(2);
|
|
1555
|
+
* assert.equal(x.xor(y), some(2));
|
|
1556
|
+
* ```
|
|
1557
|
+
* @example
|
|
1558
|
+
* ```typescript
|
|
1559
|
+
* const x: Option<number> = some(2);
|
|
1560
|
+
* const y: Option<number> = some(2);
|
|
1561
|
+
* assert.equal(x.xor(y), none);
|
|
1562
|
+
* ```
|
|
1563
|
+
* @example
|
|
1564
|
+
* ```typescript
|
|
1565
|
+
* const x: Option<number> = none;
|
|
1566
|
+
* const y: Option<number> = none;
|
|
1567
|
+
* assert.equal(x.xor(y), none);
|
|
1568
|
+
* ```
|
|
1569
|
+
*
|
|
1570
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.xor}
|
|
1571
|
+
*/
|
|
1572
|
+
xor<OtherValue, OtherExists extends boolean>(option: Option<OtherValue, OtherExists>): If<Exists, If<OtherExists, None, Some<T>>, Option<OtherValue, OtherExists>>;
|
|
1573
|
+
/**
|
|
1574
|
+
* Returns None if the option is None, otherwise calls `predicate` with the wrapped value and returns:
|
|
1575
|
+
*
|
|
1576
|
+
* - `Some(t)` if `predicate` returns `true` (where t is the wrapped value), and
|
|
1577
|
+
* - `None` if `predicate` returns `false`.
|
|
1578
|
+
* @param predicate The predicate.
|
|
1579
|
+
*
|
|
1580
|
+
* @example
|
|
1581
|
+
* ```typescript
|
|
1582
|
+
* function isEven(value: number) {
|
|
1583
|
+
* return n % 2 === 0;
|
|
1584
|
+
* }
|
|
1585
|
+
*
|
|
1586
|
+
* assert.equal(none.filter(isEven), none);
|
|
1587
|
+
* assert.equal(some(3).filter(isEven), none);
|
|
1588
|
+
* assert.equal(some(4).filter(isEven), some(4));
|
|
1589
|
+
* ```
|
|
1590
|
+
*
|
|
1591
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.filter}
|
|
1592
|
+
*/
|
|
1593
|
+
filter<R extends T>(predicate: (value: T) => value is R): Option<R>;
|
|
1594
|
+
filter(predicate: (value: T) => boolean): Option<T>;
|
|
1595
|
+
/**
|
|
1596
|
+
* Returns `true` if the option is a `Some` value containing the given value.
|
|
1597
|
+
* @param value The value to compare.
|
|
1598
|
+
*
|
|
1599
|
+
* @example
|
|
1600
|
+
* ```typescript
|
|
1601
|
+
* const x: Option<number> = some(2);
|
|
1602
|
+
* assert.equal(x.contains(2), true);
|
|
1603
|
+
* ```
|
|
1604
|
+
* @example
|
|
1605
|
+
* ```typescript
|
|
1606
|
+
* const x: Option<number> = some(3);
|
|
1607
|
+
* assert.equal(x.contains(2), false);
|
|
1608
|
+
* ```
|
|
1609
|
+
* @example
|
|
1610
|
+
* ```typescript
|
|
1611
|
+
* const x: Option<number> = none;
|
|
1612
|
+
* assert.equal(x.contains(2), false);
|
|
1613
|
+
* ```
|
|
1614
|
+
*
|
|
1615
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.contains}
|
|
1616
|
+
*/
|
|
1617
|
+
contains<const Value extends T>(value: If<Exists, Value, unknown>): this is Some<Value>;
|
|
1618
|
+
/**
|
|
1619
|
+
* Zips self with another `Option`.
|
|
1620
|
+
*
|
|
1621
|
+
* If self is `Some(s)` and `other` is `Some(o)`, this method returns `Some([s, o])`. Otherwise, `None` is returned.
|
|
1622
|
+
* @param other The option to zip self with.
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```typescript
|
|
1626
|
+
* const x = some(1);
|
|
1627
|
+
* const y = some('hi');
|
|
1628
|
+
* const z = none;
|
|
1629
|
+
*
|
|
1630
|
+
* assert.equal(x.zip(y), some([1, 'hi']));
|
|
1631
|
+
* assert.equal(x.zip(z), none);
|
|
1632
|
+
* ```
|
|
1633
|
+
*
|
|
1634
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip}
|
|
1635
|
+
*/
|
|
1636
|
+
zip<OtherValue, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): Option<[T, OtherValue], If<Exists, OtherExists, false>>;
|
|
1637
|
+
/**
|
|
1638
|
+
* Zips self and another `Option` with function `f`.
|
|
1639
|
+
*
|
|
1640
|
+
* If self is `Some(s)` and other is `Some(o)`, this method returns `Some(f(s, o))`. Otherwise, `None` is returned.
|
|
1641
|
+
* @param other The option to zip self with.
|
|
1642
|
+
* @param f The function that computes the returned value.
|
|
1643
|
+
*
|
|
1644
|
+
* @example
|
|
1645
|
+
* ```typescript
|
|
1646
|
+
* class Point {
|
|
1647
|
+
* public readonly x: number;
|
|
1648
|
+
* public readonly y: number;
|
|
1649
|
+
*
|
|
1650
|
+
* public constructor(x: number, y: number) {
|
|
1651
|
+
* this.x = x;
|
|
1652
|
+
* this.y = y;
|
|
1653
|
+
* }
|
|
1654
|
+
* }
|
|
1655
|
+
*
|
|
1656
|
+
* const x = some(17.5);
|
|
1657
|
+
* const y = some(42.7);
|
|
1658
|
+
*
|
|
1659
|
+
* assert.equal(x.zipWith(y, (s, o) => new Point(s, o)), some(new Point(17.5, 42.7)));
|
|
1660
|
+
* assert.equal(x.zipWith(none, (s, o) => new Point(s, o)), none);
|
|
1661
|
+
* ```
|
|
1662
|
+
*
|
|
1663
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.zip_with}
|
|
1664
|
+
*/
|
|
1665
|
+
zipWith<OtherValue, OtherExists extends boolean, ReturnValue>(other: Option<OtherValue, OtherExists>, f: (value0: T, value1: OtherValue) => ReturnValue): Option<ReturnValue, If<Exists, OtherExists, false>>;
|
|
1666
|
+
/**
|
|
1667
|
+
* Unzips an option containing a tuple of two options.
|
|
1668
|
+
*
|
|
1669
|
+
* If self is `Some([a, b])` this method returns `[Some(a), Some(b)]`. Otherwise, `[None, None]` is returned.
|
|
1670
|
+
*
|
|
1671
|
+
* @example
|
|
1672
|
+
* ```typescript
|
|
1673
|
+
* const x: Option<[number, string]> = some([1, 'hi']);
|
|
1674
|
+
* assert.equal(x.unzip(), [some(1), some('hi')]);
|
|
1675
|
+
* ```
|
|
1676
|
+
* @example
|
|
1677
|
+
* ```typescript
|
|
1678
|
+
* const x: Option<[number, string]> = none;
|
|
1679
|
+
* assert.equal(x.unzip(), [none, none]);
|
|
1680
|
+
* ```
|
|
1681
|
+
*
|
|
1682
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.unzip}
|
|
1683
|
+
*/
|
|
1684
|
+
unzip<Value0, Value1, Exists extends boolean>(this: Option<readonly [Value0, Value1], Exists>): [Option<Value0, Exists>, Option<Value1, Exists>];
|
|
1685
|
+
/**
|
|
1686
|
+
* Transposes an `Option` of a `Result` into a `Result` of an `Option`.
|
|
1687
|
+
*
|
|
1688
|
+
* `none` will be mapped to `ok(none)`. `some(ok(v))` and `some(err(e))` will be mapped to `ok(some(v))` and `err(e)`.
|
|
1689
|
+
*
|
|
1690
|
+
* @example
|
|
1691
|
+
* ```typescript
|
|
1692
|
+
* const x: Option<Result<number, Error>> = some(ok(5));
|
|
1693
|
+
* const y: Result<Option<number>, Error> = ok(some(5));
|
|
1694
|
+
* assert.equal(x.transpose(), y);
|
|
1695
|
+
* ```
|
|
1696
|
+
*
|
|
1697
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.transpose}
|
|
1698
|
+
*/
|
|
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>>;
|
|
1700
|
+
/**
|
|
1701
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
1702
|
+
*
|
|
1703
|
+
* @example
|
|
1704
|
+
* ```typescript
|
|
1705
|
+
* const x: Option<Option<number>> = some(some(6));
|
|
1706
|
+
* assert.equal(x.flatten(), some(6));
|
|
1707
|
+
* ```
|
|
1708
|
+
* @example
|
|
1709
|
+
* ```typescript
|
|
1710
|
+
* const x: Option<Option<number>> = some(none);
|
|
1711
|
+
* assert.equal(x.flatten(), none);
|
|
1712
|
+
* ```
|
|
1713
|
+
* @example
|
|
1714
|
+
* ```typescript
|
|
1715
|
+
* const x: Option<Option<number>> = none;
|
|
1716
|
+
* assert.equal(x.flatten(), none);
|
|
1717
|
+
* ```
|
|
1718
|
+
*
|
|
1719
|
+
* @see {@link https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten}
|
|
1720
|
+
*/
|
|
1721
|
+
flatten<InnerOption extends AnyOption, Exists extends boolean>(this: Option<InnerOption, Exists>): If<Exists, InnerOption, None>;
|
|
1722
|
+
/**
|
|
1723
|
+
* Returns a `Promise` object with the awaited value (if `Some`).
|
|
1724
|
+
*
|
|
1725
|
+
* @example
|
|
1726
|
+
* ```typescript
|
|
1727
|
+
* let x = some(Promise.resolve(3));
|
|
1728
|
+
* assert.equal(await x.intoPromise(), some(3));
|
|
1729
|
+
* ```
|
|
1730
|
+
*
|
|
1731
|
+
* @note This is an extension not supported in Rust
|
|
1732
|
+
*/
|
|
1733
|
+
intoPromise(): Promise<Option<Awaited<T>, Exists>>;
|
|
1734
|
+
/**
|
|
1735
|
+
* Checks whether or not `other` equals with self.
|
|
1736
|
+
* @param other The other option to compare.
|
|
1737
|
+
*
|
|
1738
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq}
|
|
1739
|
+
*/
|
|
1740
|
+
eq<OtherValue extends T, OtherExists extends boolean>(other: Option<OtherValue, OtherExists>): this is Option<OtherValue, OtherExists>;
|
|
1741
|
+
/**
|
|
1742
|
+
* Checks whether or not `other` doesn't equal with self.
|
|
1743
|
+
* @param other The other option to compare.
|
|
1744
|
+
*
|
|
1745
|
+
* @see {@link https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#method.ne}
|
|
1746
|
+
*/
|
|
1747
|
+
ne(other: Option<T, boolean>): boolean;
|
|
1748
|
+
/**
|
|
1749
|
+
* Runs `ok` function if self is `Ok`, otherwise runs `err` function.
|
|
1750
|
+
* @param branches The branches to match.
|
|
1751
|
+
*
|
|
1752
|
+
* @example
|
|
1753
|
+
* ```typescript
|
|
1754
|
+
* const option = some(4).match({
|
|
1755
|
+
* some: (v) => v,
|
|
1756
|
+
* none: () => 0
|
|
1757
|
+
* });
|
|
1758
|
+
* assert.equal(option, 4);
|
|
1759
|
+
* ```
|
|
1760
|
+
* @example
|
|
1761
|
+
* ```typescript
|
|
1762
|
+
* const option = none.match({
|
|
1763
|
+
* some: (v) => v,
|
|
1764
|
+
* none: () => 0
|
|
1765
|
+
* });
|
|
1766
|
+
* assert.equal(option, 0);
|
|
1767
|
+
* ```
|
|
1768
|
+
*/
|
|
1769
|
+
match<SomeValue, NoneValue>(branches: {
|
|
1770
|
+
some(this: Some<T>, value: T): SomeValue;
|
|
1771
|
+
none(this: None): NoneValue;
|
|
1772
|
+
}): If<Exists, SomeValue, NoneValue>;
|
|
1773
|
+
/**
|
|
1774
|
+
* Returns an iterator over the possibly contained value.
|
|
1775
|
+
*
|
|
1776
|
+
* The iterator yields one value if the result is `Some`, otherwise none.
|
|
1777
|
+
*
|
|
1778
|
+
* @example
|
|
1779
|
+
* ```typescript
|
|
1780
|
+
* const x = some(7);
|
|
1781
|
+
* for (const value of x) {
|
|
1782
|
+
* console.log(value);
|
|
1783
|
+
* }
|
|
1784
|
+
* // Logs 7
|
|
1785
|
+
* ```
|
|
1786
|
+
* @example
|
|
1787
|
+
* ```typescript
|
|
1788
|
+
* const x = none;
|
|
1789
|
+
* for (const value of x) {
|
|
1790
|
+
* console.log(value);
|
|
1791
|
+
* }
|
|
1792
|
+
* // Doesn't log
|
|
1793
|
+
* ```
|
|
1794
|
+
*
|
|
1795
|
+
* @see {@link IOption.iter}
|
|
1796
|
+
* @see {@link https://doc.rust-lang.org/std/option/enum.Option.html#method.iter}
|
|
1797
|
+
*/
|
|
1798
|
+
[Symbol.iterator](): Generator<T>;
|
|
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>;
|
|
1802
|
+
/**
|
|
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
|
+
*
|
|
1854
|
+
* @typeparam T The result's type.
|
|
1855
|
+
* @typeparam E The error's type.
|
|
1856
|
+
*/
|
|
1857
|
+
static from<T>(this: void, op: OptionResolvable<T> | (() => OptionResolvable<T>)): Option<T>;
|
|
1858
|
+
/**
|
|
1859
|
+
* Creates a {@link Result} out of a promise or async callback.
|
|
1860
|
+
*
|
|
1861
|
+
* @typeparam T The result's type.
|
|
1862
|
+
* @typeparam E The error's type.
|
|
1863
|
+
*/
|
|
1864
|
+
static fromAsync<T>(this: void, op: Awaitable<OptionResolvable<T>> | (() => Awaitable<OptionResolvable<T>>)): Promise<Option<T>>;
|
|
1865
|
+
/**
|
|
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}.
|
|
1871
|
+
*/
|
|
1872
|
+
static all<const Entries extends readonly AnyOption[]>(this: void, results: Entries): Option<UnwrapSomeArray<Entries>>;
|
|
1873
|
+
/**
|
|
1874
|
+
* Returns the first encountered {@link Some}, or a {@link None} if none was found.
|
|
1875
|
+
*
|
|
1876
|
+
* @param options An array of {@link Option}s.
|
|
1877
|
+
* @returns A new {@link Option}.
|
|
1878
|
+
*/
|
|
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[] | []> = {
|
|
1888
|
+
-readonly [P in keyof T]: UnwrapSome<T[P]>;
|
|
1889
|
+
};
|
|
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
|
+
}
|
|
1909
|
+
|
|
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 };
|