unthrown 3.1.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 +221 -51
- package/dist/index.d.cts +336 -71
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +336 -71
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +219 -52
- 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>;
|
|
@@ -254,20 +295,52 @@ type ResultMethods<T, E> = {
|
|
|
254
295
|
/**
|
|
255
296
|
* Extract the success value.
|
|
256
297
|
*
|
|
298
|
+
* @remarks
|
|
299
|
+
* Compiles only when the error channel is empty (`E = never`) — eliminate
|
|
300
|
+
* modeled errors first (`match` / `recoverErr` / `flatMapErr`), or reach for the
|
|
301
|
+
* `getOr` / `getOrElse` / `getOrNull` / `getOrUndefined` family (which
|
|
302
|
+
* recover an `Err`). If you get a `'this' context` type error here, that is
|
|
303
|
+
* the gate: the receiver still has a non-`never` error channel.
|
|
304
|
+
*
|
|
305
|
+
* `E = never` empties only the **modeled** error channel — a `Defect` can
|
|
306
|
+
* still be present, and `get()` **rethrows its original cause** (it
|
|
307
|
+
* _panics_); `Result<T, never>` does not mean `get()` cannot throw.
|
|
308
|
+
*
|
|
257
309
|
* @returns the `Ok` value.
|
|
258
|
-
* @throws On `Err`, an {@link UnwrapError} carrying the error. On a `Defect`,
|
|
259
|
-
* re-throws the **original cause** with its original stack, so an unhandled
|
|
260
|
-
* Defect surfaces at the global handler as the real failure.
|
|
261
310
|
*/
|
|
262
|
-
|
|
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.
|
|
317
|
+
*
|
|
318
|
+
* @returns the `Ok` value.
|
|
319
|
+
*/
|
|
320
|
+
unwrap(this: Result$1<T, never>): T;
|
|
263
321
|
/**
|
|
264
322
|
* Extract the modeled error.
|
|
265
323
|
*
|
|
324
|
+
* @remarks
|
|
325
|
+
* Compiles only when the success channel is empty (`T = never`) — eliminate
|
|
326
|
+
* the success case first. `T = never` is rarely the case in practice (a
|
|
327
|
+
* `Result` you hold usually still has a success type), so to inspect an
|
|
328
|
+
* error prefer an `isErr()` guard or, in tests, `@unthrown/vitest`'s
|
|
329
|
+
* `toBeErrWith`. A `Defect` still **rethrows its original cause** (a defect is
|
|
330
|
+
* a bug, not an absent value), so this does not mean `getErr()` can't throw.
|
|
331
|
+
*
|
|
266
332
|
* @returns the `Err` value.
|
|
267
|
-
* @throws On `Ok`, an {@link UnwrapError} carrying the value. On a `Defect`,
|
|
268
|
-
* re-throws the original cause.
|
|
269
333
|
*/
|
|
270
|
-
|
|
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.
|
|
340
|
+
*
|
|
341
|
+
* @returns the `Err` value.
|
|
342
|
+
*/
|
|
343
|
+
unwrapErr(this: Result$1<never, E>): E;
|
|
271
344
|
/**
|
|
272
345
|
* The success value, or `fallback` on `Err`.
|
|
273
346
|
*
|
|
@@ -276,6 +349,17 @@ type ResultMethods<T, E> = {
|
|
|
276
349
|
* @throws Re-throws on a `Defect` — a Defect is a bug, not an absent value, so
|
|
277
350
|
* it is never silently replaced.
|
|
278
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
|
+
*/
|
|
279
363
|
unwrapOr<U>(fallback: U): T | U;
|
|
280
364
|
/**
|
|
281
365
|
* The success value, or `f(error)` on `Err`.
|
|
@@ -284,6 +368,17 @@ type ResultMethods<T, E> = {
|
|
|
284
368
|
* @param f - lazily computes the fallback from the error (may return a different type; the return widens to `T | U`).
|
|
285
369
|
* @throws Re-throws on a `Defect`.
|
|
286
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
|
+
*/
|
|
287
382
|
unwrapOrElse<U>(f: (error: E) => U): T | U;
|
|
288
383
|
/**
|
|
289
384
|
* The success value, or `null` on `Err`.
|
|
@@ -296,7 +391,25 @@ type ResultMethods<T, E> = {
|
|
|
296
391
|
*
|
|
297
392
|
* @throws Re-throws on a `Defect`.
|
|
298
393
|
*/
|
|
299
|
-
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`. */
|
|
300
413
|
isOk(): this is OkView<T, E>; /** Whether this result is `Err` — narrows `this` to its {@link ErrView} on `true`. */
|
|
301
414
|
isErr(): this is ErrView<E, T>; /** Whether this result is a `Defect` — narrows `this` to its {@link DefectView} on `true`. */
|
|
302
415
|
isDefect(): this is DefectView<T, E>; /** Lift this synchronous `Result` into an {@link AsyncResult}. */
|
|
@@ -419,7 +532,7 @@ type Awaitable<T> = {
|
|
|
419
532
|
};
|
|
420
533
|
/**
|
|
421
534
|
* The async method surface every {@link AsyncResult} carries — the combinators
|
|
422
|
-
* (`map`, `flatMap`, `mapErr`, `match`, `
|
|
535
|
+
* (`map`, `flatMap`, `mapErr`, `match`, `get`, …) with their asynchronous
|
|
423
536
|
* signatures, documented one per entry below. The async mirror of
|
|
424
537
|
* {@link ResultMethods}: each entry links its synchronous counterpart and states
|
|
425
538
|
* only the async delta.
|
|
@@ -453,7 +566,12 @@ type AsyncResultMethods<T, E> = {
|
|
|
453
566
|
/**
|
|
454
567
|
* Asynchronous {@link ResultMethods.tap | tap}. `f` is synchronous; a throw
|
|
455
568
|
* becomes a `Defect`. An async callback is rejected at compile time
|
|
456
|
-
* ({@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}.
|
|
457
575
|
*/
|
|
458
576
|
tap<R>(f: (value: T) => R & NotThenable<R>): AsyncResult$1<T, E>;
|
|
459
577
|
/**
|
|
@@ -483,21 +601,34 @@ type AsyncResultMethods<T, E> = {
|
|
|
483
601
|
*/
|
|
484
602
|
mapErr<E2>(f: (error: E) => E2 & NotThenable<E2>): AsyncResult$1<T, E2>;
|
|
485
603
|
/**
|
|
486
|
-
* Asynchronous {@link ResultMethods.
|
|
487
|
-
* 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.
|
|
488
611
|
*/
|
|
489
612
|
orElse<U, E2>(f: (error: E) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<T | U, E2>;
|
|
490
613
|
/**
|
|
491
|
-
* Asynchronous {@link ResultMethods.
|
|
492
|
-
* throw becomes a `Defect`. An async callback is rejected at
|
|
493
|
-
* ({@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.
|
|
494
622
|
*/
|
|
495
623
|
recover<U>(f: (error: E) => U & NotThenable<U>): AsyncResult$1<T | U, never>;
|
|
496
624
|
/**
|
|
497
625
|
* Asynchronous {@link ResultMethods.tapErr | tapErr}. `f` is synchronous; if it
|
|
498
626
|
* throws, the result is a `Defect` whose cause is an `AggregateError` of
|
|
499
627
|
* `[thrown, original failure]` — observing a failure never destroys it. An
|
|
500
|
-
* 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}.
|
|
501
632
|
*/
|
|
502
633
|
tapErr<R>(f: (error: E) => R & NotThenable<R>): AsyncResult$1<T, E>;
|
|
503
634
|
/**
|
|
@@ -531,15 +662,47 @@ type AsyncResultMethods<T, E> = {
|
|
|
531
662
|
defect: (cause: unknown) => R;
|
|
532
663
|
}): Promise<R>;
|
|
533
664
|
/**
|
|
534
|
-
* Asynchronous {@link ResultMethods.
|
|
535
|
-
*
|
|
665
|
+
* Asynchronous {@link ResultMethods.get | get}. Compiles only when the
|
|
666
|
+
* error channel is empty (`this: AsyncResult<T, never>`); the returned promise
|
|
667
|
+
* rejects on a `Defect` (rethrowing its cause).
|
|
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
|
+
*/
|
|
674
|
+
unwrap(this: AsyncResult$1<T, never>): Promise<T>;
|
|
675
|
+
/**
|
|
676
|
+
* Asynchronous {@link ResultMethods.getErr | getErr}. Compiles only when
|
|
677
|
+
* the success channel is empty (`this: AsyncResult<never, E>`); the returned
|
|
678
|
+
* promise rejects on a `Defect` (rethrowing its cause).
|
|
679
|
+
*/
|
|
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.
|
|
536
696
|
*/
|
|
537
|
-
unwrap(): Promise<T>; /** Asynchronous {@link ResultMethods.unwrapErr | unwrapErr}. */
|
|
538
|
-
unwrapErr(): Promise<E>; /** Asynchronous {@link ResultMethods.unwrapOr | unwrapOr}. */
|
|
539
|
-
unwrapOr<U>(fallback: U): Promise<T | U>; /** Asynchronous {@link ResultMethods.unwrapOrElse | unwrapOrElse}. */
|
|
540
697
|
unwrapOrElse<U>(f: (error: E) => U): Promise<T | U>; /** Asynchronous {@link ResultMethods.getOrNull | getOrNull}. */
|
|
541
698
|
getOrNull(): Promise<T | null>; /** Asynchronous {@link ResultMethods.getOrUndefined | getOrUndefined}. */
|
|
542
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>;
|
|
543
706
|
};
|
|
544
707
|
/**
|
|
545
708
|
* The asynchronous counterpart of {@link Result}: an awaitable wrapper carrying
|
|
@@ -552,8 +715,8 @@ type AsyncResultMethods<T, E> = {
|
|
|
552
715
|
* rejection would silently become a `Defect`, skipping the triage that
|
|
553
716
|
* {@link fromPromise} forces. To do further async work, re-enter through a
|
|
554
717
|
* qualified boundary and compose it: `ar.flatMap((v) => fromPromise(work(v),
|
|
555
|
-
* qualify))`. The eliminators (`
|
|
556
|
-
* (`flatMap`, `flatTap`, `
|
|
718
|
+
* qualify))`. The eliminators (`get`, …) return promises; the binds
|
|
719
|
+
* (`flatMap`, `flatTap`, `flatMapErr`, `recoverDefect`) additionally accept an
|
|
557
720
|
* `AsyncResult`. Its combinators are documented one per entry on
|
|
558
721
|
* {@link AsyncResultMethods}.
|
|
559
722
|
*
|
|
@@ -640,7 +803,7 @@ type AsyncErrOf<R> = R extends AsyncResult$1<unknown, infer E> ? E : never;
|
|
|
640
803
|
* import { Ok } from "unthrown";
|
|
641
804
|
*
|
|
642
805
|
* Ok(2).map((n) => n + 1); // => Ok(3)
|
|
643
|
-
* Ok(42).
|
|
806
|
+
* Ok(42).get(); // => 42
|
|
644
807
|
* ```
|
|
645
808
|
*
|
|
646
809
|
* @category Constructors
|
|
@@ -657,12 +820,60 @@ declare function Ok<T>(value: T): Result$1<T, never>;
|
|
|
657
820
|
* import { Err } from "unthrown";
|
|
658
821
|
*
|
|
659
822
|
* Err("not_found").map((n) => n + 1); // => Err("not_found") (map skipped)
|
|
660
|
-
* Err("not_found").
|
|
823
|
+
* Err("not_found").getErr(); // => "not_found"
|
|
661
824
|
* ```
|
|
662
825
|
*
|
|
663
826
|
* @category Constructors
|
|
664
827
|
*/
|
|
665
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>;
|
|
666
877
|
/**
|
|
667
878
|
* Type guard: narrow a {@link Result} to its `Ok` variant, exposing `.value`.
|
|
668
879
|
*
|
|
@@ -730,8 +941,8 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
730
941
|
//#endregion
|
|
731
942
|
//#region src/core.d.ts
|
|
732
943
|
/**
|
|
733
|
-
* Thrown by a {@link Result}'s `
|
|
734
|
-
* 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
|
|
735
946
|
* `Ok`.
|
|
736
947
|
*
|
|
737
948
|
* @remarks
|
|
@@ -743,14 +954,19 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
743
954
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
744
955
|
* re-thrown (with its original stack) instead.
|
|
745
956
|
*
|
|
957
|
+
* `get()` and `getErr()` are type-gated (`this: Result<T, never>` /
|
|
958
|
+
* `Result<never, E>`), so the wrong-variant branch that throws this is
|
|
959
|
+
* unreachable through well-typed code — it remains only as a defensive guard
|
|
960
|
+
* against unsound runtime misuse (e.g. an `as` cast past the gate).
|
|
961
|
+
*
|
|
746
962
|
* @typeParam E - the type of the {@link UnwrapError.error} it carries.
|
|
747
963
|
*
|
|
748
964
|
* @category Errors
|
|
749
965
|
*/
|
|
750
966
|
declare class UnwrapError<E = unknown> extends Error {
|
|
751
967
|
/**
|
|
752
|
-
* The offending value: the `Err` error for `
|
|
753
|
-
* `
|
|
968
|
+
* The offending value: the `Err` error for `get()`, or the `Ok` value for
|
|
969
|
+
* `getErr()`.
|
|
754
970
|
*/
|
|
755
971
|
readonly error: E;
|
|
756
972
|
constructor(error: E);
|
|
@@ -868,9 +1084,9 @@ type Defect = {
|
|
|
868
1084
|
* import { fromNullable } from "unthrown";
|
|
869
1085
|
*
|
|
870
1086
|
* const map = new Map([["a", 1]]);
|
|
871
|
-
* fromNullable(map.get("a"), () => "absent").
|
|
1087
|
+
* fromNullable(map.get("a"), () => "absent").getOr(0); // => 1
|
|
872
1088
|
* fromNullable(map.get("z"), () => "absent"); // => Err("absent")
|
|
873
|
-
* fromNullable(0, () => "absent").
|
|
1089
|
+
* fromNullable(0, () => "absent").getOr(-1); // => 0 (falsy but present)
|
|
874
1090
|
* ```
|
|
875
1091
|
*/
|
|
876
1092
|
declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () => E): Result$1<NonNullable<T>, E>;
|
|
@@ -888,7 +1104,7 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
888
1104
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
889
1105
|
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
890
1106
|
* out-of-band and must not pollute the error channel); reach for
|
|
891
|
-
* {@link
|
|
1107
|
+
* {@link fromSafeThrowable} when every throw is a Defect.
|
|
892
1108
|
*
|
|
893
1109
|
* @typeParam A - the wrapped function's argument tuple.
|
|
894
1110
|
* @typeParam T - the wrapped function's return type.
|
|
@@ -912,11 +1128,41 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
912
1128
|
* cause instanceof SyntaxError ? ("invalid_json" as const) : defect(cause),
|
|
913
1129
|
* );
|
|
914
1130
|
*
|
|
915
|
-
* parse('{"ok":true}').
|
|
1131
|
+
* parse('{"ok":true}').getOr(null); // => { ok: true }
|
|
916
1132
|
* parse("nope"); // => Err("invalid_json")
|
|
917
1133
|
* ```
|
|
918
1134
|
*/
|
|
919
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>;
|
|
920
1166
|
/**
|
|
921
1167
|
* Wrap a `Promise` (or a thunk producing one) as an {@link AsyncResult}, forcing
|
|
922
1168
|
* every rejection to be triaged.
|
|
@@ -950,8 +1196,8 @@ declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T,
|
|
|
950
1196
|
* cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
951
1197
|
* );
|
|
952
1198
|
*
|
|
953
|
-
* user.
|
|
954
|
-
* // when fetchUser rejects with NotFoundError:
|
|
1199
|
+
* if (user.isOk()) user.value; // => the fetched user
|
|
1200
|
+
* // when fetchUser rejects with NotFoundError: user is Err("not_found")
|
|
955
1201
|
* ```
|
|
956
1202
|
*/
|
|
957
1203
|
declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): AsyncResult$1<T, Exclude<R, Defect>>;
|
|
@@ -962,7 +1208,8 @@ declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qua
|
|
|
962
1208
|
* @remarks
|
|
963
1209
|
* Use this only when a rejection genuinely indicates a bug rather than an
|
|
964
1210
|
* anticipated outcome — the error channel is `never`, so there is nothing to
|
|
965
|
-
* 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}.
|
|
966
1213
|
*
|
|
967
1214
|
* @typeParam T - the resolved value type.
|
|
968
1215
|
* @param promise - the promise, or a thunk returning one.
|
|
@@ -973,7 +1220,7 @@ declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qua
|
|
|
973
1220
|
* ```ts
|
|
974
1221
|
* import { fromSafePromise } from "unthrown";
|
|
975
1222
|
*
|
|
976
|
-
* (await fromSafePromise(Promise.resolve(3))).
|
|
1223
|
+
* (await fromSafePromise(Promise.resolve(3))).get(); // => 3
|
|
977
1224
|
* // a rejection becomes a Defect (never a modeled Err):
|
|
978
1225
|
* await fromSafePromise(Promise.reject(new Error("boom"))); // => Defect(Error("boom"))
|
|
979
1226
|
* ```
|
|
@@ -1019,7 +1266,7 @@ type AsyncResultRecord = Record<string, AsyncResult$1<unknown, unknown>>;
|
|
|
1019
1266
|
* ```ts
|
|
1020
1267
|
* import { all, Ok, Err } from "unthrown";
|
|
1021
1268
|
*
|
|
1022
|
-
* all([Ok(1), Ok("a"), Ok(true)]).
|
|
1269
|
+
* all([Ok(1), Ok("a"), Ok(true)]).get(); // => [1, "a", true] (typed [number, string, boolean])
|
|
1023
1270
|
* all([Ok(1), Err("e"), Ok(3)]); // => Err("e") (short-circuits on the first Err)
|
|
1024
1271
|
* ```
|
|
1025
1272
|
*/
|
|
@@ -1040,7 +1287,7 @@ declare function all<Rs extends readonly Result$1<unknown, unknown>[]>(results:
|
|
|
1040
1287
|
* ```ts
|
|
1041
1288
|
* import { allFromDict, Ok, Err } from "unthrown";
|
|
1042
1289
|
*
|
|
1043
|
-
* allFromDict({ id: Ok(1), name: Ok("ada") }).
|
|
1290
|
+
* allFromDict({ id: Ok(1), name: Ok("ada") }).get(); // => { id: 1, name: "ada" }
|
|
1044
1291
|
* allFromDict({ id: Ok(1), name: Err("missing") }); // => Err("missing")
|
|
1045
1292
|
* ```
|
|
1046
1293
|
*/
|
|
@@ -1062,7 +1309,7 @@ declare function allFromDict<R extends ResultRecord>(results: R): Result$1<{ [K
|
|
|
1062
1309
|
* import { allAsync, fromSafePromise } from "unthrown";
|
|
1063
1310
|
*
|
|
1064
1311
|
* const both = allAsync([fromSafePromise(Promise.resolve(1)), fromSafePromise(Promise.resolve(2))]);
|
|
1065
|
-
* (await both).
|
|
1312
|
+
* (await both).get(); // => [1, 2]
|
|
1066
1313
|
* ```
|
|
1067
1314
|
*/
|
|
1068
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]>>;
|
|
@@ -1084,7 +1331,7 @@ declare function allAsync<Rs extends readonly AsyncResult$1<unknown, unknown>[]>
|
|
|
1084
1331
|
* a: fromSafePromise(Promise.resolve(1)),
|
|
1085
1332
|
* b: fromSafePromise(Promise.resolve("x")),
|
|
1086
1333
|
* });
|
|
1087
|
-
* (await both).
|
|
1334
|
+
* (await both).get(); // => { a: 1, b: "x" }
|
|
1088
1335
|
* ```
|
|
1089
1336
|
*/
|
|
1090
1337
|
declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): AsyncResult$1<{ [K in keyof R]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
@@ -1094,8 +1341,9 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
1094
1341
|
* Companion object grouping the **`Result`-producing** entry points under a
|
|
1095
1342
|
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
1096
1343
|
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
1097
|
-
* {@link Result.
|
|
1098
|
-
* {@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}.
|
|
1099
1347
|
*
|
|
1100
1348
|
* @remarks
|
|
1101
1349
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -1112,7 +1360,7 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
1112
1360
|
* @example
|
|
1113
1361
|
* ```ts
|
|
1114
1362
|
* import { Result } from "unthrown";
|
|
1115
|
-
* Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).
|
|
1363
|
+
* Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).get(); // => 2
|
|
1116
1364
|
* ```
|
|
1117
1365
|
*/
|
|
1118
1366
|
declare const Result: {
|
|
@@ -1121,6 +1369,7 @@ declare const Result: {
|
|
|
1121
1369
|
readonly Do: typeof Do;
|
|
1122
1370
|
readonly fromNullable: typeof fromNullable;
|
|
1123
1371
|
readonly fromThrowable: typeof fromThrowable;
|
|
1372
|
+
readonly fromSafeThrowable: typeof fromSafeThrowable;
|
|
1124
1373
|
readonly all: typeof all;
|
|
1125
1374
|
readonly allFromDict: typeof allFromDict;
|
|
1126
1375
|
readonly isOk: typeof isOk;
|
|
@@ -1135,7 +1384,7 @@ declare const Result: {
|
|
|
1135
1384
|
*
|
|
1136
1385
|
* @remarks
|
|
1137
1386
|
* A `Result` is a discriminated union, so TypeDoc can't list its methods on this
|
|
1138
|
-
* alias. Its fluent combinators (`map`, `flatMap`, `match`, `
|
|
1387
|
+
* alias. Its fluent combinators (`map`, `flatMap`, `match`, `get`, …) are
|
|
1139
1388
|
* documented one per entry on {@link ResultMethods} — the shared method surface
|
|
1140
1389
|
* every variant carries. For "which one do I reach for?", see the
|
|
1141
1390
|
* [Choosing a combinator](/guide/choosing-a-combinator) guide.
|
|
@@ -1145,18 +1394,20 @@ declare const Result: {
|
|
|
1145
1394
|
type Result<T, E> = Result$1<T, E>;
|
|
1146
1395
|
/**
|
|
1147
1396
|
* Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
1148
|
-
* the matching namespace: {@link AsyncResult.
|
|
1149
|
-
* {@link AsyncResult.
|
|
1150
|
-
* {@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}.
|
|
1151
1400
|
*
|
|
1152
1401
|
* @remarks
|
|
1153
1402
|
* The async sibling of {@link Result}. Statics are grouped by what they
|
|
1154
|
-
* **return**, so `fromPromise`/`fromSafePromise
|
|
1155
|
-
* here rather than on {@link Result}; the namespace
|
|
1156
|
-
* the
|
|
1157
|
-
*
|
|
1158
|
-
*
|
|
1159
|
-
*
|
|
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.
|
|
1160
1411
|
*
|
|
1161
1412
|
* @category Facade
|
|
1162
1413
|
*
|
|
@@ -1164,10 +1415,12 @@ type Result<T, E> = Result$1<T, E>;
|
|
|
1164
1415
|
* ```ts
|
|
1165
1416
|
* import { AsyncResult } from "unthrown";
|
|
1166
1417
|
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
1167
|
-
* user.
|
|
1418
|
+
* user.get(); // => the fetched user (on success)
|
|
1168
1419
|
* ```
|
|
1169
1420
|
*/
|
|
1170
1421
|
declare const AsyncResult: {
|
|
1422
|
+
readonly Ok: typeof OkAsync;
|
|
1423
|
+
readonly Err: typeof ErrAsync;
|
|
1171
1424
|
readonly fromPromise: typeof fromPromise;
|
|
1172
1425
|
readonly fromSafePromise: typeof fromSafePromise;
|
|
1173
1426
|
readonly all: typeof allAsync;
|
|
@@ -1180,7 +1433,7 @@ declare const AsyncResult: {
|
|
|
1180
1433
|
*
|
|
1181
1434
|
* @remarks
|
|
1182
1435
|
* `AsyncResult` carries the async fluent surface; its combinators (`map`,
|
|
1183
|
-
* `flatMap`, `match`, `
|
|
1436
|
+
* `flatMap`, `match`, `get`, …) are documented one per entry — with their
|
|
1184
1437
|
* async signatures — on {@link AsyncResultMethods}. For "which one do I reach
|
|
1185
1438
|
* for?", see the [Choosing a combinator](/guide/choosing-a-combinator) guide.
|
|
1186
1439
|
*
|
|
@@ -1199,7 +1452,7 @@ type Props = Record<string, unknown>;
|
|
|
1199
1452
|
*
|
|
1200
1453
|
* @category Types
|
|
1201
1454
|
*/
|
|
1202
|
-
type TaggedErrorInstance<Tag extends string, A extends Props> = Error & Readonly<Omit<A, "name">> & {
|
|
1455
|
+
type TaggedErrorInstance<Tag extends string, A extends Props> = Error & Readonly<Omit<A, "name" | "message">> & {
|
|
1203
1456
|
readonly _tag: Tag;
|
|
1204
1457
|
};
|
|
1205
1458
|
/**
|
|
@@ -1208,10 +1461,13 @@ type TaggedErrorInstance<Tag extends string, A extends Props> = Error & Readonly
|
|
|
1208
1461
|
*
|
|
1209
1462
|
* @remarks
|
|
1210
1463
|
* When the payload is empty, the constructor takes **no** arguments (the
|
|
1211
|
-
* `keyof A extends never ? void : A` trick); otherwise it takes the payload.
|
|
1212
|
-
* `name`
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1464
|
+
* `keyof A extends never ? void : A` trick); otherwise it takes the payload. The
|
|
1465
|
+
* `name` and `message` keys are both **rejected** (`name?: never` /
|
|
1466
|
+
* `message?: never`) because both are reserved: `name` is the display label, and
|
|
1467
|
+
* `message` is the human string owned by `Error`. Set the message the standard
|
|
1468
|
+
* way — `override message = "…"` (or a constructor override) on the subclass —
|
|
1469
|
+
* never as a free-form per-call payload field. The reservations are enforced at
|
|
1470
|
+
* the call site, mirroring how {@link TaggedErrorInstance} excludes both.
|
|
1215
1471
|
*
|
|
1216
1472
|
* @typeParam Tag - the string literal discriminant.
|
|
1217
1473
|
*
|
|
@@ -1220,6 +1476,7 @@ type TaggedErrorInstance<Tag extends string, A extends Props> = Error & Readonly
|
|
|
1220
1476
|
type TaggedErrorConstructor<Tag extends string> = {
|
|
1221
1477
|
new <A extends Props = {}>(args: keyof A extends never ? void : A & {
|
|
1222
1478
|
readonly name?: never;
|
|
1479
|
+
readonly message?: never;
|
|
1223
1480
|
}): TaggedErrorInstance<Tag, A>;
|
|
1224
1481
|
};
|
|
1225
1482
|
/**
|
|
@@ -1228,8 +1485,13 @@ type TaggedErrorConstructor<Tag extends string> = {
|
|
|
1228
1485
|
*
|
|
1229
1486
|
* @remarks
|
|
1230
1487
|
* Extend the returned class to declare a concrete error. Supply the payload with
|
|
1231
|
-
* an instantiation expression; omit it for a payload-less error.
|
|
1232
|
-
*
|
|
1488
|
+
* an instantiation expression; omit it for a payload-less error. The `message`
|
|
1489
|
+
* is **not** a payload field — it is the human string owned by `Error`, not
|
|
1490
|
+
* structured data, so it is reserved. Define it once per subclass the standard
|
|
1491
|
+
* way, `override message = "…"` (it may interpolate the payload via `this`,
|
|
1492
|
+
* which the base populates before the subclass field initialiser runs); a
|
|
1493
|
+
* payload `message` is rejected at compile time, so contextual detail lives in
|
|
1494
|
+
* typed fields, never baked into per-call prose. The `_tag` always reflects
|
|
1233
1495
|
* `tag` and cannot be overridden by the payload. `name` is likewise reserved —
|
|
1234
1496
|
* it is the display label (set it with `options.name`); a payload `name` is
|
|
1235
1497
|
* rejected at compile time (and excluded from the instance type), so it can't
|
|
@@ -1244,11 +1506,14 @@ type TaggedErrorConstructor<Tag extends string> = {
|
|
|
1244
1506
|
* ```ts
|
|
1245
1507
|
* class RetryableError extends TaggedError("@my-lib/RetryableError", {
|
|
1246
1508
|
* name: "RetryableError",
|
|
1247
|
-
* })
|
|
1509
|
+
* }) {
|
|
1510
|
+
* override message = "operation failed; safe to retry";
|
|
1511
|
+
* }
|
|
1248
1512
|
*
|
|
1249
|
-
* const e = new RetryableError(
|
|
1250
|
-
* e._tag;
|
|
1251
|
-
* e.name;
|
|
1513
|
+
* const e = new RetryableError();
|
|
1514
|
+
* e._tag; // "@my-lib/RetryableError" — namespaced discriminant
|
|
1515
|
+
* e.name; // "RetryableError" — clean display name
|
|
1516
|
+
* e.message; // "operation failed; safe to retry" — the standard Error.message
|
|
1252
1517
|
* ```
|
|
1253
1518
|
*
|
|
1254
1519
|
* @typeParam Tag - the string literal discriminant.
|
|
@@ -1344,5 +1609,5 @@ declare function matchTags<T, E extends {
|
|
|
1344
1609
|
_tag: string;
|
|
1345
1610
|
}, R>(result: AsyncResult$1<T, E>, handlers: TagHandlers<T, E, R> & ([Extract<E["_tag"], "Ok" | "Defect">] extends [never] ? unknown : ReservedTagError)): Promise<R>;
|
|
1346
1611
|
//#endregion
|
|
1347
|
-
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 };
|
|
1348
1613
|
//# sourceMappingURL=index.d.cts.map
|