unthrown 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +198 -42
- package/dist/index.d.cts +284 -56
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +284 -56
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +196 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -34,7 +34,7 @@ type Bound<T, K extends string, U> = Prettify<Omit<T, K> & { readonly [P in K]:
|
|
|
34
34
|
type NotThenable<R> = [R] extends [PromiseLike<unknown>] ? "unthrown: combinator callbacks are synchronous — lift async work with fromPromise and compose with flatMap" : unknown;
|
|
35
35
|
/**
|
|
36
36
|
* The fluent method surface every {@link Result} variant carries — the
|
|
37
|
-
* combinators (`map`, `flatMap`, `mapErr`, `match`, `
|
|
37
|
+
* combinators (`map`, `flatMap`, `mapErr`, `match`, `get`, …), documented one
|
|
38
38
|
* per entry below. Factored out so the three variants ({@link OkView},
|
|
39
39
|
* {@link ErrView}, {@link DefectView}) can each intersect it; {@link AsyncResult}
|
|
40
40
|
* mirrors this surface with async signatures.
|
|
@@ -79,6 +79,15 @@ type ResultMethods<T, E> = {
|
|
|
79
79
|
* Runs only on `Ok`. If `f` throws, the throw becomes a `Defect`. An async
|
|
80
80
|
* callback is rejected at compile time ({@link NotThenable}).
|
|
81
81
|
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* `f`'s return value is **ignored** — a `Result` returned by the effect
|
|
84
|
+
* compiles but is discarded, `Err` and all. If the effect can fail, sequence
|
|
85
|
+
* it instead of tapping it: a `Result`-returning effect goes in
|
|
86
|
+
* {@link ResultMethods.flatTap | flatTap}; an `AsyncResult`-returning effect
|
|
87
|
+
* cannot be sequenced from the sync surface — lift the chain with
|
|
88
|
+
* {@link ResultMethods.toAsync | toAsync} and use the async
|
|
89
|
+
* {@link AsyncResultMethods.flatTap | flatTap} (which accepts both).
|
|
90
|
+
*
|
|
82
91
|
* @param f - the side effect (its return value is ignored).
|
|
83
92
|
*/
|
|
84
93
|
tap<R>(f: (value: T) => R & NotThenable<R>): Result$1<T, E>;
|
|
@@ -155,7 +164,8 @@ type ResultMethods<T, E> = {
|
|
|
155
164
|
*/
|
|
156
165
|
mapErr<E2>(f: (error: E) => E2 & NotThenable<E2>): Result$1<T, E2>;
|
|
157
166
|
/**
|
|
158
|
-
*
|
|
167
|
+
* Sequence from an `Err` by producing another `Result` — the error-channel
|
|
168
|
+
* mirror of {@link ResultMethods.flatMap | flatMap}.
|
|
159
169
|
*
|
|
160
170
|
* Runs `f` only on `Err`; `Ok` and `Defect` pass through. If `f` throws, the
|
|
161
171
|
* throw becomes a `Defect`.
|
|
@@ -164,10 +174,22 @@ type ResultMethods<T, E> = {
|
|
|
164
174
|
* @typeParam E2 - the error type `f` may produce instead.
|
|
165
175
|
* @param f - produces a fallback `Result` from the current error.
|
|
166
176
|
*/
|
|
177
|
+
flatMapErr<U, E2>(f: (error: E) => Result$1<U, E2>): Result$1<T | U, E2>;
|
|
178
|
+
/**
|
|
179
|
+
* Sequence from an `Err` by producing another `Result`.
|
|
180
|
+
*
|
|
181
|
+
* @deprecated Renamed to {@link ResultMethods.flatMapErr | flatMapErr} — it is
|
|
182
|
+
* `flatMap` on the error channel, so it now follows the `…Err` convention. This
|
|
183
|
+
* alias will be removed in a future major.
|
|
184
|
+
*
|
|
185
|
+
* @typeParam U - an alternative success type `f` may produce.
|
|
186
|
+
* @typeParam E2 - the error type `f` may produce instead.
|
|
187
|
+
* @param f - produces a fallback `Result` from the current error.
|
|
188
|
+
*/
|
|
167
189
|
orElse<U, E2>(f: (error: E) => Result$1<U, E2>): Result$1<T | U, E2>;
|
|
168
190
|
/**
|
|
169
191
|
* Recover from an `Err` by producing a success value, emptying the error
|
|
170
|
-
* channel.
|
|
192
|
+
* channel. Pairs with {@link ResultMethods.recoverDefect | recoverDefect}.
|
|
171
193
|
*
|
|
172
194
|
* @remarks
|
|
173
195
|
* The result type is `Result<T | U, never>`, but `never` describes only the
|
|
@@ -179,6 +201,17 @@ type ResultMethods<T, E> = {
|
|
|
179
201
|
* @typeParam U - the recovered success type.
|
|
180
202
|
* @param f - produces a success value from the current error.
|
|
181
203
|
*/
|
|
204
|
+
recoverErr<U>(f: (error: E) => U & NotThenable<U>): Result$1<T | U, never>;
|
|
205
|
+
/**
|
|
206
|
+
* Recover from an `Err` by producing a success value.
|
|
207
|
+
*
|
|
208
|
+
* @deprecated Renamed to {@link ResultMethods.recoverErr | recoverErr} — it now
|
|
209
|
+
* pairs with {@link ResultMethods.recoverDefect | recoverDefect} and follows the
|
|
210
|
+
* `…Err` convention. This alias will be removed in a future major.
|
|
211
|
+
*
|
|
212
|
+
* @typeParam U - the recovered success type.
|
|
213
|
+
* @param f - produces a success value from the current error.
|
|
214
|
+
*/
|
|
182
215
|
recover<U>(f: (error: E) => U & NotThenable<U>): Result$1<T | U, never>;
|
|
183
216
|
/**
|
|
184
217
|
* Run a side effect on the error and pass the `Result` through unchanged.
|
|
@@ -188,6 +221,14 @@ type ResultMethods<T, E> = {
|
|
|
188
221
|
* never destroys it. An async callback is rejected at compile time
|
|
189
222
|
* ({@link NotThenable}).
|
|
190
223
|
*
|
|
224
|
+
* @remarks
|
|
225
|
+
* As with {@link ResultMethods.tap | tap}, `f`'s return value is ignored — a
|
|
226
|
+
* failable `Result`-returning effect belongs in
|
|
227
|
+
* {@link ResultMethods.flatTapErr | flatTapErr}; an `AsyncResult`-returning
|
|
228
|
+
* one needs the chain lifted with {@link ResultMethods.toAsync | toAsync}
|
|
229
|
+
* first (the async {@link AsyncResultMethods.flatTapErr | flatTapErr}
|
|
230
|
+
* accepts both).
|
|
231
|
+
*
|
|
191
232
|
* @param f - the side effect (its return value is ignored).
|
|
192
233
|
*/
|
|
193
234
|
tapErr<R>(f: (error: E) => R & NotThenable<R>): Result$1<T, E>;
|
|
@@ -256,14 +297,23 @@ type ResultMethods<T, E> = {
|
|
|
256
297
|
*
|
|
257
298
|
* @remarks
|
|
258
299
|
* Compiles only when the error channel is empty (`E = never`) — eliminate
|
|
259
|
-
* modeled errors first (`match` / `
|
|
260
|
-
* `
|
|
300
|
+
* modeled errors first (`match` / `recoverErr` / `flatMapErr`), or reach for the
|
|
301
|
+
* `getOr` / `getOrElse` / `getOrNull` / `getOrUndefined` family (which
|
|
261
302
|
* recover an `Err`). If you get a `'this' context` type error here, that is
|
|
262
303
|
* the gate: the receiver still has a non-`never` error channel.
|
|
263
304
|
*
|
|
264
305
|
* `E = never` empties only the **modeled** error channel — a `Defect` can
|
|
265
|
-
* still be present, and `
|
|
266
|
-
* _panics_); `Result<T, never>` does not mean `
|
|
306
|
+
* still be present, and `get()` **rethrows its original cause** (it
|
|
307
|
+
* _panics_); `Result<T, never>` does not mean `get()` cannot throw.
|
|
308
|
+
*
|
|
309
|
+
* @returns the `Ok` value.
|
|
310
|
+
*/
|
|
311
|
+
get(this: Result$1<T, never>): T;
|
|
312
|
+
/**
|
|
313
|
+
* Extract the success value.
|
|
314
|
+
*
|
|
315
|
+
* @deprecated Renamed to {@link ResultMethods.get | get}, unifying the extractor
|
|
316
|
+
* family under `get…`. This alias will be removed in a future major.
|
|
267
317
|
*
|
|
268
318
|
* @returns the `Ok` value.
|
|
269
319
|
*/
|
|
@@ -277,7 +327,16 @@ type ResultMethods<T, E> = {
|
|
|
277
327
|
* `Result` you hold usually still has a success type), so to inspect an
|
|
278
328
|
* error prefer an `isErr()` guard or, in tests, `@unthrown/vitest`'s
|
|
279
329
|
* `toBeErrWith`. A `Defect` still **rethrows its original cause** (a defect is
|
|
280
|
-
* a bug, not an absent value), so this does not mean `
|
|
330
|
+
* a bug, not an absent value), so this does not mean `getErr()` can't throw.
|
|
331
|
+
*
|
|
332
|
+
* @returns the `Err` value.
|
|
333
|
+
*/
|
|
334
|
+
getErr(this: Result$1<never, E>): E;
|
|
335
|
+
/**
|
|
336
|
+
* Extract the modeled error.
|
|
337
|
+
*
|
|
338
|
+
* @deprecated Renamed to {@link ResultMethods.getErr | getErr}, unifying the
|
|
339
|
+
* extractor family under `get…`. This alias will be removed in a future major.
|
|
281
340
|
*
|
|
282
341
|
* @returns the `Err` value.
|
|
283
342
|
*/
|
|
@@ -290,6 +349,17 @@ type ResultMethods<T, E> = {
|
|
|
290
349
|
* @throws Re-throws on a `Defect` — a Defect is a bug, not an absent value, so
|
|
291
350
|
* it is never silently replaced.
|
|
292
351
|
*/
|
|
352
|
+
getOr<U>(fallback: U): T | U;
|
|
353
|
+
/**
|
|
354
|
+
* The success value, or `fallback` on `Err`.
|
|
355
|
+
*
|
|
356
|
+
* @deprecated Renamed to {@link ResultMethods.getOr | getOr}, unifying the
|
|
357
|
+
* extractor family under `get…`. This alias will be removed in a future major.
|
|
358
|
+
*
|
|
359
|
+
* @typeParam U - the fallback type (may differ from `T`; the return widens to `T | U`).
|
|
360
|
+
* @param fallback - returned when the result is an `Err`.
|
|
361
|
+
* @throws Re-throws on a `Defect`.
|
|
362
|
+
*/
|
|
293
363
|
unwrapOr<U>(fallback: U): T | U;
|
|
294
364
|
/**
|
|
295
365
|
* The success value, or `f(error)` on `Err`.
|
|
@@ -298,6 +368,17 @@ type ResultMethods<T, E> = {
|
|
|
298
368
|
* @param f - lazily computes the fallback from the error (may return a different type; the return widens to `T | U`).
|
|
299
369
|
* @throws Re-throws on a `Defect`.
|
|
300
370
|
*/
|
|
371
|
+
getOrElse<U>(f: (error: E) => U): T | U;
|
|
372
|
+
/**
|
|
373
|
+
* The success value, or `f(error)` on `Err`.
|
|
374
|
+
*
|
|
375
|
+
* @deprecated Renamed to {@link ResultMethods.getOrElse | getOrElse}, unifying
|
|
376
|
+
* the extractor family under `get…`. This alias will be removed in a future major.
|
|
377
|
+
*
|
|
378
|
+
* @typeParam U - the fallback type (may differ from `T`; the return widens to `T | U`).
|
|
379
|
+
* @param f - lazily computes the fallback from the error.
|
|
380
|
+
* @throws Re-throws on a `Defect`.
|
|
381
|
+
*/
|
|
301
382
|
unwrapOrElse<U>(f: (error: E) => U): T | U;
|
|
302
383
|
/**
|
|
303
384
|
* The success value, or `null` on `Err`.
|
|
@@ -310,7 +391,25 @@ type ResultMethods<T, E> = {
|
|
|
310
391
|
*
|
|
311
392
|
* @throws Re-throws on a `Defect`.
|
|
312
393
|
*/
|
|
313
|
-
getOrUndefined(): T | undefined;
|
|
394
|
+
getOrUndefined(): T | undefined;
|
|
395
|
+
/**
|
|
396
|
+
* The success value, or **throw** the modeled error on `Err`.
|
|
397
|
+
*
|
|
398
|
+
* @remarks
|
|
399
|
+
* A deliberate escape hatch off the errors-as-values model. Unlike
|
|
400
|
+
* {@link ResultMethods.get | get} (type-gated to an empty error
|
|
401
|
+
* channel), this compiles on any `Result<T, E>` and **throws the `Err` value
|
|
402
|
+
* as-is** at the call site. Its purpose is to move a literal `throw` behind a
|
|
403
|
+
* method, so a `no-throw` lint rule can ban raw throws while this one
|
|
404
|
+
* sanctioned extraction remains — _not_ to replace principled handling. When
|
|
405
|
+
* you can keep the error a value, prefer {@link ResultMethods.match | match} /
|
|
406
|
+
* {@link ResultMethods.recoverErr | recoverErr} / {@link ResultMethods.flatMapErr | flatMapErr}.
|
|
407
|
+
*
|
|
408
|
+
* @returns the `Ok` value.
|
|
409
|
+
* @throws the modeled `error` on `Err`; re-throws the original `cause` on a
|
|
410
|
+
* `Defect` (a panic, like the rest of the `getOr…` family).
|
|
411
|
+
*/
|
|
412
|
+
getOrThrow(): T; /** Whether this result is `Ok` — narrows `this` to its {@link OkView} on `true`. */
|
|
314
413
|
isOk(): this is OkView<T, E>; /** Whether this result is `Err` — narrows `this` to its {@link ErrView} on `true`. */
|
|
315
414
|
isErr(): this is ErrView<E, T>; /** Whether this result is a `Defect` — narrows `this` to its {@link DefectView} on `true`. */
|
|
316
415
|
isDefect(): this is DefectView<T, E>; /** Lift this synchronous `Result` into an {@link AsyncResult}. */
|
|
@@ -433,7 +532,7 @@ type Awaitable<T> = {
|
|
|
433
532
|
};
|
|
434
533
|
/**
|
|
435
534
|
* The async method surface every {@link AsyncResult} carries — the combinators
|
|
436
|
-
* (`map`, `flatMap`, `mapErr`, `match`, `
|
|
535
|
+
* (`map`, `flatMap`, `mapErr`, `match`, `get`, …) with their asynchronous
|
|
437
536
|
* signatures, documented one per entry below. The async mirror of
|
|
438
537
|
* {@link ResultMethods}: each entry links its synchronous counterpart and states
|
|
439
538
|
* only the async delta.
|
|
@@ -467,7 +566,12 @@ type AsyncResultMethods<T, E> = {
|
|
|
467
566
|
/**
|
|
468
567
|
* Asynchronous {@link ResultMethods.tap | tap}. `f` is synchronous; a throw
|
|
469
568
|
* becomes a `Defect`. An async callback is rejected at compile time
|
|
470
|
-
* ({@link NotThenable})
|
|
569
|
+
* ({@link NotThenable}) — and so is a returned `AsyncResult` (it is
|
|
570
|
+
* awaitable). Beware the near-miss: _calling_ an `AsyncResult`-returning
|
|
571
|
+
* effect inside the callback without returning it compiles and leaves the
|
|
572
|
+
* effect floating — fire-and-forget, never awaited, its `Err`/`Defect`
|
|
573
|
+
* unobserved. If the effect returns a `Result`/`AsyncResult`, use
|
|
574
|
+
* {@link AsyncResultMethods.flatTap | flatTap}.
|
|
471
575
|
*/
|
|
472
576
|
tap<R>(f: (value: T) => R & NotThenable<R>): AsyncResult$1<T, E>;
|
|
473
577
|
/**
|
|
@@ -497,21 +601,34 @@ type AsyncResultMethods<T, E> = {
|
|
|
497
601
|
*/
|
|
498
602
|
mapErr<E2>(f: (error: E) => E2 & NotThenable<E2>): AsyncResult$1<T, E2>;
|
|
499
603
|
/**
|
|
500
|
-
* Asynchronous {@link ResultMethods.
|
|
501
|
-
* or an `AsyncResult`.
|
|
604
|
+
* Asynchronous {@link ResultMethods.flatMapErr | flatMapErr}. `f` may return a
|
|
605
|
+
* `Result` or an `AsyncResult`.
|
|
606
|
+
*/
|
|
607
|
+
flatMapErr<U, E2>(f: (error: E) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<T | U, E2>;
|
|
608
|
+
/**
|
|
609
|
+
* @deprecated Renamed to {@link AsyncResultMethods.flatMapErr | flatMapErr}.
|
|
610
|
+
* This alias will be removed in a future major.
|
|
502
611
|
*/
|
|
503
612
|
orElse<U, E2>(f: (error: E) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<T | U, E2>;
|
|
504
613
|
/**
|
|
505
|
-
* Asynchronous {@link ResultMethods.
|
|
506
|
-
* throw becomes a `Defect`. An async callback is rejected at
|
|
507
|
-
* ({@link NotThenable}).
|
|
614
|
+
* Asynchronous {@link ResultMethods.recoverErr | recoverErr}. `f` is
|
|
615
|
+
* synchronous; a throw becomes a `Defect`. An async callback is rejected at
|
|
616
|
+
* compile time ({@link NotThenable}).
|
|
617
|
+
*/
|
|
618
|
+
recoverErr<U>(f: (error: E) => U & NotThenable<U>): AsyncResult$1<T | U, never>;
|
|
619
|
+
/**
|
|
620
|
+
* @deprecated Renamed to {@link AsyncResultMethods.recoverErr | recoverErr}.
|
|
621
|
+
* This alias will be removed in a future major.
|
|
508
622
|
*/
|
|
509
623
|
recover<U>(f: (error: E) => U & NotThenable<U>): AsyncResult$1<T | U, never>;
|
|
510
624
|
/**
|
|
511
625
|
* Asynchronous {@link ResultMethods.tapErr | tapErr}. `f` is synchronous; if it
|
|
512
626
|
* throws, the result is a `Defect` whose cause is an `AggregateError` of
|
|
513
627
|
* `[thrown, original failure]` — observing a failure never destroys it. An
|
|
514
|
-
* async callback is rejected at compile time ({@link NotThenable}).
|
|
628
|
+
* async callback is rejected at compile time ({@link NotThenable}). The
|
|
629
|
+
* {@link AsyncResultMethods.tap | tap} fire-and-forget caveat applies here
|
|
630
|
+
* too — a failable effect belongs in
|
|
631
|
+
* {@link AsyncResultMethods.flatTapErr | flatTapErr}.
|
|
515
632
|
*/
|
|
516
633
|
tapErr<R>(f: (error: E) => R & NotThenable<R>): AsyncResult$1<T, E>;
|
|
517
634
|
/**
|
|
@@ -545,21 +662,47 @@ type AsyncResultMethods<T, E> = {
|
|
|
545
662
|
defect: (cause: unknown) => R;
|
|
546
663
|
}): Promise<R>;
|
|
547
664
|
/**
|
|
548
|
-
* Asynchronous {@link ResultMethods.
|
|
665
|
+
* Asynchronous {@link ResultMethods.get | get}. Compiles only when the
|
|
549
666
|
* error channel is empty (`this: AsyncResult<T, never>`); the returned promise
|
|
550
667
|
* rejects on a `Defect` (rethrowing its cause).
|
|
551
668
|
*/
|
|
669
|
+
get(this: AsyncResult$1<T, never>): Promise<T>;
|
|
670
|
+
/**
|
|
671
|
+
* @deprecated Renamed to {@link AsyncResultMethods.get | get}. This alias will
|
|
672
|
+
* be removed in a future major.
|
|
673
|
+
*/
|
|
552
674
|
unwrap(this: AsyncResult$1<T, never>): Promise<T>;
|
|
553
675
|
/**
|
|
554
|
-
* Asynchronous {@link ResultMethods.
|
|
676
|
+
* Asynchronous {@link ResultMethods.getErr | getErr}. Compiles only when
|
|
555
677
|
* the success channel is empty (`this: AsyncResult<never, E>`); the returned
|
|
556
678
|
* promise rejects on a `Defect` (rethrowing its cause).
|
|
557
679
|
*/
|
|
558
|
-
|
|
559
|
-
|
|
680
|
+
getErr(this: AsyncResult$1<never, E>): Promise<E>;
|
|
681
|
+
/**
|
|
682
|
+
* @deprecated Renamed to {@link AsyncResultMethods.getErr | getErr}. This alias
|
|
683
|
+
* will be removed in a future major.
|
|
684
|
+
*/
|
|
685
|
+
unwrapErr(this: AsyncResult$1<never, E>): Promise<E>; /** Asynchronous {@link ResultMethods.getOr | getOr}. */
|
|
686
|
+
getOr<U>(fallback: U): Promise<T | U>;
|
|
687
|
+
/**
|
|
688
|
+
* @deprecated Renamed to {@link AsyncResultMethods.getOr | getOr}. This alias
|
|
689
|
+
* will be removed in a future major.
|
|
690
|
+
*/
|
|
691
|
+
unwrapOr<U>(fallback: U): Promise<T | U>; /** Asynchronous {@link ResultMethods.getOrElse | getOrElse}. */
|
|
692
|
+
getOrElse<U>(f: (error: E) => U): Promise<T | U>;
|
|
693
|
+
/**
|
|
694
|
+
* @deprecated Renamed to {@link AsyncResultMethods.getOrElse | getOrElse}. This
|
|
695
|
+
* alias will be removed in a future major.
|
|
696
|
+
*/
|
|
560
697
|
unwrapOrElse<U>(f: (error: E) => U): Promise<T | U>; /** Asynchronous {@link ResultMethods.getOrNull | getOrNull}. */
|
|
561
698
|
getOrNull(): Promise<T | null>; /** Asynchronous {@link ResultMethods.getOrUndefined | getOrUndefined}. */
|
|
562
699
|
getOrUndefined(): Promise<T | undefined>;
|
|
700
|
+
/**
|
|
701
|
+
* Asynchronous {@link ResultMethods.getOrThrow | getOrThrow} — the returned
|
|
702
|
+
* promise **rejects** with the modeled error on `Err` (or the original cause
|
|
703
|
+
* on a `Defect`), rather than throwing synchronously.
|
|
704
|
+
*/
|
|
705
|
+
getOrThrow(): Promise<T>;
|
|
563
706
|
};
|
|
564
707
|
/**
|
|
565
708
|
* The asynchronous counterpart of {@link Result}: an awaitable wrapper carrying
|
|
@@ -572,8 +715,8 @@ type AsyncResultMethods<T, E> = {
|
|
|
572
715
|
* rejection would silently become a `Defect`, skipping the triage that
|
|
573
716
|
* {@link fromPromise} forces. To do further async work, re-enter through a
|
|
574
717
|
* qualified boundary and compose it: `ar.flatMap((v) => fromPromise(work(v),
|
|
575
|
-
* qualify))`. The eliminators (`
|
|
576
|
-
* (`flatMap`, `flatTap`, `
|
|
718
|
+
* qualify))`. The eliminators (`get`, …) return promises; the binds
|
|
719
|
+
* (`flatMap`, `flatTap`, `flatMapErr`, `recoverDefect`) additionally accept an
|
|
577
720
|
* `AsyncResult`. Its combinators are documented one per entry on
|
|
578
721
|
* {@link AsyncResultMethods}.
|
|
579
722
|
*
|
|
@@ -660,7 +803,7 @@ type AsyncErrOf<R> = R extends AsyncResult$1<unknown, infer E> ? E : never;
|
|
|
660
803
|
* import { Ok } from "unthrown";
|
|
661
804
|
*
|
|
662
805
|
* Ok(2).map((n) => n + 1); // => Ok(3)
|
|
663
|
-
* Ok(42).
|
|
806
|
+
* Ok(42).get(); // => 42
|
|
664
807
|
* ```
|
|
665
808
|
*
|
|
666
809
|
* @category Constructors
|
|
@@ -677,12 +820,60 @@ declare function Ok<T>(value: T): Result$1<T, never>;
|
|
|
677
820
|
* import { Err } from "unthrown";
|
|
678
821
|
*
|
|
679
822
|
* Err("not_found").map((n) => n + 1); // => Err("not_found") (map skipped)
|
|
680
|
-
* Err("not_found").
|
|
823
|
+
* Err("not_found").getErr(); // => "not_found"
|
|
681
824
|
* ```
|
|
682
825
|
*
|
|
683
826
|
* @category Constructors
|
|
684
827
|
*/
|
|
685
828
|
declare function Err<E>(error: E): Result$1<never, E>;
|
|
829
|
+
/**
|
|
830
|
+
* Construct a successful {@link AsyncResult} from a pure value — the pre-lifted
|
|
831
|
+
* form of {@link Ok}, sparing you `Ok(value).toAsync()`.
|
|
832
|
+
*
|
|
833
|
+
* @remarks
|
|
834
|
+
* Reach for this on the synchronous/early branch of an `AsyncResult`-returning
|
|
835
|
+
* function, so both branches share one return type without a trailing
|
|
836
|
+
* `.toAsync()`. Named with the `Async` suffix the async free functions carry
|
|
837
|
+
* (`allAsync`, `allFromDictAsync`); the {@link AsyncResult} companion aliases it
|
|
838
|
+
* as `AsyncResult.Ok` (the namespace already says "async", so the suffix drops).
|
|
839
|
+
*
|
|
840
|
+
* @typeParam T - the success value type.
|
|
841
|
+
* @param value - the success value to wrap.
|
|
842
|
+
*
|
|
843
|
+
* @example
|
|
844
|
+
* ```ts
|
|
845
|
+
* import { OkAsync, type AsyncResult } from "unthrown";
|
|
846
|
+
*
|
|
847
|
+
* function loadItems(ids: string[]): AsyncResult<Item[], never> {
|
|
848
|
+
* if (ids.length === 0) return OkAsync([]); // no more Ok([]).toAsync()
|
|
849
|
+
* return itemRepository.load(ids);
|
|
850
|
+
* }
|
|
851
|
+
* ```
|
|
852
|
+
*
|
|
853
|
+
* @category Constructors
|
|
854
|
+
*/
|
|
855
|
+
declare function OkAsync<T>(value: T): AsyncResult$1<T, never>;
|
|
856
|
+
/**
|
|
857
|
+
* Construct a failed {@link AsyncResult} carrying a **modeled** error — the
|
|
858
|
+
* pre-lifted form of {@link Err}, sparing you `Err(error).toAsync()`.
|
|
859
|
+
*
|
|
860
|
+
* @remarks
|
|
861
|
+
* The error-channel mirror of {@link OkAsync}; see it for the naming and the
|
|
862
|
+
* `AsyncResult.Err` companion alias.
|
|
863
|
+
*
|
|
864
|
+
* @typeParam E - the modeled error type.
|
|
865
|
+
* @param error - the domain error to wrap.
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* ```ts
|
|
869
|
+
* import { ErrAsync } from "unthrown";
|
|
870
|
+
*
|
|
871
|
+
* ErrAsync("not_found"); // AsyncResult<never, string>
|
|
872
|
+
* ```
|
|
873
|
+
*
|
|
874
|
+
* @category Constructors
|
|
875
|
+
*/
|
|
876
|
+
declare function ErrAsync<E>(error: E): AsyncResult$1<never, E>;
|
|
686
877
|
/**
|
|
687
878
|
* Type guard: narrow a {@link Result} to its `Ok` variant, exposing `.value`.
|
|
688
879
|
*
|
|
@@ -750,8 +941,8 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
750
941
|
//#endregion
|
|
751
942
|
//#region src/core.d.ts
|
|
752
943
|
/**
|
|
753
|
-
* Thrown by a {@link Result}'s `
|
|
754
|
-
* wrong on a *modeled* result — `
|
|
944
|
+
* Thrown by a {@link Result}'s `get` / `getErr` when the assertion is
|
|
945
|
+
* wrong on a *modeled* result — `get()` on an `Err`, or `getErr()` on an
|
|
755
946
|
* `Ok`.
|
|
756
947
|
*
|
|
757
948
|
* @remarks
|
|
@@ -763,7 +954,7 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
763
954
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
764
955
|
* re-thrown (with its original stack) instead.
|
|
765
956
|
*
|
|
766
|
-
* `
|
|
957
|
+
* `get()` and `getErr()` are type-gated (`this: Result<T, never>` /
|
|
767
958
|
* `Result<never, E>`), so the wrong-variant branch that throws this is
|
|
768
959
|
* unreachable through well-typed code — it remains only as a defensive guard
|
|
769
960
|
* against unsound runtime misuse (e.g. an `as` cast past the gate).
|
|
@@ -774,8 +965,8 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
774
965
|
*/
|
|
775
966
|
declare class UnwrapError<E = unknown> extends Error {
|
|
776
967
|
/**
|
|
777
|
-
* The offending value: the `Err` error for `
|
|
778
|
-
* `
|
|
968
|
+
* The offending value: the `Err` error for `get()`, or the `Ok` value for
|
|
969
|
+
* `getErr()`.
|
|
779
970
|
*/
|
|
780
971
|
readonly error: E;
|
|
781
972
|
constructor(error: E);
|
|
@@ -893,9 +1084,9 @@ type Defect = {
|
|
|
893
1084
|
* import { fromNullable } from "unthrown";
|
|
894
1085
|
*
|
|
895
1086
|
* const map = new Map([["a", 1]]);
|
|
896
|
-
* fromNullable(map.get("a"), () => "absent").
|
|
1087
|
+
* fromNullable(map.get("a"), () => "absent").getOr(0); // => 1
|
|
897
1088
|
* fromNullable(map.get("z"), () => "absent"); // => Err("absent")
|
|
898
|
-
* fromNullable(0, () => "absent").
|
|
1089
|
+
* fromNullable(0, () => "absent").getOr(-1); // => 0 (falsy but present)
|
|
899
1090
|
* ```
|
|
900
1091
|
*/
|
|
901
1092
|
declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E): Result$1<NonNullable<T>, E>;
|
|
@@ -913,7 +1104,7 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
913
1104
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
914
1105
|
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
915
1106
|
* out-of-band and must not pollute the error channel); reach for
|
|
916
|
-
* {@link
|
|
1107
|
+
* {@link fromSafeThrowable} when every throw is a Defect.
|
|
917
1108
|
*
|
|
918
1109
|
* @typeParam A - the wrapped function's argument tuple.
|
|
919
1110
|
* @typeParam T - the wrapped function's return type.
|
|
@@ -937,11 +1128,41 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
937
1128
|
* cause instanceof SyntaxError ? ("invalid_json" as const) : defect(cause),
|
|
938
1129
|
* );
|
|
939
1130
|
*
|
|
940
|
-
* parse('{"ok":true}').
|
|
1131
|
+
* parse('{"ok":true}').getOr(null); // => { ok: true }
|
|
941
1132
|
* parse("nope"); // => Err("invalid_json")
|
|
942
1133
|
* ```
|
|
943
1134
|
*/
|
|
944
1135
|
declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T, qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): (...args: A) => Result$1<T, Exclude<R, Defect>>;
|
|
1136
|
+
/**
|
|
1137
|
+
* Wrap a throwing synchronous function asserted **not** to fail in any modeled
|
|
1138
|
+
* way: any throw becomes a `Defect`.
|
|
1139
|
+
*
|
|
1140
|
+
* @remarks
|
|
1141
|
+
* The synchronous counterpart of {@link fromSafePromise}. Use it only when a
|
|
1142
|
+
* throw genuinely indicates a bug rather than an anticipated outcome — the
|
|
1143
|
+
* error channel is `never`, so there is nothing to triage; there is no
|
|
1144
|
+
* `qualify`. When some throws *are* anticipated, reach for
|
|
1145
|
+
* {@link fromThrowable} and triage them.
|
|
1146
|
+
*
|
|
1147
|
+
* @typeParam A - the wrapped function's argument tuple.
|
|
1148
|
+
* @typeParam T - the wrapped function's return type.
|
|
1149
|
+
* @param fn - the throwing function to wrap.
|
|
1150
|
+
* @returns a function with the same arguments returning `Result<T, never>`.
|
|
1151
|
+
*
|
|
1152
|
+
* @category Interop
|
|
1153
|
+
*
|
|
1154
|
+
* @example
|
|
1155
|
+
* ```ts
|
|
1156
|
+
* import { fromSafeThrowable } from "unthrown";
|
|
1157
|
+
*
|
|
1158
|
+
* // A decode failure here is a bug (the row came from our own schema), so
|
|
1159
|
+
* // every throw is a defect — no throwaway `(cause, defect) => defect(cause)`.
|
|
1160
|
+
* const decode = fromSafeThrowable((row: Row) => userSchema.parse(row));
|
|
1161
|
+
*
|
|
1162
|
+
* decode(row); // => Result<User, never> — a throw becomes a Defect
|
|
1163
|
+
* ```
|
|
1164
|
+
*/
|
|
1165
|
+
declare function fromSafeThrowable<A extends unknown[], T>(fn: (...args: A) => T): (...args: A) => Result$1<T, never>;
|
|
945
1166
|
/**
|
|
946
1167
|
* Wrap a `Promise` (or a thunk producing one) as an {@link AsyncResult}, forcing
|
|
947
1168
|
* every rejection to be triaged.
|
|
@@ -987,7 +1208,8 @@ declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qua
|
|
|
987
1208
|
* @remarks
|
|
988
1209
|
* Use this only when a rejection genuinely indicates a bug rather than an
|
|
989
1210
|
* anticipated outcome — the error channel is `never`, so there is nothing to
|
|
990
|
-
* triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
1211
|
+
* triage. (`await`-ing still yields a `Result`; it never throws.) The
|
|
1212
|
+
* synchronous counterpart is {@link fromSafeThrowable}.
|
|
991
1213
|
*
|
|
992
1214
|
* @typeParam T - the resolved value type.
|
|
993
1215
|
* @param promise - the promise, or a thunk returning one.
|
|
@@ -998,7 +1220,7 @@ declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qua
|
|
|
998
1220
|
* ```ts
|
|
999
1221
|
* import { fromSafePromise } from "unthrown";
|
|
1000
1222
|
*
|
|
1001
|
-
* (await fromSafePromise(Promise.resolve(3))).
|
|
1223
|
+
* (await fromSafePromise(Promise.resolve(3))).get(); // => 3
|
|
1002
1224
|
* // a rejection becomes a Defect (never a modeled Err):
|
|
1003
1225
|
* await fromSafePromise(Promise.reject(new Error("boom"))); // => Defect(Error("boom"))
|
|
1004
1226
|
* ```
|
|
@@ -1044,7 +1266,7 @@ type AsyncResultRecord = Record<string, AsyncResult$1<unknown, unknown>>;
|
|
|
1044
1266
|
* ```ts
|
|
1045
1267
|
* import { all, Ok, Err } from "unthrown";
|
|
1046
1268
|
*
|
|
1047
|
-
* all([Ok(1), Ok("a"), Ok(true)]).
|
|
1269
|
+
* all([Ok(1), Ok("a"), Ok(true)]).get(); // => [1, "a", true] (typed [number, string, boolean])
|
|
1048
1270
|
* all([Ok(1), Err("e"), Ok(3)]); // => Err("e") (short-circuits on the first Err)
|
|
1049
1271
|
* ```
|
|
1050
1272
|
*/
|
|
@@ -1065,7 +1287,7 @@ declare function all<Rs extends readonly Result$1<unknown, unknown>[]>(results:
|
|
|
1065
1287
|
* ```ts
|
|
1066
1288
|
* import { allFromDict, Ok, Err } from "unthrown";
|
|
1067
1289
|
*
|
|
1068
|
-
* allFromDict({ id: Ok(1), name: Ok("ada") }).
|
|
1290
|
+
* allFromDict({ id: Ok(1), name: Ok("ada") }).get(); // => { id: 1, name: "ada" }
|
|
1069
1291
|
* allFromDict({ id: Ok(1), name: Err("missing") }); // => Err("missing")
|
|
1070
1292
|
* ```
|
|
1071
1293
|
*/
|
|
@@ -1087,7 +1309,7 @@ declare function allFromDict<R extends ResultRecord>(results: R): Result$1<{ [K
|
|
|
1087
1309
|
* import { allAsync, fromSafePromise } from "unthrown";
|
|
1088
1310
|
*
|
|
1089
1311
|
* const both = allAsync([fromSafePromise(Promise.resolve(1)), fromSafePromise(Promise.resolve(2))]);
|
|
1090
|
-
* (await both).
|
|
1312
|
+
* (await both).get(); // => [1, 2]
|
|
1091
1313
|
* ```
|
|
1092
1314
|
*/
|
|
1093
1315
|
declare function allAsync<Rs extends readonly AsyncResult$1<unknown, unknown>[]>(results: readonly [...Rs]): AsyncResult$1<AllOk<Rs, { [K in keyof Rs]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
@@ -1109,7 +1331,7 @@ declare function allAsync<Rs extends readonly AsyncResult$1<unknown, unknown>[]>
|
|
|
1109
1331
|
* a: fromSafePromise(Promise.resolve(1)),
|
|
1110
1332
|
* b: fromSafePromise(Promise.resolve("x")),
|
|
1111
1333
|
* });
|
|
1112
|
-
* (await both).
|
|
1334
|
+
* (await both).get(); // => { a: 1, b: "x" }
|
|
1113
1335
|
* ```
|
|
1114
1336
|
*/
|
|
1115
1337
|
declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): AsyncResult$1<{ [K in keyof R]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
@@ -1119,8 +1341,9 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
1119
1341
|
* Companion object grouping the **`Result`-producing** entry points under a
|
|
1120
1342
|
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
1121
1343
|
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
1122
|
-
* {@link Result.
|
|
1123
|
-
* {@link Result.
|
|
1344
|
+
* {@link Result.fromSafeThrowable}, {@link Result.all},
|
|
1345
|
+
* {@link Result.allFromDict}, {@link Result.isOk}, {@link Result.isErr},
|
|
1346
|
+
* {@link Result.isDefect}, {@link Result.isResult}.
|
|
1124
1347
|
*
|
|
1125
1348
|
* @remarks
|
|
1126
1349
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -1137,7 +1360,7 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
1137
1360
|
* @example
|
|
1138
1361
|
* ```ts
|
|
1139
1362
|
* import { Result } from "unthrown";
|
|
1140
|
-
* Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).
|
|
1363
|
+
* Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).get(); // => 2
|
|
1141
1364
|
* ```
|
|
1142
1365
|
*/
|
|
1143
1366
|
declare const Result: {
|
|
@@ -1146,6 +1369,7 @@ declare const Result: {
|
|
|
1146
1369
|
readonly Do: typeof Do;
|
|
1147
1370
|
readonly fromNullable: typeof fromNullable;
|
|
1148
1371
|
readonly fromThrowable: typeof fromThrowable;
|
|
1372
|
+
readonly fromSafeThrowable: typeof fromSafeThrowable;
|
|
1149
1373
|
readonly all: typeof all;
|
|
1150
1374
|
readonly allFromDict: typeof allFromDict;
|
|
1151
1375
|
readonly isOk: typeof isOk;
|
|
@@ -1160,7 +1384,7 @@ declare const Result: {
|
|
|
1160
1384
|
*
|
|
1161
1385
|
* @remarks
|
|
1162
1386
|
* A `Result` is a discriminated union, so TypeDoc can't list its methods on this
|
|
1163
|
-
* alias. Its fluent combinators (`map`, `flatMap`, `match`, `
|
|
1387
|
+
* alias. Its fluent combinators (`map`, `flatMap`, `match`, `get`, …) are
|
|
1164
1388
|
* documented one per entry on {@link ResultMethods} — the shared method surface
|
|
1165
1389
|
* every variant carries. For "which one do I reach for?", see the
|
|
1166
1390
|
* [Choosing a combinator](/guide/choosing-a-combinator) guide.
|
|
@@ -1170,18 +1394,20 @@ declare const Result: {
|
|
|
1170
1394
|
type Result<T, E> = Result$1<T, E>;
|
|
1171
1395
|
/**
|
|
1172
1396
|
* Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
1173
|
-
* the matching namespace: {@link AsyncResult.
|
|
1174
|
-
* {@link AsyncResult.
|
|
1175
|
-
* {@link AsyncResult.allFromDict}.
|
|
1397
|
+
* the matching namespace: {@link AsyncResult.Ok}, {@link AsyncResult.Err},
|
|
1398
|
+
* {@link AsyncResult.fromPromise}, {@link AsyncResult.fromSafePromise},
|
|
1399
|
+
* {@link AsyncResult.all}, {@link AsyncResult.allFromDict}.
|
|
1176
1400
|
*
|
|
1177
1401
|
* @remarks
|
|
1178
1402
|
* The async sibling of {@link Result}. Statics are grouped by what they
|
|
1179
|
-
* **return**, so `fromPromise`/`fromSafePromise
|
|
1180
|
-
* here rather than on {@link Result}; the namespace
|
|
1181
|
-
* the
|
|
1182
|
-
*
|
|
1183
|
-
*
|
|
1184
|
-
*
|
|
1403
|
+
* **return**, so the pre-lifted constructors, `fromPromise`/`fromSafePromise`,
|
|
1404
|
+
* and the async aggregates sit here rather than on {@link Result}; the namespace
|
|
1405
|
+
* already conveys "async", so the members drop the `Async` suffix their free
|
|
1406
|
+
* functions carry (`AsyncResult.Ok` is `OkAsync`; `AsyncResult.Err` is
|
|
1407
|
+
* `ErrAsync`; `AsyncResult.all` is `allAsync`; `AsyncResult.allFromDict` is
|
|
1408
|
+
* `allFromDictAsync`). Like {@link Result}, the free functions remain the
|
|
1409
|
+
* primary, tree-shakeable API; the value `AsyncResult` and the type
|
|
1410
|
+
* {@link AsyncResult} share one name.
|
|
1185
1411
|
*
|
|
1186
1412
|
* @category Facade
|
|
1187
1413
|
*
|
|
@@ -1189,10 +1415,12 @@ type Result<T, E> = Result$1<T, E>;
|
|
|
1189
1415
|
* ```ts
|
|
1190
1416
|
* import { AsyncResult } from "unthrown";
|
|
1191
1417
|
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
1192
|
-
* user.
|
|
1418
|
+
* user.get(); // => the fetched user (on success)
|
|
1193
1419
|
* ```
|
|
1194
1420
|
*/
|
|
1195
1421
|
declare const AsyncResult: {
|
|
1422
|
+
readonly Ok: typeof OkAsync;
|
|
1423
|
+
readonly Err: typeof ErrAsync;
|
|
1196
1424
|
readonly fromPromise: typeof fromPromise;
|
|
1197
1425
|
readonly fromSafePromise: typeof fromSafePromise;
|
|
1198
1426
|
readonly all: typeof allAsync;
|
|
@@ -1205,7 +1433,7 @@ declare const AsyncResult: {
|
|
|
1205
1433
|
*
|
|
1206
1434
|
* @remarks
|
|
1207
1435
|
* `AsyncResult` carries the async fluent surface; its combinators (`map`,
|
|
1208
|
-
* `flatMap`, `match`, `
|
|
1436
|
+
* `flatMap`, `match`, `get`, …) are documented one per entry — with their
|
|
1209
1437
|
* async signatures — on {@link AsyncResultMethods}. For "which one do I reach
|
|
1210
1438
|
* for?", see the [Choosing a combinator](/guide/choosing-a-combinator) guide.
|
|
1211
1439
|
*
|
|
@@ -1381,5 +1609,5 @@ declare function matchTags<T, E extends {
|
|
|
1381
1609
|
_tag: string;
|
|
1382
1610
|
}, R>(result: AsyncResult$1<T, E>, handlers: TagHandlers<T, E, R> & ([Extract<E["_tag"], "Ok" | "Defect">] extends [never] ? unknown : ReservedTagError)): Promise<R>;
|
|
1383
1611
|
//#endregion
|
|
1384
|
-
export { type AsyncErrOf, type AsyncOkOf, AsyncResult, type AsyncResultMethods, type Awaitable, type DefectView, Do, Err, type ErrOf, type ErrView, type NotThenable, Ok, type OkOf, type OkView, Result, type ResultMethods, type TagHandlers, TaggedError, type TaggedErrorConstructor, type TaggedErrorInstance, UnwrapError, all, allAsync, allFromDict, allFromDictAsync, fromNullable, fromPromise, fromSafePromise, fromThrowable, isDefect, isErr, isOk, isResult, matchTags };
|
|
1612
|
+
export { type AsyncErrOf, type AsyncOkOf, AsyncResult, type AsyncResultMethods, type Awaitable, type DefectView, Do, Err, ErrAsync, type ErrOf, type ErrView, type NotThenable, Ok, OkAsync, type OkOf, type OkView, Result, type ResultMethods, type TagHandlers, TaggedError, type TaggedErrorConstructor, type TaggedErrorInstance, UnwrapError, all, allAsync, allFromDict, allFromDictAsync, fromNullable, fromPromise, fromSafePromise, fromSafeThrowable, fromThrowable, isDefect, isErr, isOk, isResult, matchTags };
|
|
1385
1613
|
//# sourceMappingURL=index.d.cts.map
|