unthrown 3.0.0 → 3.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/README.md +5 -2
- package/dist/index.cjs +199 -43
- package/dist/index.d.cts +506 -107
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +506 -107
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +199 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
- package/docs/index.md +0 -1407
package/docs/index.md
DELETED
|
@@ -1,1407 +0,0 @@
|
|
|
1
|
-
**unthrown**
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
# unthrown
|
|
6
|
-
|
|
7
|
-
## Classes
|
|
8
|
-
|
|
9
|
-
### UnwrapError
|
|
10
|
-
|
|
11
|
-
Defined in: [packages/core/src/core.ts:35](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L35)
|
|
12
|
-
|
|
13
|
-
Thrown by a [Result](#result)'s `unwrap` / `unwrapErr` when the assertion is
|
|
14
|
-
wrong on a *modeled* result — `unwrap()` on an `Err`, or `unwrapErr()` on an
|
|
15
|
-
`Ok`.
|
|
16
|
-
|
|
17
|
-
#### Remarks
|
|
18
|
-
|
|
19
|
-
The offending value is exposed two ways: the typed [UnwrapError.error](#error)
|
|
20
|
-
property for programmatic access, and the standard `Error.cause` for the
|
|
21
|
-
runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
22
|
-
its original stack is printed under "caused by".
|
|
23
|
-
|
|
24
|
-
A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
25
|
-
re-thrown (with its original stack) instead.
|
|
26
|
-
|
|
27
|
-
#### Extends
|
|
28
|
-
|
|
29
|
-
- `Error`
|
|
30
|
-
|
|
31
|
-
#### Type Parameters
|
|
32
|
-
|
|
33
|
-
| Type Parameter | Default type | Description |
|
|
34
|
-
| ------ | ------ | ------ |
|
|
35
|
-
| `E` | `unknown` | the type of the [UnwrapError.error](#error) it carries. |
|
|
36
|
-
|
|
37
|
-
#### Constructors
|
|
38
|
-
|
|
39
|
-
##### Constructor
|
|
40
|
-
|
|
41
|
-
```ts
|
|
42
|
-
new UnwrapError<E>(error): UnwrapError<E>;
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Defined in: [packages/core/src/core.ts:41](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L41)
|
|
46
|
-
|
|
47
|
-
###### Parameters
|
|
48
|
-
|
|
49
|
-
| Parameter | Type |
|
|
50
|
-
| ------ | ------ |
|
|
51
|
-
| `error` | `E` |
|
|
52
|
-
|
|
53
|
-
###### Returns
|
|
54
|
-
|
|
55
|
-
[`UnwrapError`](#unwraperror)<`E`>
|
|
56
|
-
|
|
57
|
-
###### Overrides
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
Error.constructor
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
#### Properties
|
|
64
|
-
|
|
65
|
-
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
66
|
-
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
67
|
-
| <a id="cause"></a> `cause?` | `public` | `unknown` | - | `Error.cause` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24 |
|
|
68
|
-
| <a id="error"></a> `error` | `readonly` | `E` | The offending value: the `Err` error for `unwrap()`, or the `Ok` value for `unwrapErr()`. | - | [packages/core/src/core.ts:40](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L40) |
|
|
69
|
-
| <a id="message"></a> `message` | `public` | `string` | - | `Error.message` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075 |
|
|
70
|
-
| <a id="name"></a> `name` | `public` | `string` | - | `Error.name` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074 |
|
|
71
|
-
| <a id="stack"></a> `stack?` | `public` | `string` | - | `Error.stack` | node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076 |
|
|
72
|
-
| <a id="stacktracelimit"></a> `stackTraceLimit` | `static` | `number` | The `Error.stackTraceLimit` property specifies the number of stack frames collected by a stack trace (whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). The default value is `10` but may be set to any valid JavaScript number. Changes will affect any stack trace captured _after_ the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames. | `Error.stackTraceLimit` | node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:68 |
|
|
73
|
-
|
|
74
|
-
#### Methods
|
|
75
|
-
|
|
76
|
-
##### captureStackTrace()
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
static captureStackTrace(targetObject, constructorOpt?): void;
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Defined in: node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:52
|
|
83
|
-
|
|
84
|
-
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
85
|
-
a string representing the location in the code at which
|
|
86
|
-
`Error.captureStackTrace()` was called.
|
|
87
|
-
|
|
88
|
-
```js
|
|
89
|
-
const myObject = {};
|
|
90
|
-
Error.captureStackTrace(myObject);
|
|
91
|
-
myObject.stack; // Similar to `new Error().stack`
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
The first line of the trace will be prefixed with
|
|
95
|
-
`${myObject.name}: ${myObject.message}`.
|
|
96
|
-
|
|
97
|
-
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
98
|
-
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
99
|
-
generated stack trace.
|
|
100
|
-
|
|
101
|
-
The `constructorOpt` argument is useful for hiding implementation
|
|
102
|
-
details of error generation from the user. For instance:
|
|
103
|
-
|
|
104
|
-
```js
|
|
105
|
-
function a() {
|
|
106
|
-
b();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function b() {
|
|
110
|
-
c();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function c() {
|
|
114
|
-
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
115
|
-
const { stackTraceLimit } = Error;
|
|
116
|
-
Error.stackTraceLimit = 0;
|
|
117
|
-
const error = new Error();
|
|
118
|
-
Error.stackTraceLimit = stackTraceLimit;
|
|
119
|
-
|
|
120
|
-
// Capture the stack trace above function b
|
|
121
|
-
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
a();
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
###### Parameters
|
|
129
|
-
|
|
130
|
-
| Parameter | Type |
|
|
131
|
-
| ------ | ------ |
|
|
132
|
-
| `targetObject` | `object` |
|
|
133
|
-
| `constructorOpt?` | `Function` |
|
|
134
|
-
|
|
135
|
-
###### Returns
|
|
136
|
-
|
|
137
|
-
`void`
|
|
138
|
-
|
|
139
|
-
###### Inherited from
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
Error.captureStackTrace
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
##### prepareStackTrace()
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
static prepareStackTrace(err, stackTraces): any;
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
Defined in: node\_modules/.pnpm/@types+node@24.13.2/node\_modules/@types/node/globals.d.ts:56
|
|
152
|
-
|
|
153
|
-
###### Parameters
|
|
154
|
-
|
|
155
|
-
| Parameter | Type |
|
|
156
|
-
| ------ | ------ |
|
|
157
|
-
| `err` | `Error` |
|
|
158
|
-
| `stackTraces` | `CallSite`[] |
|
|
159
|
-
|
|
160
|
-
###### Returns
|
|
161
|
-
|
|
162
|
-
`any`
|
|
163
|
-
|
|
164
|
-
###### See
|
|
165
|
-
|
|
166
|
-
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
167
|
-
|
|
168
|
-
###### Inherited from
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
Error.prepareStackTrace
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
## Type Aliases
|
|
175
|
-
|
|
176
|
-
### AsyncErrOf
|
|
177
|
-
|
|
178
|
-
```ts
|
|
179
|
-
type AsyncErrOf<R> = R extends AsyncResult<unknown, infer E> ? E : never;
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
Defined in: [packages/core/src/types.ts:468](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L468)
|
|
183
|
-
|
|
184
|
-
Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
185
|
-
|
|
186
|
-
#### Type Parameters
|
|
187
|
-
|
|
188
|
-
| Type Parameter | Description |
|
|
189
|
-
| ------ | ------ |
|
|
190
|
-
| `R` | the `AsyncResult` type to inspect. |
|
|
191
|
-
|
|
192
|
-
***
|
|
193
|
-
|
|
194
|
-
### AsyncOkOf
|
|
195
|
-
|
|
196
|
-
```ts
|
|
197
|
-
type AsyncOkOf<R> = R extends AsyncResult<infer T, unknown> ? T : never;
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
Defined in: [packages/core/src/types.ts:462](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L462)
|
|
201
|
-
|
|
202
|
-
Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
203
|
-
|
|
204
|
-
#### Type Parameters
|
|
205
|
-
|
|
206
|
-
| Type Parameter | Description |
|
|
207
|
-
| ------ | ------ |
|
|
208
|
-
| `R` | the `AsyncResult` type to inspect. |
|
|
209
|
-
|
|
210
|
-
***
|
|
211
|
-
|
|
212
|
-
### AsyncResult
|
|
213
|
-
|
|
214
|
-
```ts
|
|
215
|
-
type AsyncResult<T, E> = AsyncResultType<T, E>;
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
Defined in: [packages/core/src/facade.ts:85](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L85)
|
|
219
|
-
|
|
220
|
-
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
221
|
-
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
222
|
-
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
223
|
-
[AsyncResult.allFromDict](#property-allfromdict).
|
|
224
|
-
|
|
225
|
-
#### Type Parameters
|
|
226
|
-
|
|
227
|
-
| Type Parameter |
|
|
228
|
-
| ------ |
|
|
229
|
-
| `T` |
|
|
230
|
-
| `E` |
|
|
231
|
-
|
|
232
|
-
#### Remarks
|
|
233
|
-
|
|
234
|
-
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
235
|
-
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
236
|
-
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
237
|
-
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
238
|
-
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
239
|
-
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
240
|
-
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
241
|
-
|
|
242
|
-
#### Example
|
|
243
|
-
|
|
244
|
-
```ts
|
|
245
|
-
import { AsyncResult } from "unthrown";
|
|
246
|
-
const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
***
|
|
250
|
-
|
|
251
|
-
### Awaitable
|
|
252
|
-
|
|
253
|
-
```ts
|
|
254
|
-
type Awaitable<T> = object;
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
Defined in: [packages/core/src/types.ts:346](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L346)
|
|
258
|
-
|
|
259
|
-
A success-only thenable: awaitable, but deliberately **not** a full
|
|
260
|
-
`PromiseLike`.
|
|
261
|
-
|
|
262
|
-
#### Remarks
|
|
263
|
-
|
|
264
|
-
An [AsyncResult](#asyncresult)'s internal promise never rejects, so `await`-ing one
|
|
265
|
-
always yields a [Result](#result) and never throws — there is no rejection
|
|
266
|
-
channel to model, and none is advertised. At runtime it is still a thenable
|
|
267
|
-
(the only way `await` can collapse it); the narrowing simply keeps it from
|
|
268
|
-
being treated as a raw promise (e.g. dropped into `Promise.all`).
|
|
269
|
-
|
|
270
|
-
#### Type Parameters
|
|
271
|
-
|
|
272
|
-
| Type Parameter | Description |
|
|
273
|
-
| ------ | ------ |
|
|
274
|
-
| `T` | the value `await` resolves to. |
|
|
275
|
-
|
|
276
|
-
#### Methods
|
|
277
|
-
|
|
278
|
-
##### then()
|
|
279
|
-
|
|
280
|
-
```ts
|
|
281
|
-
then<R>(onfulfilled?): PromiseLike<R>;
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
Defined in: [packages/core/src/types.ts:347](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L347)
|
|
285
|
-
|
|
286
|
-
###### Type Parameters
|
|
287
|
-
|
|
288
|
-
| Type Parameter | Default type |
|
|
289
|
-
| ------ | ------ |
|
|
290
|
-
| `R` | `T` |
|
|
291
|
-
|
|
292
|
-
###### Parameters
|
|
293
|
-
|
|
294
|
-
| Parameter | Type |
|
|
295
|
-
| ------ | ------ |
|
|
296
|
-
| `onfulfilled?` | ((`value`) => `R` \| `PromiseLike`<`R`>) \| `null` |
|
|
297
|
-
|
|
298
|
-
###### Returns
|
|
299
|
-
|
|
300
|
-
`PromiseLike`<`R`>
|
|
301
|
-
|
|
302
|
-
***
|
|
303
|
-
|
|
304
|
-
### DefectView
|
|
305
|
-
|
|
306
|
-
```ts
|
|
307
|
-
type DefectView<T, E> = ResultMethods<T, E> & object;
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
Defined in: [packages/core/src/types.ts:289](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L289)
|
|
311
|
-
|
|
312
|
-
The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `cause`.
|
|
313
|
-
|
|
314
|
-
#### Type Declaration
|
|
315
|
-
|
|
316
|
-
| Name | Type | Defined in |
|
|
317
|
-
| ------ | ------ | ------ |
|
|
318
|
-
| `cause` | `unknown` | [packages/core/src/types.ts:291](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L291) |
|
|
319
|
-
| `tag` | `"Defect"` | [packages/core/src/types.ts:290](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L290) |
|
|
320
|
-
|
|
321
|
-
#### Type Parameters
|
|
322
|
-
|
|
323
|
-
| Type Parameter | Default type |
|
|
324
|
-
| ------ | ------ |
|
|
325
|
-
| `T` | `never` |
|
|
326
|
-
| `E` | `never` |
|
|
327
|
-
|
|
328
|
-
***
|
|
329
|
-
|
|
330
|
-
### ErrOf
|
|
331
|
-
|
|
332
|
-
```ts
|
|
333
|
-
type ErrOf<R> = R extends object ? E : never;
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
Defined in: [packages/core/src/types.ts:456](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L456)
|
|
337
|
-
|
|
338
|
-
Extract the error type `E` from a `Result`.
|
|
339
|
-
|
|
340
|
-
#### Type Parameters
|
|
341
|
-
|
|
342
|
-
| Type Parameter | Description |
|
|
343
|
-
| ------ | ------ |
|
|
344
|
-
| `R` | the `Result` type to inspect. |
|
|
345
|
-
|
|
346
|
-
***
|
|
347
|
-
|
|
348
|
-
### ErrView
|
|
349
|
-
|
|
350
|
-
```ts
|
|
351
|
-
type ErrView<E, T> = ResultMethods<T, E> & object;
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
Defined in: [packages/core/src/types.ts:284](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L284)
|
|
355
|
-
|
|
356
|
-
The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
357
|
-
|
|
358
|
-
#### Type Declaration
|
|
359
|
-
|
|
360
|
-
| Name | Type | Defined in |
|
|
361
|
-
| ------ | ------ | ------ |
|
|
362
|
-
| `error` | `E` | [packages/core/src/types.ts:286](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L286) |
|
|
363
|
-
| `tag` | `"Err"` | [packages/core/src/types.ts:285](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L285) |
|
|
364
|
-
|
|
365
|
-
#### Type Parameters
|
|
366
|
-
|
|
367
|
-
| Type Parameter | Default type |
|
|
368
|
-
| ------ | ------ |
|
|
369
|
-
| `E` | - |
|
|
370
|
-
| `T` | `never` |
|
|
371
|
-
|
|
372
|
-
***
|
|
373
|
-
|
|
374
|
-
### OkOf
|
|
375
|
-
|
|
376
|
-
```ts
|
|
377
|
-
type OkOf<R> = R extends object ? T : never;
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
Defined in: [packages/core/src/types.ts:450](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L450)
|
|
381
|
-
|
|
382
|
-
Extract the success type `T` from a `Result`.
|
|
383
|
-
|
|
384
|
-
#### Type Parameters
|
|
385
|
-
|
|
386
|
-
| Type Parameter | Description |
|
|
387
|
-
| ------ | ------ |
|
|
388
|
-
| `R` | the `Result` type to inspect. |
|
|
389
|
-
|
|
390
|
-
***
|
|
391
|
-
|
|
392
|
-
### OkView
|
|
393
|
-
|
|
394
|
-
```ts
|
|
395
|
-
type OkView<T, E> = ResultMethods<T, E> & object;
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
Defined in: [packages/core/src/types.ts:279](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L279)
|
|
399
|
-
|
|
400
|
-
The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
401
|
-
|
|
402
|
-
#### Type Declaration
|
|
403
|
-
|
|
404
|
-
| Name | Type | Defined in |
|
|
405
|
-
| ------ | ------ | ------ |
|
|
406
|
-
| `tag` | `"Ok"` | [packages/core/src/types.ts:280](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L280) |
|
|
407
|
-
| `value` | `T` | [packages/core/src/types.ts:281](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/types.ts#L281) |
|
|
408
|
-
|
|
409
|
-
#### Type Parameters
|
|
410
|
-
|
|
411
|
-
| Type Parameter | Default type |
|
|
412
|
-
| ------ | ------ |
|
|
413
|
-
| `T` | - |
|
|
414
|
-
| `E` | `never` |
|
|
415
|
-
|
|
416
|
-
***
|
|
417
|
-
|
|
418
|
-
### Result
|
|
419
|
-
|
|
420
|
-
```ts
|
|
421
|
-
type Result<T, E> = ResultType<T, E>;
|
|
422
|
-
```
|
|
423
|
-
|
|
424
|
-
Defined in: [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L45)
|
|
425
|
-
|
|
426
|
-
Companion object grouping the **`Result`-producing** entry points under a
|
|
427
|
-
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
428
|
-
[Result.Do](#property-do), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
429
|
-
[Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1), [Result.isOk](#property-isok),
|
|
430
|
-
[Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
431
|
-
|
|
432
|
-
#### Type Parameters
|
|
433
|
-
|
|
434
|
-
| Type Parameter |
|
|
435
|
-
| ------ |
|
|
436
|
-
| `T` |
|
|
437
|
-
| `E` |
|
|
438
|
-
|
|
439
|
-
#### Remarks
|
|
440
|
-
|
|
441
|
-
Purely additive sugar — each member **is** the corresponding free function.
|
|
442
|
-
The free functions remain the primary, tree-shakeable API; importing only
|
|
443
|
-
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
444
|
-
[Result](#result-1) share one name (the companion-object pattern).
|
|
445
|
-
|
|
446
|
-
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
447
|
-
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
448
|
-
return — a static lives in exactly one namespace.
|
|
449
|
-
|
|
450
|
-
#### Example
|
|
451
|
-
|
|
452
|
-
```ts
|
|
453
|
-
import { Result } from "unthrown";
|
|
454
|
-
Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
***
|
|
458
|
-
|
|
459
|
-
### TaggedErrorConstructor
|
|
460
|
-
|
|
461
|
-
```ts
|
|
462
|
-
type TaggedErrorConstructor<Tag> = <A>(args) => TaggedErrorInstance<Tag, A>;
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L28)
|
|
466
|
-
|
|
467
|
-
The class constructor returned by [TaggedError](#taggederror). Generic in its payload:
|
|
468
|
-
apply it with an instantiation expression at the `extends` site.
|
|
469
|
-
|
|
470
|
-
#### Type Parameters
|
|
471
|
-
|
|
472
|
-
| Type Parameter | Description |
|
|
473
|
-
| ------ | ------ |
|
|
474
|
-
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
475
|
-
|
|
476
|
-
#### Parameters
|
|
477
|
-
|
|
478
|
-
| Parameter | Type |
|
|
479
|
-
| ------ | ------ |
|
|
480
|
-
| `args` | keyof `A` *extends* `never` ? `void` : `A` |
|
|
481
|
-
|
|
482
|
-
#### Returns
|
|
483
|
-
|
|
484
|
-
[`TaggedErrorInstance`](#taggederrorinstance)<`Tag`, `A`>
|
|
485
|
-
|
|
486
|
-
#### Remarks
|
|
487
|
-
|
|
488
|
-
When the payload is empty, the constructor takes **no** arguments (the
|
|
489
|
-
`keyof A extends never ? void : A` trick); otherwise it takes the payload.
|
|
490
|
-
|
|
491
|
-
***
|
|
492
|
-
|
|
493
|
-
### TaggedErrorInstance
|
|
494
|
-
|
|
495
|
-
```ts
|
|
496
|
-
type TaggedErrorInstance<Tag, A> = Error & Readonly<A> & object;
|
|
497
|
-
```
|
|
498
|
-
|
|
499
|
-
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L15)
|
|
500
|
-
|
|
501
|
-
The instance shape produced by a [TaggedError](#taggederror) class: an `Error` plus a
|
|
502
|
-
`_tag` discriminant and the (readonly) payload fields.
|
|
503
|
-
|
|
504
|
-
#### Type Declaration
|
|
505
|
-
|
|
506
|
-
| Name | Type | Defined in |
|
|
507
|
-
| ------ | ------ | ------ |
|
|
508
|
-
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L16) |
|
|
509
|
-
|
|
510
|
-
#### Type Parameters
|
|
511
|
-
|
|
512
|
-
| Type Parameter | Description |
|
|
513
|
-
| ------ | ------ |
|
|
514
|
-
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
515
|
-
| `A` *extends* `Props` | the payload object type. |
|
|
516
|
-
|
|
517
|
-
***
|
|
518
|
-
|
|
519
|
-
### TagHandlers
|
|
520
|
-
|
|
521
|
-
```ts
|
|
522
|
-
type TagHandlers<T, E, R> = object & { [K in E["_tag"]]: (error: Extract<E, { _tag: K }>) => R };
|
|
523
|
-
```
|
|
524
|
-
|
|
525
|
-
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L103)
|
|
526
|
-
|
|
527
|
-
The handler object [matchTags](#matchtags) requires: a branch per error tag, plus
|
|
528
|
-
`Ok` and `Defect`. Miss a tag and it will not compile — the exhaustiveness is
|
|
529
|
-
enforced by the type, with no `.exhaustive()` to forget.
|
|
530
|
-
|
|
531
|
-
#### Type Declaration
|
|
532
|
-
|
|
533
|
-
| Name | Type | Defined in |
|
|
534
|
-
| ------ | ------ | ------ |
|
|
535
|
-
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L105) |
|
|
536
|
-
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L104) |
|
|
537
|
-
|
|
538
|
-
#### Type Parameters
|
|
539
|
-
|
|
540
|
-
| Type Parameter | Description |
|
|
541
|
-
| ------ | ------ |
|
|
542
|
-
| `T` | the success value type. |
|
|
543
|
-
| `E` *extends* `object` | the tagged error union. |
|
|
544
|
-
| `R` | the folded result type. |
|
|
545
|
-
|
|
546
|
-
## Variables
|
|
547
|
-
|
|
548
|
-
### AsyncResult
|
|
549
|
-
|
|
550
|
-
```ts
|
|
551
|
-
const AsyncResult: object;
|
|
552
|
-
```
|
|
553
|
-
|
|
554
|
-
Defined in: [packages/core/src/facade.ts:85](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L85)
|
|
555
|
-
|
|
556
|
-
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
557
|
-
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
558
|
-
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
559
|
-
[AsyncResult.allFromDict](#property-allfromdict).
|
|
560
|
-
|
|
561
|
-
#### Type Declaration
|
|
562
|
-
|
|
563
|
-
| Name | Type | Default value | Defined in |
|
|
564
|
-
| ------ | ------ | ------ | ------ |
|
|
565
|
-
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `AsyncResult`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>> | `allAsync` | [packages/core/src/facade.ts:88](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L88) |
|
|
566
|
-
| <a id="property-allfromdict"></a> `allFromDict()` | <`R`>(`results`) => `AsyncResult`<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>> | `allFromDictAsync` | [packages/core/src/facade.ts:89](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L89) |
|
|
567
|
-
| <a id="property-frompromise"></a> `fromPromise()` | <`T`, `R`>(`promise`, `qualify`) => `AsyncResult`<`T`, `Exclude`<`R`, `Defect`>> | - | [packages/core/src/facade.ts:86](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L86) |
|
|
568
|
-
| <a id="property-fromsafepromise"></a> `fromSafePromise()` | <`T`>(`promise`) => `AsyncResult`<`T`, `never`> | - | [packages/core/src/facade.ts:87](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L87) |
|
|
569
|
-
|
|
570
|
-
#### Remarks
|
|
571
|
-
|
|
572
|
-
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
573
|
-
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
574
|
-
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
575
|
-
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
576
|
-
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
577
|
-
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
578
|
-
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
579
|
-
|
|
580
|
-
#### Example
|
|
581
|
-
|
|
582
|
-
```ts
|
|
583
|
-
import { AsyncResult } from "unthrown";
|
|
584
|
-
const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
585
|
-
```
|
|
586
|
-
|
|
587
|
-
***
|
|
588
|
-
|
|
589
|
-
### Result
|
|
590
|
-
|
|
591
|
-
```ts
|
|
592
|
-
const Result: object;
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
Defined in: [packages/core/src/facade.ts:45](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L45)
|
|
596
|
-
|
|
597
|
-
Companion object grouping the **`Result`-producing** entry points under a
|
|
598
|
-
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
599
|
-
[Result.Do](#property-do), [Result.fromNullable](#property-fromnullable), [Result.fromThrowable](#property-fromthrowable),
|
|
600
|
-
[Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1), [Result.isOk](#property-isok),
|
|
601
|
-
[Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect), [Result.isResult](#property-isresult).
|
|
602
|
-
|
|
603
|
-
#### Type Declaration
|
|
604
|
-
|
|
605
|
-
| Name | Type | Defined in |
|
|
606
|
-
| ------ | ------ | ------ |
|
|
607
|
-
| <a id="property-all-1"></a> `all()` | <`Rs`>(`results`) => `Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:51](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L51) |
|
|
608
|
-
| <a id="property-allfromdict-1"></a> `allFromDict()` | <`R`>(`results`) => `Result`<\{ \[K in string \| number \| symbol\]: OkOf\<R\[K\]\> \}, [`ErrOf`](#errof)<`R`\[keyof `R`\]>> | [packages/core/src/facade.ts:52](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L52) |
|
|
609
|
-
| <a id="property-do"></a> `Do()` | () => `Result`<\{ \}, `never`> | [packages/core/src/facade.ts:48](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L48) |
|
|
610
|
-
| <a id="property-err"></a> `Err()` | <`E`>(`error`) => `Result`<`never`, `E`> | [packages/core/src/facade.ts:47](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L47) |
|
|
611
|
-
| <a id="property-fromnullable"></a> `fromNullable()` | <`T`, `E`>(`value`, `onAbsent`) => `Result`<`NonNullable`<`T`>, `E`> | [packages/core/src/facade.ts:49](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L49) |
|
|
612
|
-
| <a id="property-fromthrowable"></a> `fromThrowable()` | <`A`, `T`, `R`>(`fn`, `qualify`) => (...`args`) => `Result`<`T`, `Exclude`<`R`, `Defect`>> | [packages/core/src/facade.ts:50](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L50) |
|
|
613
|
-
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:55](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L55) |
|
|
614
|
-
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:54](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L54) |
|
|
615
|
-
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:53](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L53) |
|
|
616
|
-
| <a id="property-isresult"></a> `isResult()` | (`x`) => `x is Result<unknown, unknown>` | [packages/core/src/facade.ts:56](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L56) |
|
|
617
|
-
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:46](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/facade.ts#L46) |
|
|
618
|
-
|
|
619
|
-
#### Remarks
|
|
620
|
-
|
|
621
|
-
Purely additive sugar — each member **is** the corresponding free function.
|
|
622
|
-
The free functions remain the primary, tree-shakeable API; importing only
|
|
623
|
-
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
624
|
-
[Result](#result-1) share one name (the companion-object pattern).
|
|
625
|
-
|
|
626
|
-
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
627
|
-
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
628
|
-
return — a static lives in exactly one namespace.
|
|
629
|
-
|
|
630
|
-
#### Example
|
|
631
|
-
|
|
632
|
-
```ts
|
|
633
|
-
import { Result } from "unthrown";
|
|
634
|
-
Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
635
|
-
```
|
|
636
|
-
|
|
637
|
-
## Functions
|
|
638
|
-
|
|
639
|
-
### all()
|
|
640
|
-
|
|
641
|
-
```ts
|
|
642
|
-
function all<Rs>(results): Result<AllOk<Rs, { [K in string | number | symbol]: OkOf<Rs[K]> }>, ErrOf<Rs[number]>>;
|
|
643
|
-
```
|
|
644
|
-
|
|
645
|
-
Defined in: [packages/core/src/interop.ts:252](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L252)
|
|
646
|
-
|
|
647
|
-
Collect a tuple/array of [Result](#result)s into a single `Result` of all their
|
|
648
|
-
success values.
|
|
649
|
-
|
|
650
|
-
#### Type Parameters
|
|
651
|
-
|
|
652
|
-
| Type Parameter |
|
|
653
|
-
| ------ |
|
|
654
|
-
| `Rs` *extends* readonly `Result`<`unknown`, `unknown`>[] |
|
|
655
|
-
|
|
656
|
-
#### Parameters
|
|
657
|
-
|
|
658
|
-
| Parameter | Type |
|
|
659
|
-
| ------ | ------ |
|
|
660
|
-
| `results` | readonly \[`Rs`\] |
|
|
661
|
-
|
|
662
|
-
#### Returns
|
|
663
|
-
|
|
664
|
-
`Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>>
|
|
665
|
-
|
|
666
|
-
#### Remarks
|
|
667
|
-
|
|
668
|
-
Short-circuits on the **first** `Err` (later entries are not inspected for
|
|
669
|
-
their error); any `Defect` present **dominates**, winning even over an earlier
|
|
670
|
-
`Err`. A **fixed tuple** keeps its positional types — `all([Ok(1), Ok("a")])`
|
|
671
|
-
is `Result<[number, string], …>` — while a **dynamic array** `Result<T, E>[]`
|
|
672
|
-
collapses to `Result<T[], E>` with no cast. For a **record** keyed by name,
|
|
673
|
-
use [allFromDict](#allfromdict).
|
|
674
|
-
|
|
675
|
-
#### Example
|
|
676
|
-
|
|
677
|
-
```ts
|
|
678
|
-
import { all, Ok } from "unthrown";
|
|
679
|
-
all([Ok(1), Ok("a"), Ok(true)]).unwrap(); // [1, "a", true] (typed [number, string, boolean])
|
|
680
|
-
all([Ok(1), Ok(2)] as Result<number, never>[]).unwrap(); // number[]
|
|
681
|
-
```
|
|
682
|
-
|
|
683
|
-
***
|
|
684
|
-
|
|
685
|
-
### allAsync()
|
|
686
|
-
|
|
687
|
-
```ts
|
|
688
|
-
function allAsync<Rs>(results): AsyncResult<AllOk<Rs, { [K in string | number | symbol]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
689
|
-
```
|
|
690
|
-
|
|
691
|
-
Defined in: [packages/core/src/interop.ts:302](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L302)
|
|
692
|
-
|
|
693
|
-
The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
694
|
-
[AsyncResult](#asyncresult)s into one `AsyncResult` of all their success values.
|
|
695
|
-
|
|
696
|
-
#### Type Parameters
|
|
697
|
-
|
|
698
|
-
| Type Parameter |
|
|
699
|
-
| ------ |
|
|
700
|
-
| `Rs` *extends* readonly `AsyncResult`<`unknown`, `unknown`>[] |
|
|
701
|
-
|
|
702
|
-
#### Parameters
|
|
703
|
-
|
|
704
|
-
| Parameter | Type |
|
|
705
|
-
| ------ | ------ |
|
|
706
|
-
| `results` | readonly \[`Rs`\] |
|
|
707
|
-
|
|
708
|
-
#### Returns
|
|
709
|
-
|
|
710
|
-
`AsyncResult`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>>
|
|
711
|
-
|
|
712
|
-
#### Remarks
|
|
713
|
-
|
|
714
|
-
The inputs are resolved **concurrently** (order preserved); the resolved
|
|
715
|
-
`Result`s are then folded with the same rules as [all](#all) — first `Err`
|
|
716
|
-
short-circuits, any `Defect` dominates. As ever, the returned `AsyncResult`'s
|
|
717
|
-
internal promise never rejects. For a **record**, use [allFromDictAsync](#allfromdictasync).
|
|
718
|
-
|
|
719
|
-
#### Example
|
|
720
|
-
|
|
721
|
-
```ts
|
|
722
|
-
import { allAsync, fromSafePromise } from "unthrown";
|
|
723
|
-
await allAsync([fromSafePromise(a()), fromSafePromise(b())]);
|
|
724
|
-
```
|
|
725
|
-
|
|
726
|
-
***
|
|
727
|
-
|
|
728
|
-
### allFromDict()
|
|
729
|
-
|
|
730
|
-
```ts
|
|
731
|
-
function allFromDict<R>(results): Result<{ [K in string | number | symbol]: OkOf<R[K]> }, ErrOf<R[keyof R]>>;
|
|
732
|
-
```
|
|
733
|
-
|
|
734
|
-
Defined in: [packages/core/src/interop.ts:277](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L277)
|
|
735
|
-
|
|
736
|
-
Collect a **record** of [Result](#result)s into a single `Result` of a record of
|
|
737
|
-
their success values — `allFromDict({ a: Result<A, E>, b: Result<B, E> })` is
|
|
738
|
-
`Result<{ a: A; b: B }, E>`. The named counterpart of [all](#all), for
|
|
739
|
-
parallel work you'd rather not tuple.
|
|
740
|
-
|
|
741
|
-
#### Type Parameters
|
|
742
|
-
|
|
743
|
-
| Type Parameter |
|
|
744
|
-
| ------ |
|
|
745
|
-
| `R` *extends* `ResultRecord` |
|
|
746
|
-
|
|
747
|
-
#### Parameters
|
|
748
|
-
|
|
749
|
-
| Parameter | Type |
|
|
750
|
-
| ------ | ------ |
|
|
751
|
-
| `results` | `R` |
|
|
752
|
-
|
|
753
|
-
#### Returns
|
|
754
|
-
|
|
755
|
-
`Result`<\{ \[K in string \| number \| symbol\]: OkOf\<R\[K\]\> \}, [`ErrOf`](#errof)<`R`\[keyof `R`\]>>
|
|
756
|
-
|
|
757
|
-
#### Remarks
|
|
758
|
-
|
|
759
|
-
Same folding rules as [all](#all): first `Err` short-circuits, any `Defect`
|
|
760
|
-
dominates. This is **not** error accumulation.
|
|
761
|
-
|
|
762
|
-
#### Example
|
|
763
|
-
|
|
764
|
-
```ts
|
|
765
|
-
import { allFromDict, Ok } from "unthrown";
|
|
766
|
-
allFromDict({ id: Ok(1), name: Ok("ada") }).unwrap(); // { id: 1, name: "ada" }
|
|
767
|
-
```
|
|
768
|
-
|
|
769
|
-
***
|
|
770
|
-
|
|
771
|
-
### allFromDictAsync()
|
|
772
|
-
|
|
773
|
-
```ts
|
|
774
|
-
function allFromDictAsync<R>(results): AsyncResult<{ [K in string | number | symbol]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
775
|
-
```
|
|
776
|
-
|
|
777
|
-
Defined in: [packages/core/src/interop.ts:330](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L330)
|
|
778
|
-
|
|
779
|
-
The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
780
|
-
[AsyncResult](#asyncresult)s into one `AsyncResult` of a record of their values.
|
|
781
|
-
|
|
782
|
-
#### Type Parameters
|
|
783
|
-
|
|
784
|
-
| Type Parameter |
|
|
785
|
-
| ------ |
|
|
786
|
-
| `R` *extends* `AsyncResultRecord` |
|
|
787
|
-
|
|
788
|
-
#### Parameters
|
|
789
|
-
|
|
790
|
-
| Parameter | Type |
|
|
791
|
-
| ------ | ------ |
|
|
792
|
-
| `results` | `R` |
|
|
793
|
-
|
|
794
|
-
#### Returns
|
|
795
|
-
|
|
796
|
-
`AsyncResult`<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>>
|
|
797
|
-
|
|
798
|
-
#### Remarks
|
|
799
|
-
|
|
800
|
-
Resolved concurrently (order preserved), folded with the [all](#all) rules,
|
|
801
|
-
and the internal promise never rejects.
|
|
802
|
-
|
|
803
|
-
#### Example
|
|
804
|
-
|
|
805
|
-
```ts
|
|
806
|
-
import { allFromDictAsync, fromSafePromise } from "unthrown";
|
|
807
|
-
await allFromDictAsync({ a: fromSafePromise(a()), b: fromSafePromise(b()) });
|
|
808
|
-
```
|
|
809
|
-
|
|
810
|
-
***
|
|
811
|
-
|
|
812
|
-
### Do()
|
|
813
|
-
|
|
814
|
-
```ts
|
|
815
|
-
function Do(): Result<{
|
|
816
|
-
}, never>;
|
|
817
|
-
```
|
|
818
|
-
|
|
819
|
-
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/do.ts#L30)
|
|
820
|
-
|
|
821
|
-
Start a do-notation chain with an empty object scope, grown step by step with
|
|
822
|
-
`bind` (for `Result`-returning steps) and `let` (for pure values).
|
|
823
|
-
|
|
824
|
-
#### Returns
|
|
825
|
-
|
|
826
|
-
`Result`<\{
|
|
827
|
-
\}, `never`>
|
|
828
|
-
|
|
829
|
-
#### Remarks
|
|
830
|
-
|
|
831
|
-
Capitalised because `do` is a reserved word. Each step receives the scope
|
|
832
|
-
accumulated so far; the error types union across `bind`s, and a throw in any
|
|
833
|
-
step becomes a `Defect`. To go asynchronous, lift the chain with `toAsync()`
|
|
834
|
-
(then a `bind` may return an `AsyncResult`).
|
|
835
|
-
|
|
836
|
-
#### Example
|
|
837
|
-
|
|
838
|
-
```ts
|
|
839
|
-
import { Do, Ok } from "unthrown";
|
|
840
|
-
|
|
841
|
-
const result = Do()
|
|
842
|
-
.bind("user", () => findUser(id)) // Result<User, NotFound>
|
|
843
|
-
.bind("org", ({ user }) => findOrg(user.orgId)) // Result<Org, NotFound>
|
|
844
|
-
.let("label", ({ user, org }) => `${user.name} @ ${org.name}`)
|
|
845
|
-
.map(({ user, org, label }) => render(user, org, label));
|
|
846
|
-
// Result<View, NotFound>
|
|
847
|
-
```
|
|
848
|
-
|
|
849
|
-
***
|
|
850
|
-
|
|
851
|
-
### Err()
|
|
852
|
-
|
|
853
|
-
```ts
|
|
854
|
-
function Err<E>(error): Result<never, E>;
|
|
855
|
-
```
|
|
856
|
-
|
|
857
|
-
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L34)
|
|
858
|
-
|
|
859
|
-
Construct a failed [Result](#result) carrying a **modeled** error.
|
|
860
|
-
|
|
861
|
-
#### Type Parameters
|
|
862
|
-
|
|
863
|
-
| Type Parameter | Description |
|
|
864
|
-
| ------ | ------ |
|
|
865
|
-
| `E` | the modeled error type. |
|
|
866
|
-
|
|
867
|
-
#### Parameters
|
|
868
|
-
|
|
869
|
-
| Parameter | Type | Description |
|
|
870
|
-
| ------ | ------ | ------ |
|
|
871
|
-
| `error` | `E` | the domain error to wrap. |
|
|
872
|
-
|
|
873
|
-
#### Returns
|
|
874
|
-
|
|
875
|
-
`Result`<`never`, `E`>
|
|
876
|
-
|
|
877
|
-
#### Example
|
|
878
|
-
|
|
879
|
-
```ts
|
|
880
|
-
import { Err } from "unthrown";
|
|
881
|
-
Err("not_found").unwrapErr(); // "not_found"
|
|
882
|
-
```
|
|
883
|
-
|
|
884
|
-
***
|
|
885
|
-
|
|
886
|
-
### fromNullable()
|
|
887
|
-
|
|
888
|
-
```ts
|
|
889
|
-
function fromNullable<T, E>(value, onAbsent): Result<NonNullable<T>, E>;
|
|
890
|
-
```
|
|
891
|
-
|
|
892
|
-
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L29)
|
|
893
|
-
|
|
894
|
-
Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
895
|
-
`Err`. The sanctioned alternative to an `Option` type.
|
|
896
|
-
|
|
897
|
-
#### Type Parameters
|
|
898
|
-
|
|
899
|
-
| Type Parameter | Description |
|
|
900
|
-
| ------ | ------ |
|
|
901
|
-
| `T` | the (nullable) value type. |
|
|
902
|
-
| `E` | the error produced when the value is absent. |
|
|
903
|
-
|
|
904
|
-
#### Parameters
|
|
905
|
-
|
|
906
|
-
| Parameter | Type | Description |
|
|
907
|
-
| ------ | ------ | ------ |
|
|
908
|
-
| `value` | `T` \| `null` \| `undefined` | the possibly-absent value. |
|
|
909
|
-
| `onAbsent` | () => `E` | lazily produces the error for the absent case. |
|
|
910
|
-
|
|
911
|
-
#### Returns
|
|
912
|
-
|
|
913
|
-
`Result`<`NonNullable`<`T`>, `E`>
|
|
914
|
-
|
|
915
|
-
#### Remarks
|
|
916
|
-
|
|
917
|
-
`null` and `undefined` map to `Err(onAbsent())`; any other value (including
|
|
918
|
-
falsy ones like `0`, `""`, `false`) maps to `Ok`.
|
|
919
|
-
|
|
920
|
-
#### Example
|
|
921
|
-
|
|
922
|
-
```ts
|
|
923
|
-
import { fromNullable } from "unthrown";
|
|
924
|
-
fromNullable(map.get(key), () => "missing").unwrap();
|
|
925
|
-
```
|
|
926
|
-
|
|
927
|
-
***
|
|
928
|
-
|
|
929
|
-
### fromPromise()
|
|
930
|
-
|
|
931
|
-
```ts
|
|
932
|
-
function fromPromise<T, R>(promise, qualify): AsyncResult<T, Exclude<R, Defect>>;
|
|
933
|
-
```
|
|
934
|
-
|
|
935
|
-
Defined in: [packages/core/src/interop.ts:113](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L113)
|
|
936
|
-
|
|
937
|
-
Wrap a `Promise` (or a thunk producing one) as an [AsyncResult](#asyncresult), forcing
|
|
938
|
-
every rejection to be triaged.
|
|
939
|
-
|
|
940
|
-
#### Type Parameters
|
|
941
|
-
|
|
942
|
-
| Type Parameter | Description |
|
|
943
|
-
| ------ | ------ |
|
|
944
|
-
| `T` | the resolved value type. |
|
|
945
|
-
| `R` | `qualify`'s return type; the modeled error `E` is `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted). |
|
|
946
|
-
|
|
947
|
-
#### Parameters
|
|
948
|
-
|
|
949
|
-
| Parameter | Type | Description |
|
|
950
|
-
| ------ | ------ | ------ |
|
|
951
|
-
| `promise` | `Promise`<`T`> \| (() => `Promise`<`T`>) | the promise, or a thunk returning one. |
|
|
952
|
-
| `qualify` | (`cause`, `defect`) => `R` | triages a rejection `cause` into a modeled `E`, or marks it unmodeled by returning `defect(cause)` (the helper passed as its second arg). |
|
|
953
|
-
|
|
954
|
-
#### Returns
|
|
955
|
-
|
|
956
|
-
`AsyncResult`<`T`, `Exclude`<`R`, `Defect`>>
|
|
957
|
-
|
|
958
|
-
#### Remarks
|
|
959
|
-
|
|
960
|
-
`qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
961
|
-
`Defect` (via the injected `defect` helper, its second argument). The returned
|
|
962
|
-
`AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
963
|
-
`Result`. A throw inside `qualify` is itself a `Defect`.
|
|
964
|
-
|
|
965
|
-
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
966
|
-
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
967
|
-
`qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
968
|
-
rejection is a Defect, prefer [fromSafePromise](#fromsafepromise).
|
|
969
|
-
|
|
970
|
-
#### Example
|
|
971
|
-
|
|
972
|
-
```ts
|
|
973
|
-
import { fromPromise } from "unthrown";
|
|
974
|
-
const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
975
|
-
cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
976
|
-
);
|
|
977
|
-
```
|
|
978
|
-
|
|
979
|
-
***
|
|
980
|
-
|
|
981
|
-
### fromSafePromise()
|
|
982
|
-
|
|
983
|
-
```ts
|
|
984
|
-
function fromSafePromise<T>(promise): AsyncResult<T, never>;
|
|
985
|
-
```
|
|
986
|
-
|
|
987
|
-
Defined in: [packages/core/src/interop.ts:139](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L139)
|
|
988
|
-
|
|
989
|
-
Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
990
|
-
becomes a `Defect`.
|
|
991
|
-
|
|
992
|
-
#### Type Parameters
|
|
993
|
-
|
|
994
|
-
| Type Parameter | Description |
|
|
995
|
-
| ------ | ------ |
|
|
996
|
-
| `T` | the resolved value type. |
|
|
997
|
-
|
|
998
|
-
#### Parameters
|
|
999
|
-
|
|
1000
|
-
| Parameter | Type | Description |
|
|
1001
|
-
| ------ | ------ | ------ |
|
|
1002
|
-
| `promise` | `Promise`<`T`> \| (() => `Promise`<`T`>) | the promise, or a thunk returning one. |
|
|
1003
|
-
|
|
1004
|
-
#### Returns
|
|
1005
|
-
|
|
1006
|
-
`AsyncResult`<`T`, `never`>
|
|
1007
|
-
|
|
1008
|
-
#### Remarks
|
|
1009
|
-
|
|
1010
|
-
Use this only when a rejection genuinely indicates a bug rather than an
|
|
1011
|
-
anticipated outcome — the error channel is `never`, so there is nothing to
|
|
1012
|
-
triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
1013
|
-
|
|
1014
|
-
***
|
|
1015
|
-
|
|
1016
|
-
### fromThrowable()
|
|
1017
|
-
|
|
1018
|
-
```ts
|
|
1019
|
-
function fromThrowable<A, T, R>(fn, qualify): (...args) => Result<T, Exclude<R, Defect>>;
|
|
1020
|
-
```
|
|
1021
|
-
|
|
1022
|
-
Defined in: [packages/core/src/interop.ts:68](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/interop.ts#L68)
|
|
1023
|
-
|
|
1024
|
-
Wrap a throwing synchronous function so it returns a [Result](#result) instead of
|
|
1025
|
-
throwing.
|
|
1026
|
-
|
|
1027
|
-
#### Type Parameters
|
|
1028
|
-
|
|
1029
|
-
| Type Parameter | Description |
|
|
1030
|
-
| ------ | ------ |
|
|
1031
|
-
| `A` *extends* `unknown`[] | the wrapped function's argument tuple. |
|
|
1032
|
-
| `T` | the wrapped function's return type. |
|
|
1033
|
-
| `R` | `qualify`'s return type; the modeled error `E` is `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted). |
|
|
1034
|
-
|
|
1035
|
-
#### Parameters
|
|
1036
|
-
|
|
1037
|
-
| Parameter | Type | Description |
|
|
1038
|
-
| ------ | ------ | ------ |
|
|
1039
|
-
| `fn` | (...`args`) => `T` | the throwing function to wrap. |
|
|
1040
|
-
| `qualify` | (`cause`, `defect`) => `R` | triages a thrown `cause` into a modeled `E`, or marks it unmodeled by returning `defect(cause)` (the helper passed as its second arg). |
|
|
1041
|
-
|
|
1042
|
-
#### Returns
|
|
1043
|
-
|
|
1044
|
-
a function with the same arguments returning `Result<T, E>`.
|
|
1045
|
-
|
|
1046
|
-
(...`args`) => `Result`<`T`, `Exclude`<`R`, `Defect`>>
|
|
1047
|
-
|
|
1048
|
-
#### Remarks
|
|
1049
|
-
|
|
1050
|
-
`qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
1051
|
-
`Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
1052
|
-
path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
1053
|
-
as a `Defect`.
|
|
1054
|
-
|
|
1055
|
-
The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
1056
|
-
`qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
1057
|
-
`qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
1058
|
-
out-of-band and must not pollute the error channel); reach for
|
|
1059
|
-
[fromSafePromise](#fromsafepromise) when every failure is a Defect.
|
|
1060
|
-
|
|
1061
|
-
#### Example
|
|
1062
|
-
|
|
1063
|
-
```ts
|
|
1064
|
-
import { fromThrowable } from "unthrown";
|
|
1065
|
-
const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
1066
|
-
parse("{}").unwrap();
|
|
1067
|
-
```
|
|
1068
|
-
|
|
1069
|
-
***
|
|
1070
|
-
|
|
1071
|
-
### isDefect()
|
|
1072
|
-
|
|
1073
|
-
```ts
|
|
1074
|
-
function isDefect<T, E>(r): r is DefectView<T, E>;
|
|
1075
|
-
```
|
|
1076
|
-
|
|
1077
|
-
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L66)
|
|
1078
|
-
|
|
1079
|
-
Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause`.
|
|
1080
|
-
|
|
1081
|
-
#### Type Parameters
|
|
1082
|
-
|
|
1083
|
-
| Type Parameter |
|
|
1084
|
-
| ------ |
|
|
1085
|
-
| `T` |
|
|
1086
|
-
| `E` |
|
|
1087
|
-
|
|
1088
|
-
#### Parameters
|
|
1089
|
-
|
|
1090
|
-
| Parameter | Type |
|
|
1091
|
-
| ------ | ------ |
|
|
1092
|
-
| `r` | `Result`<`T`, `E`> |
|
|
1093
|
-
|
|
1094
|
-
#### Returns
|
|
1095
|
-
|
|
1096
|
-
`r is DefectView<T, E>`
|
|
1097
|
-
|
|
1098
|
-
`true` when `r` is a `Defect`.
|
|
1099
|
-
|
|
1100
|
-
***
|
|
1101
|
-
|
|
1102
|
-
### isErr()
|
|
1103
|
-
|
|
1104
|
-
```ts
|
|
1105
|
-
function isErr<T, E>(r): r is ErrView<E, T>;
|
|
1106
|
-
```
|
|
1107
|
-
|
|
1108
|
-
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L58)
|
|
1109
|
-
|
|
1110
|
-
Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
1111
|
-
|
|
1112
|
-
#### Type Parameters
|
|
1113
|
-
|
|
1114
|
-
| Type Parameter |
|
|
1115
|
-
| ------ |
|
|
1116
|
-
| `T` |
|
|
1117
|
-
| `E` |
|
|
1118
|
-
|
|
1119
|
-
#### Parameters
|
|
1120
|
-
|
|
1121
|
-
| Parameter | Type |
|
|
1122
|
-
| ------ | ------ |
|
|
1123
|
-
| `r` | `Result`<`T`, `E`> |
|
|
1124
|
-
|
|
1125
|
-
#### Returns
|
|
1126
|
-
|
|
1127
|
-
`r is ErrView<E, T>`
|
|
1128
|
-
|
|
1129
|
-
`true` when `r` is `Err`.
|
|
1130
|
-
|
|
1131
|
-
***
|
|
1132
|
-
|
|
1133
|
-
### isOk()
|
|
1134
|
-
|
|
1135
|
-
```ts
|
|
1136
|
-
function isOk<T, E>(r): r is OkView<T, E>;
|
|
1137
|
-
```
|
|
1138
|
-
|
|
1139
|
-
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L50)
|
|
1140
|
-
|
|
1141
|
-
Type guard: narrow a [Result](#result) to its `Ok` variant, exposing `.value`.
|
|
1142
|
-
|
|
1143
|
-
#### Type Parameters
|
|
1144
|
-
|
|
1145
|
-
| Type Parameter |
|
|
1146
|
-
| ------ |
|
|
1147
|
-
| `T` |
|
|
1148
|
-
| `E` |
|
|
1149
|
-
|
|
1150
|
-
#### Parameters
|
|
1151
|
-
|
|
1152
|
-
| Parameter | Type |
|
|
1153
|
-
| ------ | ------ |
|
|
1154
|
-
| `r` | `Result`<`T`, `E`> |
|
|
1155
|
-
|
|
1156
|
-
#### Returns
|
|
1157
|
-
|
|
1158
|
-
`r is OkView<T, E>`
|
|
1159
|
-
|
|
1160
|
-
`true` when `r` is `Ok`.
|
|
1161
|
-
|
|
1162
|
-
#### Example
|
|
1163
|
-
|
|
1164
|
-
```ts
|
|
1165
|
-
import { isOk, type Result } from "unthrown";
|
|
1166
|
-
declare const r: Result<number, string>;
|
|
1167
|
-
if (isOk(r)) r.value; // number, narrowed
|
|
1168
|
-
```
|
|
1169
|
-
|
|
1170
|
-
***
|
|
1171
|
-
|
|
1172
|
-
### isResult()
|
|
1173
|
-
|
|
1174
|
-
```ts
|
|
1175
|
-
function isResult(x): x is Result<unknown, unknown>;
|
|
1176
|
-
```
|
|
1177
|
-
|
|
1178
|
-
Defined in: [packages/core/src/core.ts:338](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/core.ts#L338)
|
|
1179
|
-
|
|
1180
|
-
Type guard: is `x` a [Result](#result) (any of `Ok` / `Err` / `Defect`)?
|
|
1181
|
-
|
|
1182
|
-
#### Parameters
|
|
1183
|
-
|
|
1184
|
-
| Parameter | Type |
|
|
1185
|
-
| ------ | ------ |
|
|
1186
|
-
| `x` | `unknown` |
|
|
1187
|
-
|
|
1188
|
-
#### Returns
|
|
1189
|
-
|
|
1190
|
-
`x is Result<unknown, unknown>`
|
|
1191
|
-
|
|
1192
|
-
`true` when `x` is a `Result` produced by this library.
|
|
1193
|
-
|
|
1194
|
-
#### Remarks
|
|
1195
|
-
|
|
1196
|
-
Unlike [isOk](#isok) / [isErr](#iserr) / [isDefect](#isdefect), which narrow a value
|
|
1197
|
-
already known to be a `Result`, this narrows from `unknown` — useful at an
|
|
1198
|
-
untyped boundary. It checks the value carries the `Result` prototype, so a
|
|
1199
|
-
look-alike plain object (`{ tag: "Ok" }`) is **not** matched. An `AsyncResult`
|
|
1200
|
-
is not a `Result` and returns `false`.
|
|
1201
|
-
|
|
1202
|
-
***
|
|
1203
|
-
|
|
1204
|
-
### matchTags()
|
|
1205
|
-
|
|
1206
|
-
#### Call Signature
|
|
1207
|
-
|
|
1208
|
-
```ts
|
|
1209
|
-
function matchTags<T, E, R>(result, handlers): R;
|
|
1210
|
-
```
|
|
1211
|
-
|
|
1212
|
-
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L138)
|
|
1213
|
-
|
|
1214
|
-
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1215
|
-
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
1216
|
-
|
|
1217
|
-
##### Type Parameters
|
|
1218
|
-
|
|
1219
|
-
| Type Parameter | Description |
|
|
1220
|
-
| ------ | ------ |
|
|
1221
|
-
| `T` | the success value type. |
|
|
1222
|
-
| `E` *extends* `object` | the tagged error union (`E extends { _tag: string }`). |
|
|
1223
|
-
| `R` | the folded result type. |
|
|
1224
|
-
|
|
1225
|
-
##### Parameters
|
|
1226
|
-
|
|
1227
|
-
| Parameter | Type | Description |
|
|
1228
|
-
| ------ | ------ | ------ |
|
|
1229
|
-
| `result` | `Result`<`T`, `E`> | the result to fold. |
|
|
1230
|
-
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1231
|
-
|
|
1232
|
-
##### Returns
|
|
1233
|
-
|
|
1234
|
-
`R`
|
|
1235
|
-
|
|
1236
|
-
##### Remarks
|
|
1237
|
-
|
|
1238
|
-
The `handlers` object must provide `Ok`, `Defect`, and exactly one function
|
|
1239
|
-
per error tag; each tag's handler receives the narrowed error variant. A
|
|
1240
|
-
missing tag is a compile error. For an `AsyncResult`, the fold resolves to a
|
|
1241
|
-
`Promise<R>`.
|
|
1242
|
-
|
|
1243
|
-
##### Example
|
|
1244
|
-
|
|
1245
|
-
```ts
|
|
1246
|
-
class NotFound extends TaggedError("NotFound") {}
|
|
1247
|
-
class Forbidden extends TaggedError("Forbidden")<{ user: string }> {}
|
|
1248
|
-
|
|
1249
|
-
declare const r: Result<number, NotFound | Forbidden>;
|
|
1250
|
-
matchTags(r, {
|
|
1251
|
-
Ok: (n) => `got ${n}`,
|
|
1252
|
-
Defect: (cause) => `bug: ${String(cause)}`,
|
|
1253
|
-
NotFound: () => "404",
|
|
1254
|
-
Forbidden: (e) => `403 for ${e.user}`,
|
|
1255
|
-
});
|
|
1256
|
-
```
|
|
1257
|
-
|
|
1258
|
-
#### Call Signature
|
|
1259
|
-
|
|
1260
|
-
```ts
|
|
1261
|
-
function matchTags<T, E, R>(result, handlers): Promise<R>;
|
|
1262
|
-
```
|
|
1263
|
-
|
|
1264
|
-
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L142)
|
|
1265
|
-
|
|
1266
|
-
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1267
|
-
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
1268
|
-
|
|
1269
|
-
##### Type Parameters
|
|
1270
|
-
|
|
1271
|
-
| Type Parameter | Description |
|
|
1272
|
-
| ------ | ------ |
|
|
1273
|
-
| `T` | the success value type. |
|
|
1274
|
-
| `E` *extends* `object` | the tagged error union (`E extends { _tag: string }`). |
|
|
1275
|
-
| `R` | the folded result type. |
|
|
1276
|
-
|
|
1277
|
-
##### Parameters
|
|
1278
|
-
|
|
1279
|
-
| Parameter | Type | Description |
|
|
1280
|
-
| ------ | ------ | ------ |
|
|
1281
|
-
| `result` | `AsyncResult`<`T`, `E`> | the result to fold. |
|
|
1282
|
-
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1283
|
-
|
|
1284
|
-
##### Returns
|
|
1285
|
-
|
|
1286
|
-
`Promise`<`R`>
|
|
1287
|
-
|
|
1288
|
-
##### Remarks
|
|
1289
|
-
|
|
1290
|
-
The `handlers` object must provide `Ok`, `Defect`, and exactly one function
|
|
1291
|
-
per error tag; each tag's handler receives the narrowed error variant. A
|
|
1292
|
-
missing tag is a compile error. For an `AsyncResult`, the fold resolves to a
|
|
1293
|
-
`Promise<R>`.
|
|
1294
|
-
|
|
1295
|
-
##### Example
|
|
1296
|
-
|
|
1297
|
-
```ts
|
|
1298
|
-
class NotFound extends TaggedError("NotFound") {}
|
|
1299
|
-
class Forbidden extends TaggedError("Forbidden")<{ user: string }> {}
|
|
1300
|
-
|
|
1301
|
-
declare const r: Result<number, NotFound | Forbidden>;
|
|
1302
|
-
matchTags(r, {
|
|
1303
|
-
Ok: (n) => `got ${n}`,
|
|
1304
|
-
Defect: (cause) => `bug: ${String(cause)}`,
|
|
1305
|
-
NotFound: () => "404",
|
|
1306
|
-
Forbidden: (e) => `403 for ${e.user}`,
|
|
1307
|
-
});
|
|
1308
|
-
```
|
|
1309
|
-
|
|
1310
|
-
***
|
|
1311
|
-
|
|
1312
|
-
### Ok()
|
|
1313
|
-
|
|
1314
|
-
```ts
|
|
1315
|
-
function Ok<T>(value): Result<T, never>;
|
|
1316
|
-
```
|
|
1317
|
-
|
|
1318
|
-
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/constructors.ts#L18)
|
|
1319
|
-
|
|
1320
|
-
Construct a successful [Result](#result).
|
|
1321
|
-
|
|
1322
|
-
#### Type Parameters
|
|
1323
|
-
|
|
1324
|
-
| Type Parameter | Description |
|
|
1325
|
-
| ------ | ------ |
|
|
1326
|
-
| `T` | the success value type. |
|
|
1327
|
-
|
|
1328
|
-
#### Parameters
|
|
1329
|
-
|
|
1330
|
-
| Parameter | Type | Description |
|
|
1331
|
-
| ------ | ------ | ------ |
|
|
1332
|
-
| `value` | `T` | the success value to wrap. |
|
|
1333
|
-
|
|
1334
|
-
#### Returns
|
|
1335
|
-
|
|
1336
|
-
`Result`<`T`, `never`>
|
|
1337
|
-
|
|
1338
|
-
#### Example
|
|
1339
|
-
|
|
1340
|
-
```ts
|
|
1341
|
-
import { Ok } from "unthrown";
|
|
1342
|
-
Ok(42).unwrap(); // 42
|
|
1343
|
-
```
|
|
1344
|
-
|
|
1345
|
-
***
|
|
1346
|
-
|
|
1347
|
-
### TaggedError()
|
|
1348
|
-
|
|
1349
|
-
```ts
|
|
1350
|
-
function TaggedError<Tag>(tag, options?): TaggedErrorConstructor<Tag>;
|
|
1351
|
-
```
|
|
1352
|
-
|
|
1353
|
-
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/544e3bf9c78133de7b31b82a17110bf70ee814fb/packages/core/src/tagged.ts#L72)
|
|
1354
|
-
|
|
1355
|
-
Build a base class for a tagged error — a class extending `Error` with a
|
|
1356
|
-
`_tag` string discriminant, in the style of Effect's `Data.TaggedError`.
|
|
1357
|
-
|
|
1358
|
-
#### Type Parameters
|
|
1359
|
-
|
|
1360
|
-
| Type Parameter | Description |
|
|
1361
|
-
| ------ | ------ |
|
|
1362
|
-
| `Tag` *extends* `string` | the string literal discriminant. |
|
|
1363
|
-
|
|
1364
|
-
#### Parameters
|
|
1365
|
-
|
|
1366
|
-
| Parameter | Type | Description |
|
|
1367
|
-
| ------ | ------ | ------ |
|
|
1368
|
-
| `tag` | `Tag` | the discriminant value; also the default error `name`. |
|
|
1369
|
-
| `options?` | \{ `name?`: `string`; \} | optional overrides. `options.name` sets `Error.name` independently of `tag` (defaults to `tag`). |
|
|
1370
|
-
| `options.name?` | `string` | - |
|
|
1371
|
-
|
|
1372
|
-
#### Returns
|
|
1373
|
-
|
|
1374
|
-
[`TaggedErrorConstructor`](#taggederrorconstructor)<`Tag`>
|
|
1375
|
-
|
|
1376
|
-
#### Remarks
|
|
1377
|
-
|
|
1378
|
-
Extend the returned class to declare a concrete error. Supply the payload with
|
|
1379
|
-
an instantiation expression; omit it for a payload-less error. A `message`
|
|
1380
|
-
field in the payload is forwarded to `Error`. The `_tag` always reflects
|
|
1381
|
-
`tag` and cannot be overridden by the payload.
|
|
1382
|
-
|
|
1383
|
-
`_tag` is the discriminant used by [matchTags](#matchtags); `Error.name` is the
|
|
1384
|
-
human-facing label in stack traces and logs. By default they coincide, but
|
|
1385
|
-
they can be **decoupled** with `options.name` — so a tag can be namespaced for
|
|
1386
|
-
collision-safety (`"@my-lib/RetryableError"`) without that slash-prefixed
|
|
1387
|
-
string leaking into `Error.name`:
|
|
1388
|
-
|
|
1389
|
-
```ts
|
|
1390
|
-
class RetryableError extends TaggedError("@my-lib/RetryableError", {
|
|
1391
|
-
name: "RetryableError",
|
|
1392
|
-
})<{ message: string }> {}
|
|
1393
|
-
|
|
1394
|
-
const e = new RetryableError({ message: "boom" });
|
|
1395
|
-
e._tag; // "@my-lib/RetryableError" — namespaced discriminant
|
|
1396
|
-
e.name; // "RetryableError" — clean display name
|
|
1397
|
-
```
|
|
1398
|
-
|
|
1399
|
-
#### Example
|
|
1400
|
-
|
|
1401
|
-
```ts
|
|
1402
|
-
class NotFound extends TaggedError("NotFound") {}
|
|
1403
|
-
class HttpError extends TaggedError("HttpError")<{ status: number }> {}
|
|
1404
|
-
|
|
1405
|
-
new NotFound()._tag; // "NotFound"
|
|
1406
|
-
new HttpError({ status: 500 }).status; // 500
|
|
1407
|
-
```
|