unthrown 1.0.0 → 2.0.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 +76 -12
- package/dist/index.d.cts +100 -35
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +100 -35
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +75 -13
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +181 -124
- package/package.json +1 -1
package/docs/index.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
### UnwrapError
|
|
10
10
|
|
|
11
|
-
Defined in: [packages/core/src/core.ts:30](https://github.com/btravstack/unthrown/blob/
|
|
11
|
+
Defined in: [packages/core/src/core.ts:30](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/core.ts#L30)
|
|
12
12
|
|
|
13
13
|
Thrown by a [Result](#result)'s `unwrap` / `unwrapErr` when the assertion is
|
|
14
14
|
wrong on a *modeled* result — `unwrap()` on an `Err`, or `unwrapErr()` on an
|
|
@@ -37,7 +37,7 @@ re-thrown (with its original stack) instead.
|
|
|
37
37
|
new UnwrapError<E>(error): UnwrapError<E>;
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Defined in: [packages/core/src/core.ts:36](https://github.com/btravstack/unthrown/blob/
|
|
40
|
+
Defined in: [packages/core/src/core.ts:36](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/core.ts#L36)
|
|
41
41
|
|
|
42
42
|
###### Parameters
|
|
43
43
|
|
|
@@ -60,7 +60,7 @@ Error.constructor
|
|
|
60
60
|
| Property | Modifier | Type | Description | Inherited from | Defined in |
|
|
61
61
|
| ------ | ------ | ------ | ------ | ------ | ------ |
|
|
62
62
|
| <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 |
|
|
63
|
-
| <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:35](https://github.com/btravstack/unthrown/blob/
|
|
63
|
+
| <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:35](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/core.ts#L35) |
|
|
64
64
|
| <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 |
|
|
65
65
|
| <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 |
|
|
66
66
|
| <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 |
|
|
@@ -174,7 +174,7 @@ Error.prepareStackTrace
|
|
|
174
174
|
type AsyncErrOf<R> = R extends AsyncResult<unknown, infer E> ? E : never;
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
-
Defined in: [packages/core/src/types.ts:
|
|
177
|
+
Defined in: [packages/core/src/types.ts:468](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L468)
|
|
178
178
|
|
|
179
179
|
Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
180
180
|
|
|
@@ -192,7 +192,7 @@ Extract the error type `E` from an [AsyncResult](#asyncresult).
|
|
|
192
192
|
type AsyncOkOf<R> = R extends AsyncResult<infer T, unknown> ? T : never;
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
Defined in: [packages/core/src/types.ts:
|
|
195
|
+
Defined in: [packages/core/src/types.ts:462](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L462)
|
|
196
196
|
|
|
197
197
|
Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
198
198
|
|
|
@@ -207,58 +207,39 @@ Extract the success type `T` from an [AsyncResult](#asyncresult).
|
|
|
207
207
|
### AsyncResult
|
|
208
208
|
|
|
209
209
|
```ts
|
|
210
|
-
type AsyncResult<T, E> =
|
|
210
|
+
type AsyncResult<T, E> = AsyncResultType<T, E>;
|
|
211
211
|
```
|
|
212
212
|
|
|
213
|
-
Defined in: [packages/core/src/
|
|
213
|
+
Defined in: [packages/core/src/facade.ts:88](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L88)
|
|
214
214
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
| Name | Type | Description | Defined in |
|
|
221
|
-
| ------ | ------ | ------ | ------ |
|
|
222
|
-
| `as()` | (`value`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `as`. | [packages/core/src/types.ts:381](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L381) |
|
|
223
|
-
| `bind()` | (`name`, `f`) => [`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: (Omit\<T, K\> & \{ readonly \[P in string\]: U \})\[K\] \}, `E` \| `E2`> | Asynchronous `bind` (do-notation). `f` may return a `Result` **or** an `AsyncResult`; its value is bound under `name` in the accumulating scope. | [packages/core/src/types.ts:374](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L374) |
|
|
224
|
-
| `flatMap()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E` \| `E2`> | Asynchronous `flatMap`. `f` may return a `Result` **or** an `AsyncResult` (never a raw `Promise`); a throw becomes a `Defect`. | [packages/core/src/types.ts:359](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L359) |
|
|
225
|
-
| `flatTap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E` \| `E2`> | Asynchronous `flatTap` — a failable tap that keeps the original value. `f` may return a `Result` **or** an `AsyncResult`; its `Ok` value is discarded, an `Err`/`Defect` short-circuits, and a throw becomes a `Defect`. | [packages/core/src/types.ts:367](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L367) |
|
|
226
|
-
| `getOrNull()` | () => `Promise`<`T` \| `null`> | Asynchronous `getOrNull`. | [packages/core/src/types.ts:414](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L414) |
|
|
227
|
-
| `getOrUndefined()` | () => `Promise`<`T` \| `undefined`> | Asynchronous `getOrUndefined`. | [packages/core/src/types.ts:416](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L416) |
|
|
228
|
-
| `let()` | (`name`, `f`) => [`AsyncResult`](#asyncresult)<\{ \[K in string \| number \| symbol\]: (Omit\<T, K\> & \{ readonly \[P in string\]: U \})\[K\] \}, `E`> | Asynchronous `let` (do-notation). `f` returns a plain value, bound under `name`. | [packages/core/src/types.ts:379](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L379) |
|
|
229
|
-
| `map()` | (`f`) => [`AsyncResult`](#asyncresult)<`U`, `E`> | Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:354](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L354) |
|
|
230
|
-
| `mapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E2`> | Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:384](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L384) |
|
|
231
|
-
| `match()` | (`cases`) => `Promise`<`R`> | Asynchronous `match`. Handlers are synchronous; resolves to `R`. | [packages/core/src/types.ts:400](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L400) |
|
|
232
|
-
| `orElse()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E2`> | Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:386](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L386) |
|
|
233
|
-
| `recover()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `never`> | Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:388](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L388) |
|
|
234
|
-
| `recoverDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T` \| `U`, `E` \| `E2`> | Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. | [packages/core/src/types.ts:393](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L393) |
|
|
235
|
-
| `tap()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:361](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L361) |
|
|
236
|
-
| `tapDefect()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapDefect`. | [packages/core/src/types.ts:397](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L397) |
|
|
237
|
-
| `tapErr()` | (`f`) => [`AsyncResult`](#asyncresult)<`T`, `E`> | Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. | [packages/core/src/types.ts:390](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L390) |
|
|
238
|
-
| `unwrap()` | () => `Promise`<`T`> | Asynchronous `unwrap`. The returned promise rejects on `Err`/`Defect`. | [packages/core/src/types.ts:406](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L406) |
|
|
239
|
-
| `unwrapErr()` | () => `Promise`<`E`> | Asynchronous `unwrapErr`. | [packages/core/src/types.ts:408](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L408) |
|
|
240
|
-
| `unwrapOr()` | (`fallback`) => `Promise`<`T`> | Asynchronous `unwrapOr`. | [packages/core/src/types.ts:410](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L410) |
|
|
241
|
-
| `unwrapOrElse()` | (`f`) => `Promise`<`T`> | Asynchronous `unwrapOrElse`. | [packages/core/src/types.ts:412](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/types.ts#L412) |
|
|
215
|
+
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
216
|
+
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
217
|
+
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
218
|
+
[AsyncResult.allFromDict](#property-allfromdict).
|
|
242
219
|
|
|
243
220
|
#### Type Parameters
|
|
244
221
|
|
|
245
|
-
| Type Parameter |
|
|
246
|
-
| ------ |
|
|
247
|
-
| `T` |
|
|
248
|
-
| `E` |
|
|
222
|
+
| Type Parameter |
|
|
223
|
+
| ------ |
|
|
224
|
+
| `T` |
|
|
225
|
+
| `E` |
|
|
249
226
|
|
|
250
227
|
#### Remarks
|
|
251
228
|
|
|
252
|
-
|
|
253
|
-
`
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
`AsyncResult`.
|
|
229
|
+
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
230
|
+
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
231
|
+
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
232
|
+
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
233
|
+
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
234
|
+
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
235
|
+
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
260
236
|
|
|
261
|
-
|
|
237
|
+
#### Example
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { AsyncResult, Defect } from "unthrown";
|
|
241
|
+
const user = await AsyncResult.fromPromise(fetchUser(id), (c) => Defect(c));
|
|
242
|
+
```
|
|
262
243
|
|
|
263
244
|
***
|
|
264
245
|
|
|
@@ -268,7 +249,7 @@ To pattern-match an `AsyncResult`, `await` it first: `match(await ar)`.
|
|
|
268
249
|
type Awaitable<T> = object;
|
|
269
250
|
```
|
|
270
251
|
|
|
271
|
-
Defined in: [packages/core/src/types.ts:
|
|
252
|
+
Defined in: [packages/core/src/types.ts:346](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L346)
|
|
272
253
|
|
|
273
254
|
A success-only thenable: awaitable, but deliberately **not** a full
|
|
274
255
|
`PromiseLike`.
|
|
@@ -295,7 +276,7 @@ being treated as a raw promise (e.g. dropped into `Promise.all`).
|
|
|
295
276
|
then<R>(onfulfilled?): PromiseLike<R>;
|
|
296
277
|
```
|
|
297
278
|
|
|
298
|
-
Defined in: [packages/core/src/types.ts:
|
|
279
|
+
Defined in: [packages/core/src/types.ts:347](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L347)
|
|
299
280
|
|
|
300
281
|
###### Type Parameters
|
|
301
282
|
|
|
@@ -321,7 +302,7 @@ Defined in: [packages/core/src/types.ts:330](https://github.com/btravstack/unthr
|
|
|
321
302
|
type Defect = object;
|
|
322
303
|
```
|
|
323
304
|
|
|
324
|
-
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/
|
|
305
|
+
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/defect.ts#L36)
|
|
325
306
|
|
|
326
307
|
The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
327
308
|
|
|
@@ -337,8 +318,8 @@ runtime state of a `Result`.
|
|
|
337
318
|
|
|
338
319
|
| Property | Modifier | Type | Defined in |
|
|
339
320
|
| ------ | ------ | ------ | ------ |
|
|
340
|
-
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/
|
|
341
|
-
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/
|
|
321
|
+
| <a id="defect-1"></a> `[DEFECT]` | `readonly` | `true` | [packages/core/src/defect.ts:16](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/defect.ts#L16) |
|
|
322
|
+
| <a id="cause-1"></a> `cause` | `readonly` | `unknown` | [packages/core/src/defect.ts:17](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/defect.ts#L17) |
|
|
342
323
|
|
|
343
324
|
***
|
|
344
325
|
|
|
@@ -348,7 +329,7 @@ runtime state of a `Result`.
|
|
|
348
329
|
type DefectView<T, E> = ResultMethods<T, E> & object;
|
|
349
330
|
```
|
|
350
331
|
|
|
351
|
-
Defined in: [packages/core/src/types.ts:
|
|
332
|
+
Defined in: [packages/core/src/types.ts:289](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L289)
|
|
352
333
|
|
|
353
334
|
The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `cause`.
|
|
354
335
|
|
|
@@ -356,8 +337,8 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
356
337
|
|
|
357
338
|
| Name | Type | Defined in |
|
|
358
339
|
| ------ | ------ | ------ |
|
|
359
|
-
| `cause` | `unknown` | [packages/core/src/types.ts:
|
|
360
|
-
| `tag` | `"Defect"` | [packages/core/src/types.ts:
|
|
340
|
+
| `cause` | `unknown` | [packages/core/src/types.ts:291](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L291) |
|
|
341
|
+
| `tag` | `"Defect"` | [packages/core/src/types.ts:290](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L290) |
|
|
361
342
|
|
|
362
343
|
#### Type Parameters
|
|
363
344
|
|
|
@@ -374,7 +355,7 @@ The `Defect` variant of a [Result](#result): an unmodeled failure carrying a `ca
|
|
|
374
355
|
type ErrOf<R> = R extends object ? E : never;
|
|
375
356
|
```
|
|
376
357
|
|
|
377
|
-
Defined in: [packages/core/src/types.ts:
|
|
358
|
+
Defined in: [packages/core/src/types.ts:456](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L456)
|
|
378
359
|
|
|
379
360
|
Extract the error type `E` from a `Result`.
|
|
380
361
|
|
|
@@ -392,7 +373,7 @@ Extract the error type `E` from a `Result`.
|
|
|
392
373
|
type ErrView<E, T> = ResultMethods<T, E> & object;
|
|
393
374
|
```
|
|
394
375
|
|
|
395
|
-
Defined in: [packages/core/src/types.ts:
|
|
376
|
+
Defined in: [packages/core/src/types.ts:284](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L284)
|
|
396
377
|
|
|
397
378
|
The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
398
379
|
|
|
@@ -400,8 +381,8 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
400
381
|
|
|
401
382
|
| Name | Type | Defined in |
|
|
402
383
|
| ------ | ------ | ------ |
|
|
403
|
-
| `error` | `E` | [packages/core/src/types.ts:
|
|
404
|
-
| `tag` | `"Err"` | [packages/core/src/types.ts:
|
|
384
|
+
| `error` | `E` | [packages/core/src/types.ts:286](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L286) |
|
|
385
|
+
| `tag` | `"Err"` | [packages/core/src/types.ts:285](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L285) |
|
|
405
386
|
|
|
406
387
|
#### Type Parameters
|
|
407
388
|
|
|
@@ -418,7 +399,7 @@ The `Err` variant of a [Result](#result): a modeled failure carrying an `error`.
|
|
|
418
399
|
type OkOf<R> = R extends object ? T : never;
|
|
419
400
|
```
|
|
420
401
|
|
|
421
|
-
Defined in: [packages/core/src/types.ts:
|
|
402
|
+
Defined in: [packages/core/src/types.ts:450](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L450)
|
|
422
403
|
|
|
423
404
|
Extract the success type `T` from a `Result`.
|
|
424
405
|
|
|
@@ -436,7 +417,7 @@ Extract the success type `T` from a `Result`.
|
|
|
436
417
|
type OkView<T, E> = ResultMethods<T, E> & object;
|
|
437
418
|
```
|
|
438
419
|
|
|
439
|
-
Defined in: [packages/core/src/types.ts:
|
|
420
|
+
Defined in: [packages/core/src/types.ts:279](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L279)
|
|
440
421
|
|
|
441
422
|
The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
442
423
|
|
|
@@ -444,8 +425,8 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
444
425
|
|
|
445
426
|
| Name | Type | Defined in |
|
|
446
427
|
| ------ | ------ | ------ |
|
|
447
|
-
| `tag` | `"Ok"` | [packages/core/src/types.ts:
|
|
448
|
-
| `value` | `T` | [packages/core/src/types.ts:
|
|
428
|
+
| `tag` | `"Ok"` | [packages/core/src/types.ts:280](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L280) |
|
|
429
|
+
| `value` | `T` | [packages/core/src/types.ts:281](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/types.ts#L281) |
|
|
449
430
|
|
|
450
431
|
#### Type Parameters
|
|
451
432
|
|
|
@@ -462,15 +443,14 @@ The `Ok` variant of a [Result](#result): a success carrying a `value`.
|
|
|
462
443
|
type Result<T, E> = ResultType<T, E>;
|
|
463
444
|
```
|
|
464
445
|
|
|
465
|
-
Defined in: [packages/core/src/facade.ts:
|
|
446
|
+
Defined in: [packages/core/src/facade.ts:47](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L47)
|
|
466
447
|
|
|
467
|
-
Companion object grouping the
|
|
468
|
-
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
469
|
-
[Result.Defect](#property-defect), [Result.
|
|
470
|
-
[Result.
|
|
471
|
-
[Result.
|
|
472
|
-
[Result.
|
|
473
|
-
[Result.isDefect](#property-isdefect).
|
|
448
|
+
Companion object grouping the **`Result`-producing** entry points under a
|
|
449
|
+
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
450
|
+
[Result.Defect](#property-defect), [Result.Do](#property-do), [Result.fromNullable](#property-fromnullable),
|
|
451
|
+
[Result.fromThrowable](#property-fromthrowable), [Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1),
|
|
452
|
+
[Result.isOk](#property-isok), [Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect),
|
|
453
|
+
[Result.isResult](#property-isresult).
|
|
474
454
|
|
|
475
455
|
#### Type Parameters
|
|
476
456
|
|
|
@@ -486,6 +466,10 @@ The free functions remain the primary, tree-shakeable API; importing only
|
|
|
486
466
|
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
487
467
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
488
468
|
|
|
469
|
+
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
470
|
+
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
471
|
+
return — a static lives in exactly one namespace.
|
|
472
|
+
|
|
489
473
|
#### Example
|
|
490
474
|
|
|
491
475
|
```ts
|
|
@@ -501,7 +485,7 @@ Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
|
501
485
|
type TaggedErrorConstructor<Tag> = <A>(args) => TaggedErrorInstance<Tag, A>;
|
|
502
486
|
```
|
|
503
487
|
|
|
504
|
-
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/
|
|
488
|
+
Defined in: [packages/core/src/tagged.ts:28](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L28)
|
|
505
489
|
|
|
506
490
|
The class constructor returned by [TaggedError](#taggederror). Generic in its payload:
|
|
507
491
|
apply it with an instantiation expression at the `extends` site.
|
|
@@ -535,7 +519,7 @@ When the payload is empty, the constructor takes **no** arguments (the
|
|
|
535
519
|
type TaggedErrorInstance<Tag, A> = Error & Readonly<A> & object;
|
|
536
520
|
```
|
|
537
521
|
|
|
538
|
-
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/
|
|
522
|
+
Defined in: [packages/core/src/tagged.ts:15](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L15)
|
|
539
523
|
|
|
540
524
|
The instance shape produced by a [TaggedError](#taggederror) class: an `Error` plus a
|
|
541
525
|
`_tag` discriminant and the (readonly) payload fields.
|
|
@@ -544,7 +528,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
544
528
|
|
|
545
529
|
| Name | Type | Defined in |
|
|
546
530
|
| ------ | ------ | ------ |
|
|
547
|
-
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/
|
|
531
|
+
| `_tag` | `Tag` | [packages/core/src/tagged.ts:16](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L16) |
|
|
548
532
|
|
|
549
533
|
#### Type Parameters
|
|
550
534
|
|
|
@@ -561,7 +545,7 @@ The instance shape produced by a [TaggedError](#taggederror) class: an `Error` p
|
|
|
561
545
|
type TagHandlers<T, E, R> = object & { [K in E["_tag"]]: (error: Extract<E, { _tag: K }>) => R };
|
|
562
546
|
```
|
|
563
547
|
|
|
564
|
-
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/
|
|
548
|
+
Defined in: [packages/core/src/tagged.ts:103](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L103)
|
|
565
549
|
|
|
566
550
|
The handler object [matchTags](#matchtags) requires: a branch per error tag, plus
|
|
567
551
|
`Ok` and `Defect`. Miss a tag and it will not compile — the exhaustiveness is
|
|
@@ -571,8 +555,8 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
571
555
|
|
|
572
556
|
| Name | Type | Defined in |
|
|
573
557
|
| ------ | ------ | ------ |
|
|
574
|
-
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/
|
|
575
|
-
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/
|
|
558
|
+
| `Defect()` | (`cause`) => `R` | [packages/core/src/tagged.ts:105](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L105) |
|
|
559
|
+
| `Ok()` | (`value`) => `R` | [packages/core/src/tagged.ts:104](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L104) |
|
|
576
560
|
|
|
577
561
|
#### Type Parameters
|
|
578
562
|
|
|
@@ -584,41 +568,78 @@ enforced by the type, with no `.exhaustive()` to forget.
|
|
|
584
568
|
|
|
585
569
|
## Variables
|
|
586
570
|
|
|
571
|
+
### AsyncResult
|
|
572
|
+
|
|
573
|
+
```ts
|
|
574
|
+
const AsyncResult: object;
|
|
575
|
+
```
|
|
576
|
+
|
|
577
|
+
Defined in: [packages/core/src/facade.ts:88](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L88)
|
|
578
|
+
|
|
579
|
+
Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
580
|
+
the matching namespace: [AsyncResult.fromPromise](#property-frompromise),
|
|
581
|
+
[AsyncResult.fromSafePromise](#property-fromsafepromise), [AsyncResult.all](#property-all),
|
|
582
|
+
[AsyncResult.allFromDict](#property-allfromdict).
|
|
583
|
+
|
|
584
|
+
#### Type Declaration
|
|
585
|
+
|
|
586
|
+
| Name | Type | Default value | Defined in |
|
|
587
|
+
| ------ | ------ | ------ | ------ |
|
|
588
|
+
| <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:91](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L91) |
|
|
589
|
+
| <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:92](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L92) |
|
|
590
|
+
| <a id="property-frompromise"></a> `fromPromise()` | <`T`, `R`>(`promise`, `qualify`) => `AsyncResult`<`T`, `Exclude`<`R`, [`Defect`](#defect)>> | - | [packages/core/src/facade.ts:89](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L89) |
|
|
591
|
+
| <a id="property-fromsafepromise"></a> `fromSafePromise()` | <`T`>(`promise`) => `AsyncResult`<`T`, `never`> | - | [packages/core/src/facade.ts:90](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L90) |
|
|
592
|
+
|
|
593
|
+
#### Remarks
|
|
594
|
+
|
|
595
|
+
The async sibling of [Result](#result-1). Statics are grouped by what they
|
|
596
|
+
**return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
597
|
+
here rather than on [Result](#result-1); the namespace already conveys "async", so
|
|
598
|
+
the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
599
|
+
`allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
600
|
+
[Result](#result-1), the free functions remain the primary, tree-shakeable API; the
|
|
601
|
+
value `AsyncResult` and the type [AsyncResult](#asyncresult-1) share one name.
|
|
602
|
+
|
|
603
|
+
#### Example
|
|
604
|
+
|
|
605
|
+
```ts
|
|
606
|
+
import { AsyncResult, Defect } from "unthrown";
|
|
607
|
+
const user = await AsyncResult.fromPromise(fetchUser(id), (c) => Defect(c));
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
***
|
|
611
|
+
|
|
587
612
|
### Result
|
|
588
613
|
|
|
589
614
|
```ts
|
|
590
615
|
const Result: object;
|
|
591
616
|
```
|
|
592
617
|
|
|
593
|
-
Defined in: [packages/core/src/facade.ts:
|
|
618
|
+
Defined in: [packages/core/src/facade.ts:47](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L47)
|
|
594
619
|
|
|
595
|
-
Companion object grouping the
|
|
596
|
-
discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
597
|
-
[Result.Defect](#property-defect), [Result.
|
|
598
|
-
[Result.
|
|
599
|
-
[Result.
|
|
600
|
-
[Result.
|
|
601
|
-
[Result.isDefect](#property-isdefect).
|
|
620
|
+
Companion object grouping the **`Result`-producing** entry points under a
|
|
621
|
+
single, discoverable namespace: [Result.Ok](#property-ok), [Result.Err](#property-err),
|
|
622
|
+
[Result.Defect](#property-defect), [Result.Do](#property-do), [Result.fromNullable](#property-fromnullable),
|
|
623
|
+
[Result.fromThrowable](#property-fromthrowable), [Result.all](#property-all-1), [Result.allFromDict](#property-allfromdict-1),
|
|
624
|
+
[Result.isOk](#property-isok), [Result.isErr](#property-iserr), [Result.isDefect](#property-isdefect),
|
|
625
|
+
[Result.isResult](#property-isresult).
|
|
602
626
|
|
|
603
627
|
#### Type Declaration
|
|
604
628
|
|
|
605
629
|
| Name | Type | Defined in |
|
|
606
630
|
| ------ | ------ | ------ |
|
|
607
|
-
| <a id="property-all"></a> `all()` | <`Rs`>(`results`) => `Result`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: OkOf\<Rs\[K\]\> \}>, [`ErrOf`](#errof)<`Rs`\[`number`\]>> | [packages/core/src/facade.ts:
|
|
608
|
-
| <a id="property-
|
|
609
|
-
| <a id="property-
|
|
610
|
-
| <a id="property-
|
|
611
|
-
| <a id="property-
|
|
612
|
-
| <a id="property-
|
|
613
|
-
| <a id="property-
|
|
614
|
-
| <a id="property-
|
|
615
|
-
| <a id="property-
|
|
616
|
-
| <a id="property-
|
|
617
|
-
| <a id="property-
|
|
618
|
-
| <a id="property-
|
|
619
|
-
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:57](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L57) |
|
|
620
|
-
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:56](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L56) |
|
|
621
|
-
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:44](https://github.com/btravstack/unthrown/blob/fa4c17ddc81bb2075e8f48dfcc49e5e04d951dd1/packages/core/src/facade.ts#L44) |
|
|
631
|
+
| <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:54](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L54) |
|
|
632
|
+
| <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:55](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L55) |
|
|
633
|
+
| <a id="property-defect"></a> `Defect()` | (`cause`) => [`Defect`](#defect) | [packages/core/src/facade.ts:50](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L50) |
|
|
634
|
+
| <a id="property-do"></a> `Do()` | () => `Result`<\{ \}, `never`> | [packages/core/src/facade.ts:51](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L51) |
|
|
635
|
+
| <a id="property-err"></a> `Err()` | <`E`>(`error`) => `Result`<`never`, `E`> | [packages/core/src/facade.ts:49](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L49) |
|
|
636
|
+
| <a id="property-fromnullable"></a> `fromNullable()` | <`T`, `E`>(`value`, `onAbsent`) => `Result`<`NonNullable`<`T`>, `E`> | [packages/core/src/facade.ts:52](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L52) |
|
|
637
|
+
| <a id="property-fromthrowable"></a> `fromThrowable()` | <`A`, `T`, `R`>(`fn`, `qualify`) => (...`args`) => `Result`<`T`, `Exclude`<`R`, [`Defect`](#defect)>> | [packages/core/src/facade.ts:53](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L53) |
|
|
638
|
+
| <a id="property-isdefect"></a> `isDefect()` | <`T`, `E`>(`r`) => `r is DefectView<T, E>` | [packages/core/src/facade.ts:58](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L58) |
|
|
639
|
+
| <a id="property-iserr"></a> `isErr()` | <`T`, `E`>(`r`) => `r is ErrView<E, T>` | [packages/core/src/facade.ts:57](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L57) |
|
|
640
|
+
| <a id="property-isok"></a> `isOk()` | <`T`, `E`>(`r`) => `r is OkView<T, E>` | [packages/core/src/facade.ts:56](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L56) |
|
|
641
|
+
| <a id="property-isresult"></a> `isResult()` | (`x`) => `x is Result<unknown, unknown>` | [packages/core/src/facade.ts:59](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L59) |
|
|
642
|
+
| <a id="property-ok"></a> `Ok()` | <`T`>(`value`) => `Result`<`T`, `never`> | [packages/core/src/facade.ts:48](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/facade.ts#L48) |
|
|
622
643
|
|
|
623
644
|
#### Remarks
|
|
624
645
|
|
|
@@ -627,6 +648,10 @@ The free functions remain the primary, tree-shakeable API; importing only
|
|
|
627
648
|
`{ Ok }` never pulls this object in. The value `Result` and the type
|
|
628
649
|
[Result](#result-1) share one name (the companion-object pattern).
|
|
629
650
|
|
|
651
|
+
The **async** entry points live on the sibling [AsyncResult](#asyncresult-1) companion
|
|
652
|
+
(`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
653
|
+
return — a static lives in exactly one namespace.
|
|
654
|
+
|
|
630
655
|
#### Example
|
|
631
656
|
|
|
632
657
|
```ts
|
|
@@ -642,7 +667,7 @@ Result.Ok(1).flatMap((n) => Result.Ok(n + 1)).unwrap(); // 2
|
|
|
642
667
|
function all<Rs>(results): Result<AllOk<Rs, { [K in string | number | symbol]: OkOf<Rs[K]> }>, ErrOf<Rs[number]>>;
|
|
643
668
|
```
|
|
644
669
|
|
|
645
|
-
Defined in: [packages/core/src/interop.ts:249](https://github.com/btravstack/unthrown/blob/
|
|
670
|
+
Defined in: [packages/core/src/interop.ts:249](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L249)
|
|
646
671
|
|
|
647
672
|
Collect a tuple/array of [Result](#result)s into a single `Result` of all their
|
|
648
673
|
success values.
|
|
@@ -688,7 +713,7 @@ all([Ok(1), Ok(2)] as Result<number, never>[]).unwrap(); // number[]
|
|
|
688
713
|
function allAsync<Rs>(results): AsyncResult<AllOk<Rs, { [K in string | number | symbol]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
689
714
|
```
|
|
690
715
|
|
|
691
|
-
Defined in: [packages/core/src/interop.ts:299](https://github.com/btravstack/unthrown/blob/
|
|
716
|
+
Defined in: [packages/core/src/interop.ts:299](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L299)
|
|
692
717
|
|
|
693
718
|
The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
694
719
|
[AsyncResult](#asyncresult)s into one `AsyncResult` of all their success values.
|
|
@@ -697,7 +722,7 @@ The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
|
697
722
|
|
|
698
723
|
| Type Parameter |
|
|
699
724
|
| ------ |
|
|
700
|
-
| `Rs` *extends* readonly
|
|
725
|
+
| `Rs` *extends* readonly `AsyncResult`<`unknown`, `unknown`>[] |
|
|
701
726
|
|
|
702
727
|
#### Parameters
|
|
703
728
|
|
|
@@ -707,7 +732,7 @@ The asynchronous counterpart of [all](#all): combine a tuple/array of
|
|
|
707
732
|
|
|
708
733
|
#### Returns
|
|
709
734
|
|
|
710
|
-
|
|
735
|
+
`AsyncResult`<`AllOk`<`Rs`, \{ \[K in string \| number \| symbol\]: AsyncOkOf\<Rs\[K\]\> \}>, [`AsyncErrOf`](#asyncerrof)<`Rs`\[`number`\]>>
|
|
711
736
|
|
|
712
737
|
#### Remarks
|
|
713
738
|
|
|
@@ -731,7 +756,7 @@ await allAsync([fromSafePromise(a()), fromSafePromise(b())]);
|
|
|
731
756
|
function allFromDict<R>(results): Result<{ [K in string | number | symbol]: OkOf<R[K]> }, ErrOf<R[keyof R]>>;
|
|
732
757
|
```
|
|
733
758
|
|
|
734
|
-
Defined in: [packages/core/src/interop.ts:274](https://github.com/btravstack/unthrown/blob/
|
|
759
|
+
Defined in: [packages/core/src/interop.ts:274](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L274)
|
|
735
760
|
|
|
736
761
|
Collect a **record** of [Result](#result)s into a single `Result` of a record of
|
|
737
762
|
their success values — `allFromDict({ a: Result<A, E>, b: Result<B, E> })` is
|
|
@@ -774,7 +799,7 @@ allFromDict({ id: Ok(1), name: Ok("ada") }).unwrap(); // { id: 1, name: "ada" }
|
|
|
774
799
|
function allFromDictAsync<R>(results): AsyncResult<{ [K in string | number | symbol]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
775
800
|
```
|
|
776
801
|
|
|
777
|
-
Defined in: [packages/core/src/interop.ts:327](https://github.com/btravstack/unthrown/blob/
|
|
802
|
+
Defined in: [packages/core/src/interop.ts:327](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L327)
|
|
778
803
|
|
|
779
804
|
The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
780
805
|
[AsyncResult](#asyncresult)s into one `AsyncResult` of a record of their values.
|
|
@@ -793,7 +818,7 @@ The asynchronous counterpart of [allFromDict](#allfromdict): combine a record of
|
|
|
793
818
|
|
|
794
819
|
#### Returns
|
|
795
820
|
|
|
796
|
-
|
|
821
|
+
`AsyncResult`<\{ \[K in string \| number \| symbol\]: AsyncOkOf\<R\[K\]\> \}, [`AsyncErrOf`](#asyncerrof)<`R`\[keyof `R`\]>>
|
|
797
822
|
|
|
798
823
|
#### Remarks
|
|
799
824
|
|
|
@@ -815,7 +840,7 @@ await allFromDictAsync({ a: fromSafePromise(a()), b: fromSafePromise(b()) });
|
|
|
815
840
|
function Defect(cause): Defect;
|
|
816
841
|
```
|
|
817
842
|
|
|
818
|
-
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/
|
|
843
|
+
Defined in: [packages/core/src/defect.ts:36](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/defect.ts#L36)
|
|
819
844
|
|
|
820
845
|
Wrap a cause as a [Defect](#defect-2) — the value you return from a `qualify`
|
|
821
846
|
function when a failure is **not** a modeled domain error.
|
|
@@ -851,7 +876,7 @@ function Do(): Result<{
|
|
|
851
876
|
}, never>;
|
|
852
877
|
```
|
|
853
878
|
|
|
854
|
-
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/
|
|
879
|
+
Defined in: [packages/core/src/do.ts:30](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/do.ts#L30)
|
|
855
880
|
|
|
856
881
|
Start a do-notation chain with an empty object scope, grown step by step with
|
|
857
882
|
`bind` (for `Result`-returning steps) and `let` (for pure values).
|
|
@@ -889,7 +914,7 @@ const result = Do()
|
|
|
889
914
|
function Err<E>(error): Result<never, E>;
|
|
890
915
|
```
|
|
891
916
|
|
|
892
|
-
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/
|
|
917
|
+
Defined in: [packages/core/src/constructors.ts:34](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/constructors.ts#L34)
|
|
893
918
|
|
|
894
919
|
Construct a failed [Result](#result) carrying a **modeled** error.
|
|
895
920
|
|
|
@@ -924,7 +949,7 @@ Err("not_found").unwrapErr(); // "not_found"
|
|
|
924
949
|
function fromNullable<T, E>(value, onAbsent): Result<NonNullable<T>, E>;
|
|
925
950
|
```
|
|
926
951
|
|
|
927
|
-
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/
|
|
952
|
+
Defined in: [packages/core/src/interop.ts:29](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L29)
|
|
928
953
|
|
|
929
954
|
Bridge a nullable value into a [Result](#result): absence becomes a **modeled**
|
|
930
955
|
`Err`. The sanctioned alternative to an `Option` type.
|
|
@@ -967,7 +992,7 @@ fromNullable(map.get(key), () => "missing").unwrap();
|
|
|
967
992
|
function fromPromise<T, R>(promise, qualify): AsyncResult<T, Exclude<R, Defect>>;
|
|
968
993
|
```
|
|
969
994
|
|
|
970
|
-
Defined in: [packages/core/src/interop.ts:110](https://github.com/btravstack/unthrown/blob/
|
|
995
|
+
Defined in: [packages/core/src/interop.ts:110](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L110)
|
|
971
996
|
|
|
972
997
|
Wrap a `Promise` (or a thunk producing one) as an [AsyncResult](#asyncresult), forcing
|
|
973
998
|
every rejection to be triaged.
|
|
@@ -988,7 +1013,7 @@ every rejection to be triaged.
|
|
|
988
1013
|
|
|
989
1014
|
#### Returns
|
|
990
1015
|
|
|
991
|
-
|
|
1016
|
+
`AsyncResult`<`T`, `Exclude`<`R`, [`Defect`](#defect)>>
|
|
992
1017
|
|
|
993
1018
|
#### Remarks
|
|
994
1019
|
|
|
@@ -1019,7 +1044,7 @@ const user = await fromPromise(fetchUser(id), (cause) =>
|
|
|
1019
1044
|
function fromSafePromise<T>(promise): AsyncResult<T, never>;
|
|
1020
1045
|
```
|
|
1021
1046
|
|
|
1022
|
-
Defined in: [packages/core/src/interop.ts:136](https://github.com/btravstack/unthrown/blob/
|
|
1047
|
+
Defined in: [packages/core/src/interop.ts:136](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L136)
|
|
1023
1048
|
|
|
1024
1049
|
Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
1025
1050
|
becomes a `Defect`.
|
|
@@ -1038,7 +1063,7 @@ becomes a `Defect`.
|
|
|
1038
1063
|
|
|
1039
1064
|
#### Returns
|
|
1040
1065
|
|
|
1041
|
-
|
|
1066
|
+
`AsyncResult`<`T`, `never`>
|
|
1042
1067
|
|
|
1043
1068
|
#### Remarks
|
|
1044
1069
|
|
|
@@ -1054,7 +1079,7 @@ triage. (`await`-ing still yields a `Result`; it never throws.)
|
|
|
1054
1079
|
function fromThrowable<A, T, R>(fn, qualify): (...args) => Result<T, Exclude<R, Defect>>;
|
|
1055
1080
|
```
|
|
1056
1081
|
|
|
1057
|
-
Defined in: [packages/core/src/interop.ts:66](https://github.com/btravstack/unthrown/blob/
|
|
1082
|
+
Defined in: [packages/core/src/interop.ts:66](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/interop.ts#L66)
|
|
1058
1083
|
|
|
1059
1084
|
Wrap a throwing synchronous function so it returns a [Result](#result) instead of
|
|
1060
1085
|
throwing.
|
|
@@ -1108,7 +1133,7 @@ parse("{}").unwrap();
|
|
|
1108
1133
|
function isDefect<T, E>(r): r is DefectView<T, E>;
|
|
1109
1134
|
```
|
|
1110
1135
|
|
|
1111
|
-
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/
|
|
1136
|
+
Defined in: [packages/core/src/constructors.ts:66](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/constructors.ts#L66)
|
|
1112
1137
|
|
|
1113
1138
|
Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause`.
|
|
1114
1139
|
|
|
@@ -1139,7 +1164,7 @@ Type guard: narrow a [Result](#result) to its `Defect` variant, exposing `.cause
|
|
|
1139
1164
|
function isErr<T, E>(r): r is ErrView<E, T>;
|
|
1140
1165
|
```
|
|
1141
1166
|
|
|
1142
|
-
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/
|
|
1167
|
+
Defined in: [packages/core/src/constructors.ts:58](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/constructors.ts#L58)
|
|
1143
1168
|
|
|
1144
1169
|
Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
1145
1170
|
|
|
@@ -1170,7 +1195,7 @@ Type guard: narrow a [Result](#result) to its `Err` variant, exposing `.error`.
|
|
|
1170
1195
|
function isOk<T, E>(r): r is OkView<T, E>;
|
|
1171
1196
|
```
|
|
1172
1197
|
|
|
1173
|
-
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/
|
|
1198
|
+
Defined in: [packages/core/src/constructors.ts:50](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/constructors.ts#L50)
|
|
1174
1199
|
|
|
1175
1200
|
Type guard: narrow a [Result](#result) to its `Ok` variant, exposing `.value`.
|
|
1176
1201
|
|
|
@@ -1203,6 +1228,38 @@ if (isOk(r)) r.value; // number, narrowed
|
|
|
1203
1228
|
|
|
1204
1229
|
***
|
|
1205
1230
|
|
|
1231
|
+
### isResult()
|
|
1232
|
+
|
|
1233
|
+
```ts
|
|
1234
|
+
function isResult(x): x is Result<unknown, unknown>;
|
|
1235
|
+
```
|
|
1236
|
+
|
|
1237
|
+
Defined in: [packages/core/src/core.ts:333](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/core.ts#L333)
|
|
1238
|
+
|
|
1239
|
+
Type guard: is `x` a [Result](#result) (any of `Ok` / `Err` / `Defect`)?
|
|
1240
|
+
|
|
1241
|
+
#### Parameters
|
|
1242
|
+
|
|
1243
|
+
| Parameter | Type |
|
|
1244
|
+
| ------ | ------ |
|
|
1245
|
+
| `x` | `unknown` |
|
|
1246
|
+
|
|
1247
|
+
#### Returns
|
|
1248
|
+
|
|
1249
|
+
`x is Result<unknown, unknown>`
|
|
1250
|
+
|
|
1251
|
+
`true` when `x` is a `Result` produced by this library.
|
|
1252
|
+
|
|
1253
|
+
#### Remarks
|
|
1254
|
+
|
|
1255
|
+
Unlike [isOk](#isok) / [isErr](#iserr) / [isDefect](#isdefect), which narrow a value
|
|
1256
|
+
already known to be a `Result`, this narrows from `unknown` — useful at an
|
|
1257
|
+
untyped boundary. It checks the value carries the `Result` prototype, so a
|
|
1258
|
+
look-alike plain object (`{ tag: "Ok" }`) is **not** matched. An `AsyncResult`
|
|
1259
|
+
is not a `Result` and returns `false`.
|
|
1260
|
+
|
|
1261
|
+
***
|
|
1262
|
+
|
|
1206
1263
|
### matchTags()
|
|
1207
1264
|
|
|
1208
1265
|
#### Call Signature
|
|
@@ -1211,7 +1268,7 @@ if (isOk(r)) r.value; // number, narrowed
|
|
|
1211
1268
|
function matchTags<T, E, R>(result, handlers): R;
|
|
1212
1269
|
```
|
|
1213
1270
|
|
|
1214
|
-
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/
|
|
1271
|
+
Defined in: [packages/core/src/tagged.ts:138](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L138)
|
|
1215
1272
|
|
|
1216
1273
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1217
1274
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1263,7 +1320,7 @@ matchTags(r, {
|
|
|
1263
1320
|
function matchTags<T, E, R>(result, handlers): Promise<R>;
|
|
1264
1321
|
```
|
|
1265
1322
|
|
|
1266
|
-
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/
|
|
1323
|
+
Defined in: [packages/core/src/tagged.ts:142](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L142)
|
|
1267
1324
|
|
|
1268
1325
|
Exhaustively fold a [Result](#result) (or [AsyncResult](#asyncresult)) whose error type is
|
|
1269
1326
|
a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
@@ -1280,7 +1337,7 @@ a tagged union, dispatching each error to the handler matching its `_tag`.
|
|
|
1280
1337
|
|
|
1281
1338
|
| Parameter | Type | Description |
|
|
1282
1339
|
| ------ | ------ | ------ |
|
|
1283
|
-
| `result` |
|
|
1340
|
+
| `result` | `AsyncResult`<`T`, `E`> | the result to fold. |
|
|
1284
1341
|
| `handlers` | [`TagHandlers`](#taghandlers)<`T`, `E`, `R`> | one branch per channel/tag. |
|
|
1285
1342
|
|
|
1286
1343
|
##### Returns
|
|
@@ -1317,7 +1374,7 @@ matchTags(r, {
|
|
|
1317
1374
|
function Ok<T>(value): Result<T, never>;
|
|
1318
1375
|
```
|
|
1319
1376
|
|
|
1320
|
-
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/
|
|
1377
|
+
Defined in: [packages/core/src/constructors.ts:18](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/constructors.ts#L18)
|
|
1321
1378
|
|
|
1322
1379
|
Construct a successful [Result](#result).
|
|
1323
1380
|
|
|
@@ -1352,7 +1409,7 @@ Ok(42).unwrap(); // 42
|
|
|
1352
1409
|
function TaggedError<Tag>(tag, options?): TaggedErrorConstructor<Tag>;
|
|
1353
1410
|
```
|
|
1354
1411
|
|
|
1355
|
-
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/
|
|
1412
|
+
Defined in: [packages/core/src/tagged.ts:72](https://github.com/btravstack/unthrown/blob/9964bddff12c79925119bbfc5de67104bbcfc533/packages/core/src/tagged.ts#L72)
|
|
1356
1413
|
|
|
1357
1414
|
Build a base class for a tagged error — a class extending `Error` with a
|
|
1358
1415
|
`_tag` string discriminant, in the style of Effect's `Data.TaggedError`.
|