happy-rusty 1.0.9 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -1,145 +1,560 @@
1
1
  /**
2
- * option::Option type
2
+ * @fileoverview
3
+ * A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum type, used as an alternative to the use of null and undefined.
4
+ *
5
+ * And [Result](https://doc.rust-lang.org/core/result/index.html) enum type, used for better error handling.
6
+ *
3
7
  */
4
- type Option<T> = Some<T> | None;
5
8
  /**
6
- * @fileoverview A Rust-inspired [Option](https://doc.rust-lang.org/core/option/index.html) enum, used as an alternative to the use of null and undefined.
9
+ * Symbol for Option kind: `Some` or `None`.
7
10
  */
11
+ declare const optionKindSymbol: unique symbol;
8
12
  /**
9
- * option::Some type
13
+ * Symbol for Result kind: `Ok` or `Err`.
10
14
  */
11
- interface Some<T> {
12
- readonly kind: 'Some';
13
- readonly isSome: (this: Option<T>) => this is Some<T>;
14
- readonly isNone: (this: Option<T>) => this is None;
15
- readonly unwrap: () => T;
16
- }
15
+ declare const resultKindSymbol: unique symbol;
17
16
  /**
18
- * Create a `Some` object.
19
- *
20
- * # Examples
21
- *
22
- * ```
23
- * const v = Some(10);
24
- * console.assert(v.unwrap() === 10);
25
- * ```
17
+ * Type `Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not.
18
+ * This interface includes methods that act on the `Option` type, similar to Rust's `Option` enum.
26
19
  *
27
- * @param value The wrapped value which can not be null or undefined.
28
- * @returns {Some}
20
+ * As Rust Code:
21
+ ```rust
22
+ pub enum Option<T> {
23
+ None,
24
+ Some(T),
25
+ }
26
+ ```
27
+ * @typeParam T - The type of the value contained in the `Some` variant.
29
28
  */
30
- declare function Some<T>(value: NonNullable<T>): Option<T>;
29
+ interface Option<T> {
30
+ /**
31
+ * Identify `Some` or `None`.
32
+ *
33
+ * @private
34
+ */
35
+ readonly [optionKindSymbol]: 'Some' | 'None';
36
+ /**
37
+ * The `isSome` and `isNone` methods return `true` if the `Option` is `Some` or `None`, respectively.
38
+ */
39
+ /**
40
+ * Returns `true` if the Option is a `Some` value.
41
+ */
42
+ isSome(): boolean;
43
+ /**
44
+ * Returns `true` if the Option is a `None` value.
45
+ */
46
+ isNone(): boolean;
47
+ /**
48
+ * Returns `true` if the Option is a `Some` value and the predicate returns `true` for the contained value.
49
+ * @param predicate - A function that takes the contained value and returns a boolean.
50
+ */
51
+ isSomeAnd(predicate: (value: T) => boolean): boolean;
52
+ /**
53
+ * These methods extract the contained value in an `Option<T>` when it is the `Some` variant:
54
+ */
55
+ /**
56
+ * Returns the contained `Some` value, with a provided error message if the value is a `None`.
57
+ * @param msg - The error message to provide if the value is a `None`.
58
+ * @throws {TypeError} Throws an error with the provided message if the Option is a `None`.
59
+ */
60
+ expect(msg: string): T;
61
+ /**
62
+ * Returns the contained `Some` value.
63
+ * @throws {TypeError} Throws an error if the value is a `None`.
64
+ */
65
+ unwrap(): T;
66
+ /**
67
+ * Returns the contained `Some` value or a provided default.
68
+ * @param defaultValue - The value to return if the Option is a `None`.
69
+ */
70
+ unwrapOr(defaultValue: T): T;
71
+ /**
72
+ * Returns the contained `Some` value or computes it from a closure.
73
+ * @param fn - A function that returns the default value.
74
+ */
75
+ unwrapOrElse(fn: () => T): T;
76
+ /**
77
+ * These methods transform `Option` to `Result`:
78
+ */
79
+ /**
80
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
81
+ * @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
82
+ * @param error - The error value to use if the Option is a `None`.
83
+ */
84
+ okOr<E>(error: E): Result<T, E>;
85
+ /**
86
+ * Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
87
+ * @typeParam E - The type of the error value in the `Err` variant of the resulting `Result`.
88
+ * @param err - A function that returns the error value.
89
+ */
90
+ okOrElse<E>(err: () => E): Result<T, E>;
91
+ /**
92
+ * Transposes an `Option` of a `Result` into a `Result` of an `Option`.
93
+ * @typeParam T - The type of the success value in the `Ok` variant of the `Result`.
94
+ * @typeParam E - The type of the error value in the `Err` variant of the `Result`.
95
+ * @returns `Ok` containing `Some` if the Option is a `Some` containing `Ok`,
96
+ * `Err` containing the error if the Option is a `Some` containing `Err`,
97
+ * `Ok` containing `None` if the Option is `None`.
98
+ */
99
+ transpose<T, E>(this: Option<Result<T, E>>): Result<Option<T>, E>;
100
+ /**
101
+ * These methods transform the `Some` variant:
102
+ */
103
+ /**
104
+ * Returns `None` if the Option is `None`, otherwise calls predicate with the wrapped value and returns:
105
+ * - `Some(t)` if predicate returns `true` (where `t` is the wrapped value), and
106
+ * - `None` if predicate returns `false`.
107
+ * @param predicate - A function that takes the contained value and returns a boolean.
108
+ */
109
+ filter(predicate: (value: T) => boolean): Option<T>;
110
+ /**
111
+ * Converts from `Option<Option<T>>` to `Option<T>`.
112
+ * @returns `None` if the Option is `None`, otherwise returns the contained `Option`.
113
+ */
114
+ flatten<T>(this: Option<Option<T>>): Option<T>;
115
+ /**
116
+ * Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
117
+ * @typeParam U - The type of the value returned by the map function.
118
+ * @param fn - A function that takes the contained value and returns a new value.
119
+ */
120
+ map<U>(fn: (value: T) => U): Option<U>;
121
+ /**
122
+ * Maps an `Option<T>` to `U` by applying a function to the contained value (if any), or returns the provided default (if not).
123
+ * @typeParam U - The type of the value returned by the map function or the default value.
124
+ * @param defaultValue - The value to return if the Option is `None`.
125
+ * @param fn - A function that takes the contained value and returns a new value.
126
+ */
127
+ mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
128
+ /**
129
+ * Maps an `Option<T>` to `U` by applying a function to a contained value (if any), or computes a default (if not).
130
+ * @typeParam U - The type of the value returned by the map function or the default function.
131
+ * @param defaultFn - A function that returns the default value.
132
+ * @param fn - A function that takes the contained value and returns a new value.
133
+ */
134
+ mapOrElse<U>(defaultFn: () => U, fn: (value: T) => U): U;
135
+ /**
136
+ * These methods combine the `Some` variants of two `Option` values:
137
+ */
138
+ /**
139
+ * Combines `this` with another `Option` by zipping their contained values.
140
+ * If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some([s, o])`.
141
+ * If either `this` or `other` is `None`, returns `None`.
142
+ * @typeParam U - The type of the value in the other `Option`.
143
+ * @param other - The other `Option` to zip with.
144
+ * @returns An `Option` containing a tuple of the values if both are `Some`, otherwise `None`.
145
+ */
146
+ zip<U>(other: Option<U>): Option<[T, U]>;
147
+ /**
148
+ * Zips `this` with another `Option` using a provided function to combine their contained values.
149
+ * If `this` is `Some(s)` and `other` is `Some(o)`, returns `Some(fn(s, o))`.
150
+ * If either `this` or `other` is `None`, returns `None`.
151
+ * @typeParam U - The type of the value in the other `Option`.
152
+ * @typeParam R - The return type of the combining function.
153
+ * @param other - The other `Option` to zip with.
154
+ * @param fn - The function to combine the values from both `Options`.
155
+ * @returns An `Option` containing the result of `fn` if both `Options` are `Some`, otherwise `None`.
156
+ */
157
+ zipWith<U, R>(other: Option<U>, fn: (value: T, otherValue: U) => R): Option<R>;
158
+ /**
159
+ * Converts from `Option<[T, U]>` to `[Option<T>, Option<U>]`.
160
+ * If `this` is `Some([a, b])`, returns `[Some(a), Some(b)]`.
161
+ * If `this` is `None`, returns `[None, None]`.
162
+ * @typeParam T - The type of the first value in the tuple.
163
+ * @typeParam U - The type of the second value in the tuple.
164
+ * @returns A tuple of `Options`, one for each element in the original `Option` of a tuple.
165
+ */
166
+ unzip<T, U>(this: Option<[T, U]>): [Option<T>, Option<U>];
167
+ /**
168
+ * These methods treat the `Option` as a boolean value, where `Some` acts like `true` and `None` acts like `false`.
169
+ */
170
+ /**
171
+ * Returns `None` if the Option is `None`, otherwise returns `other`.
172
+ * This is sometimes called "and then" because it is similar to a logical AND operation.
173
+ * @typeParam U - The type of the value in the other `Option`.
174
+ * @param other - The `Option` to return if `this` is `Some`.
175
+ * @returns `None` if `this` is `None`, otherwise returns `other`.
176
+ */
177
+ and<U>(other: Option<U>): Option<U>;
178
+ /**
179
+ * Returns `None` if the Option is `None`, otherwise calls `fn` with the wrapped value and returns the result.
180
+ * This function can be used for control flow based on `Option` values.
181
+ * @typeParam U - The type of the value returned by the function.
182
+ * @param fn - A function that takes the contained value and returns an `Option`.
183
+ * @returns The result of `fn` if `this` is `Some`, otherwise `None`.
184
+ */
185
+ andThen<U>(fn: (value: T) => Option<U>): Option<U>;
186
+ /**
187
+ * Returns the Option if it contains a value, otherwise returns `other`.
188
+ * This can be used for providing a fallback `Option`.
189
+ * @param other - The fallback `Option` to use if `this` is `None`.
190
+ * @returns `this` if it is `Some`, otherwise `other`.
191
+ */
192
+ or(other: Option<T>): Option<T>;
193
+ /**
194
+ * Returns the Option if it contains a value, otherwise calls `fn` and returns the result.
195
+ * This method can be used for lazy fallbacks, as `fn` is only evaluated if `this` is `None`.
196
+ * @param fn - A function that produces an `Option`.
197
+ * @returns `this` if it is `Some`, otherwise the result of `fn`.
198
+ */
199
+ orElse(fn: () => Option<T>): Option<T>;
200
+ /**
201
+ * Returns `Some` if exactly one of `this`, `other` is `Some`, otherwise returns `None`.
202
+ * This can be thought of as an exclusive or operation on `Option` values.
203
+ * @param other - The other `Option` to compare with.
204
+ * @returns `Some` if exactly one of `this` and `other` is `Some`, otherwise `None`.
205
+ */
206
+ xor(other: Option<T>): Option<T>;
207
+ /**
208
+ * Calls the provided function with the contained value if `this` is `Some`.
209
+ * This is primarily for side effects and does not transform the `Option`.
210
+ * @param fn - A function to call with the contained value.
211
+ * @returns `this`, unmodified, for chaining additional methods.
212
+ */
213
+ inspect(fn: (value: T) => void): this;
214
+ /**
215
+ * Tests whether `this` and `other` are both `Some` containing equal values, or both are `None`.
216
+ * This method can be used for comparing `Option` instances in a value-sensitive manner.
217
+ * @param other - The other `Option` to compare with.
218
+ * @returns `true` if `this` and `other` are both `Some` with equal values, or both are `None`, otherwise `false`.
219
+ */
220
+ eq(other: Option<T>): boolean;
221
+ }
31
222
  /**
32
- * option::None type
223
+ * The `Result` type is used for returning and propagating errors.
224
+ * It is an enum with the variants, `Ok(T)`, representing success and containing a value, and `Err(E)`, representing error and containing an error value.
225
+ * This interface includes methods that act on the `Result` type, similar to Rust's `Result` enum.
226
+ *
227
+ * As Rust Code:
228
+ ```rust
229
+ pub enum Result<T, E> {
230
+ Ok(T),
231
+ Err(E),
232
+ }
233
+ ```
234
+ * @typeParam T - The type of the value contained in a successful `Result`.
235
+ * @typeParam E - The type of the error contained in an unsuccessful `Result`.
33
236
  */
34
- interface None {
35
- readonly kind: 'None';
36
- readonly isSome: <T>(this: Option<T>) => this is Some<T>;
37
- readonly isNone: <T>(this: Option<T>) => this is None;
38
- readonly unwrap: () => never;
237
+ interface Result<T, E> {
238
+ /**
239
+ * Identify `Ok` or `Err`.
240
+ *
241
+ * @private
242
+ */
243
+ readonly [resultKindSymbol]: 'Ok' | 'Err';
244
+ /**
245
+ * The `isOk` and `isErr` methods return `true` if the `Result` is `Ok` or `Err`, respectively.
246
+ */
247
+ /**
248
+ * Returns `true` if the result is `Ok`.
249
+ */
250
+ isOk(): boolean;
251
+ /**
252
+ * Returns `true` if the result is `Err`.
253
+ */
254
+ isErr(): boolean;
255
+ /**
256
+ * Returns `true` if the result is `Ok` and the provided predicate returns `true` for the contained value.
257
+ * @param predicate - A function that takes the `Ok` value and returns a boolean.
258
+ */
259
+ isOkAnd(predicate: (value: T) => boolean): boolean;
260
+ /**
261
+ * Returns `true` if the result is `Err` and the provided predicate returns `true` for the contained error.
262
+ * @param predicate - A function that takes the `Err` value and returns a boolean.
263
+ */
264
+ isErrAnd(predicate: (error: E) => boolean): boolean;
265
+ /**
266
+ * These methods extract the contained value in a `Result<T, E>` when it is the `Ok` variant.
267
+ */
268
+ /**
269
+ * Returns the contained `Ok` value, with a provided error message if the result is `Err`.
270
+ * @param msg - The error message to provide if the result is an `Err`.
271
+ * @throws {TypeError} Throws an error with the provided message if the result is an `Err`.
272
+ */
273
+ expect(msg: string): T;
274
+ /**
275
+ * Returns the contained `Ok` value.
276
+ * @throws {TypeError} Throws an error if the result is an `Err`.
277
+ */
278
+ unwrap(): T;
279
+ /**
280
+ * Returns the contained `Ok` value or a provided default.
281
+ * @param defaultValue - The value to return if the result is an `Err`.
282
+ */
283
+ unwrapOr(defaultValue: T): T;
284
+ /**
285
+ * Returns the contained `Ok` value or computes it from a closure if the result is `Err`.
286
+ * @param fn - A function that takes the `Err` value and returns an `Ok` value.
287
+ */
288
+ unwrapOrElse(fn: (error: E) => T): T;
289
+ /**
290
+ * These methods extract the contained value in a `Result<T, E>` when it is the `Err` variant.
291
+ */
292
+ /**
293
+ * Returns the contained `Err` value, with a provided error message if the result is `Ok`.
294
+ * @param msg - The error message to provide if the result is an `Ok`.
295
+ * @throws {TypeError} Throws an error with the provided message if the result is an `Ok`.
296
+ */
297
+ expectErr(msg: string): E;
298
+ /**
299
+ * Returns the contained `Err` value.
300
+ * @throws {TypeError} Throws an error if the result is an `Ok`.
301
+ */
302
+ unwrapErr(): E;
303
+ /**
304
+ * These methods transform `Result` to `Option`:
305
+ */
306
+ /**
307
+ * Converts from `Result<T, E>` to `Option<T>`.
308
+ * If the result is `Ok`, returns `Some(T)`.
309
+ * If the result is `Err`, returns `None`.
310
+ */
311
+ ok(): Option<T>;
312
+ /**
313
+ * Converts from `Result<T, E>` to `Option<E>`.
314
+ * If the result is `Err`, returns `Some(E)`.
315
+ * If the result is `Ok`, returns `None`.
316
+ */
317
+ err(): Option<E>;
318
+ /**
319
+ * Transposes a `Result` of an `Option` into an `Option` of a `Result`.
320
+ * @typeParam T - The type of the success value in the `Ok` variant of the `Option`.
321
+ * @returns `Some` containing `Ok` if the result is `Ok` containing `Some`,
322
+ * `Some` containing `Err` if the result is `Err`,
323
+ * `None` if the result is `Ok` containing `None`.
324
+ */
325
+ transpose<T>(this: Result<Option<T>, E>): Option<Result<T, E>>;
326
+ /**
327
+ * This method transforms the contained value of the `Ok` variant:
328
+ */
329
+ /**
330
+ * Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value,
331
+ * leaving an `Err` value untouched.
332
+ * @typeParam U - The type of the value returned by the map function.
333
+ * @param fn - A function that takes the `Ok` value and returns a new value.
334
+ */
335
+ map<U>(fn: (value: T) => U): Result<U, E>;
336
+ /**
337
+ * This method transforms the contained value of the `Err` variant:
338
+ */
339
+ /**
340
+ * Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value,
341
+ * leaving an `Ok` value untouched.
342
+ * @typeParam F - The type of the error returned by the map function.
343
+ * @param fn - A function that takes the `Err` value and returns a new error value.
344
+ */
345
+ mapErr<F>(fn: (error: E) => F): Result<T, F>;
346
+ /**
347
+ * These methods transform a `Result<T, E>` into a value of a possibly different type `U`:
348
+ */
349
+ /**
350
+ * Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or returns the provided default (if `Err`).
351
+ * @typeParam U - The type of the value returned by the map function or the default value.
352
+ * @param defaultValue - The value to return if the result is `Err`.
353
+ * @param fn - A function that takes the `Ok` value and returns a new value.
354
+ */
355
+ mapOr<U>(defaultValue: U, fn: (value: T) => U): U;
356
+ /**
357
+ * Maps a `Result<T, E>` to `U` by applying a function to the contained `Ok` value (if `Ok`), or computes a default (if `Err`).
358
+ * @typeParam U - The type of the value returned by the map function or the default function.
359
+ * @param defaultFn - A function that returns the default value.
360
+ * @param fn - A function that takes the `Ok` value and returns a new value.
361
+ */
362
+ mapOrElse<U>(defaultFn: (error: E) => U, fn: (value: T) => U): U;
363
+ /**
364
+ * Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
365
+ * If the result is `Ok(Ok(T))`, returns `Ok(T)`.
366
+ * If the result is `Ok(Err(E))` or `Err(E)`, returns `Err(E)`.
367
+ */
368
+ flatten<T>(this: Result<Result<T, E>, E>): Result<T, E>;
369
+ /**
370
+ * These methods treat the `Result` as a boolean value, where `Ok` acts like `true` and `Err` acts like `false`.
371
+ */
372
+ /**
373
+ * Returns `this` if the result is `Err`, otherwise returns the passed `Result`.
374
+ * @typeParam U - The type of the value in the other `Result`.
375
+ * @param other - The `Result` to return if `this` is `Ok`.
376
+ * @returns The passed `Result` if `this` is `Ok`, otherwise returns `this` (which is `Err`).
377
+ */
378
+ and<U>(other: Result<U, E>): Result<U, E>;
379
+ /**
380
+ * Returns `this` if it is `Ok`, otherwise returns the passed `Result`.
381
+ * @typeParam F - The type of the error in the other `Result`.
382
+ * @param other - The `Result` to return if `this` is `Err`.
383
+ * @returns `this` if it is `Ok`, otherwise returns `other`.
384
+ */
385
+ or<F>(other: Result<T, F>): Result<T, F>;
386
+ /**
387
+ * Calls the provided function with the contained value if `this` is `Ok`, otherwise returns `this` as `Err`.
388
+ * @typeParam U - The type of the value returned by the function.
389
+ * @param fn - A function that takes the `Ok` value and returns a `Result`.
390
+ * @returns The result of `fn` if `this` is `Ok`, otherwise `this` as `Err`.
391
+ */
392
+ andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
393
+ /**
394
+ * Calls the provided function with the contained error if `this` is `Err`, otherwise returns `this` as `Ok`.
395
+ * @typeParam F - The type of the error returned by the function.
396
+ * @param fn - A function that takes the `Err` value and returns a `Result`.
397
+ * @returns The result of `fn` if `this` is `Err`, otherwise `this` as `Ok`.
398
+ */
399
+ orElse<F>(fn: (error: E) => Result<T, F>): Result<T, F>;
400
+ /**
401
+ * Calls the provided function with the contained value if `this` is `Ok`, for side effects only.
402
+ * Does not modify the `Result`.
403
+ * @param fn - A function to call with the `Ok` value.
404
+ * @returns `this`, unmodified.
405
+ */
406
+ inspect(fn: (value: T) => void): this;
407
+ /**
408
+ * Calls the provided function with the contained error if `this` is `Err`, for side effects only.
409
+ * Does not modify the `Result`.
410
+ * @param fn - A function to call with the `Err` value.
411
+ * @returns `this`, unmodified.
412
+ */
413
+ inspectErr(fn: (error: E) => void): this;
414
+ /**
415
+ * Tests whether `this` and `other` are both `Ok` containing equal values, or both are `Err` containing equal errors.
416
+ * @param other - The other `Result` to compare with.
417
+ * @returns `true` if `this` and `other` are both `Ok` with equal values, or both are `Err` with equal errors, otherwise `false`.
418
+ */
419
+ eq(other: Result<T, E>): boolean;
39
420
  }
40
421
  /**
41
- * `None` value is freeze.
422
+ * Export some commonly used types.
423
+ */
424
+ /**
425
+ * Represents an asynchronous operation that yields an `Option<T>`.
426
+ * This is a promise that resolves to either `Some(T)` if the value is present, or `None` if the value is absent.
42
427
  *
43
- * @constant {None}
428
+ * @typeParam T - The type of the value that may be contained within the `Option`.
44
429
  */
45
- declare const None: None;
46
-
430
+ type AsyncOption<T> = Promise<Option<T>>;
47
431
  /**
48
- * result::Result type
432
+ * Represents an asynchronous operation that yields a `Result<T, E>`.
433
+ * This is a promise that resolves to `Ok(T)` if the operation was successful, or `Err(E)` if there was an error.
434
+ *
435
+ * @typeParam T - The type of the value that is produced by a successful operation.
436
+ * @typeParam E - The type of the error that may be produced by a failed operation.
49
437
  */
50
- type Result<T, E> = Ok<T, E> | Err<T, E>;
438
+ type AsyncResult<T, E> = Promise<Result<T, E>>;
51
439
  /**
52
- * @fileoverview A Rust-inspired [Result](https://doc.rust-lang.org/core/result/index.html) enum, used for better error handling.
440
+ * Represents a synchronous operation that yields a `Result<T, Error>`.
441
+ * This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error.
442
+ *
443
+ * @typeParam T - The type of the value that is produced by a successful operation.
53
444
  */
445
+ type IOResult<T> = Result<T, Error>;
54
446
  /**
55
- * result::Ok type
447
+ * Represents an asynchronous I/O operation that yields a `Result<T, Error>`.
448
+ * This is a promise that resolves to `Ok(T)` if the I/O operation was successful, or `Err(Error)` if there was an error.
449
+ *
450
+ * @typeParam T - The type of the value that is produced by a successful I/O operation.
56
451
  */
57
- interface Ok<T, E> {
58
- readonly kind: 'Ok';
59
- readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
60
- readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
61
- readonly unwrap: () => T;
62
- readonly err: () => never;
63
- }
452
+ type AsyncIOResult<T> = Promise<IOResult<T>>;
64
453
  /**
65
- * Create an `Ok` object.
454
+ * Creates an `Option<T>` representing the presence of a value.
455
+ * This function is typically used to construct an `Option` that contains a value, indicating that the operation yielding the value was successful.
66
456
  *
67
- * # Examples
457
+ * @typeParam T - The type of the value to be wrapped in a `Some`.
458
+ * @param value - The value to wrap as a `Some` option.
459
+ * @returns An `Option<T>` that contains the provided value, representing the `Some` case.
68
460
  *
461
+ * @example
462
+ * ```ts
463
+ * const maybeValue = Some(1); // Option<number> with a value
464
+ * if (maybeValue.isSome()) {
465
+ * console.log(maybeValue.unwrap()); // Outputs: 1
466
+ * }
69
467
  * ```
70
- * const v = Ok(10);
71
- * console.assert(v.unwrap() === 10);
72
- *
73
- function judge(n: number): Option<Promise<Result<number, Error>>> {
74
- if (n < 0 || n >= 1) {
75
- return None;
76
- }
77
-
78
- return Some(new Promise(resolve => {
79
- const r = Math.random();
80
- resolve(r > n ? Ok(r) : Err(new Error('lose')));
81
- }));
82
- }
83
-
84
- const res = judge(0.8);
85
- if (res.isNone()) {
86
- console.error('invalid number');
87
- } else {
88
- const result = await res.unwrap();
89
- if (result.isErr()) {
90
- console.assert(result.err().message === 'lose');
91
- } else {
92
- console.log(result.unwrap()); // must greater than 0.8
93
- }
94
- }
95
- *
96
- * ```
97
- *
98
- * @param value The wrapped value.
99
- * @returns {Ok}
100
468
  */
101
- declare function Ok<T, E>(value: T): Result<T, E>;
469
+ declare function Some<T>(value: T): Option<T>;
102
470
  /**
103
- * result::Err type
471
+ * Represents the absence of a value, as a specialized `Option` type.
472
+ * The type parameter is set to `never` because `None` does not hold a value.
104
473
  */
105
- interface Err<T, E> {
106
- readonly kind: 'Err';
107
- readonly isOk: (this: Result<T, E>) => this is Ok<T, E>;
108
- readonly isErr: (this: Result<T, E>) => this is Err<T, E>;
109
- readonly unwrap: () => never;
110
- readonly err: () => E;
474
+ interface None extends Option<never> {
475
+ /**
476
+ * When using `None` alone, the following overrides can make type inference more accurate.
477
+ */
478
+ readonly [optionKindSymbol]: 'None';
479
+ unwrapOr<T>(defaultValue: T): T;
480
+ unwrapOrElse<T>(fn: () => T): T;
481
+ transpose(): Result<None, never>;
482
+ filter(predicate: (value: never) => boolean): None;
483
+ flatten(): None;
484
+ map<U>(fn: (value: never) => U): None;
485
+ zip<U>(other: Option<U>): None;
486
+ zipWith<U, R>(other: Option<U>, fn: (value: never, otherValue: U) => R): None;
487
+ unzip(): [None, None];
488
+ and<U>(other: Option<U>): None;
489
+ andThen<U>(fn: (value: never) => Option<U>): None;
490
+ or<T>(other: Option<T>): Option<T>;
491
+ orElse<T>(fn: () => Option<T>): Option<T>;
492
+ xor<T>(other: Option<T>): Option<T>;
493
+ eq<T>(other: Option<T>): boolean;
111
494
  }
112
495
  /**
113
- * Create an `Err` object.
496
+ * A constant representing the `None` case of an `Option`, indicating the absence of a value.
497
+ * This constant is frozen to ensure it is immutable and cannot be altered, preserving the integrity of `None` throughout the application.
498
+ */
499
+ declare const None: None;
500
+ /**
501
+ * Creates a `Result<T, E>` representing a successful outcome containing a value.
502
+ * This function is used to construct a `Result` that signifies the operation was successful by containing the value `T`.
114
503
  *
115
- * # Examples
504
+ * @typeParam T - The type of the value to be contained in the `Ok` result.
505
+ * @typeParam E - The type of the error that the result could potentially contain (not used in this case).
506
+ * @param value - The value to wrap as an `Ok` result.
507
+ * @returns A `Result<T, E>` that contains the provided value, representing the `Ok` case.
116
508
  *
509
+ * @example
510
+ * ```ts
511
+ * const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
512
+ * if (goodResult.isOk()) {
513
+ * console.log(goodResult.unwrap()); // Outputs: 1
514
+ * }
117
515
  * ```
118
- * const e = Err(new Error('unknown error'));
119
- * console.assert(e.err().message === 'unknown error');
120
- * ```
121
- *
122
- * @param error The wrapped error value.
123
- * @returns {Err}
124
516
  */
125
- declare function Err<T, E>(error: E): Result<T, E>;
126
-
127
- /**
128
- * The shorthand of Promise<Option<T>>.
129
- */
130
- type AsyncOption<T> = Promise<Option<T>>;
131
- /**
132
- * The shorthand of Promise<Result<T, E>>.
133
- */
134
- type AsyncResult<T, E> = Promise<Result<T, E>>;
517
+ declare function Ok<T, E>(value: T): Result<T, E>;
135
518
  /**
136
- * The shorthand of Result<T, Error>.
519
+ * Creates a `Result<T, E>` representing a failed outcome containing an error.
520
+ * This function is used to construct a `Result` that signifies the operation failed by containing the error `E`.
521
+ *
522
+ * @typeParam T - The type of the value that the result could potentially contain (not used in this case).
523
+ * @typeParam E - The type of the error to be wrapped in the `Err` result.
524
+ * @param error - The error to wrap as an `Err` result.
525
+ * @returns A `Result<T, E>` that contains the provided error, representing the `Err` case.
526
+ *
527
+ * @example
528
+ * ```ts
529
+ * const badResult = Err<number, Error>(new Error('Something went wrong'));
530
+ * if (badResult.isErr()) {
531
+ * console.error(badResult.unwrapErr()); // Outputs: Error: Something went wrong
532
+ * }
533
+ * ```
137
534
  */
138
- type IOResult<T> = Result<T, Error>;
535
+ declare function Err<T, E>(error: E): Result<T, E>;
139
536
  /**
140
- * The shorthand of Promise<Result<T, Error>>.
537
+ * Converts a Promise to a Result type, capturing the resolved value in an `Ok`, or the error in an `Err`.
538
+ * This allows for promise-based asynchronous operations to be handled in a way that is more in line with the Result pattern.
539
+ *
540
+ * @typeParam T - The type of the value that the promise resolves to.
541
+ * @typeParam E - The type of the error that the promise may reject with, defaults to `Error`.
542
+ * @param p - The promise to convert into a `Result` type.
543
+ * @returns A promise that resolves to a `Result<T, E>`. If the input promise `p` resolves, the resulting promise will resolve with `Ok<T>`. If the input promise `p` rejects, the resulting promise will resolve with `Err<E>`.
544
+ *
545
+ * @example
546
+ * ```ts
547
+ * async function example() {
548
+ * const result = await promiseToResult(fetchData());
549
+ * if (result.isOk()) {
550
+ * console.log('Data:', result.unwrap());
551
+ * } else {
552
+ * console.error('Error:', result.unwrapErr());
553
+ * }
554
+ * }
555
+ * ```
141
556
  */
142
- type AsyncIOResult<T> = Promise<IOResult<T>>;
557
+ declare function promiseToResult<T, E = Error>(p: Promise<T>): Promise<Result<T, E>>;
143
558
 
144
- export { type AsyncIOResult, type AsyncOption, type AsyncResult, Err, type IOResult, None, Ok, type Option, type Result, Some };
559
+ export { type AsyncIOResult, type AsyncOption, type AsyncResult, Err, type IOResult, None, Ok, type Option, type Result, Some, promiseToResult };
145
560
  //# sourceMappingURL=types.d.ts.map