|
1 |
| -/* v8 ignore next */ |
2 | 1 | export { assertUnreachable } from "./helpers.js";
|
3 |
| -/* v8 ignore next */ |
4 |
| -export { AsyncResult, Result } from "./result.js"; |
| 2 | + |
| 3 | +import { |
| 4 | + type AsyncResult, |
| 5 | + type IfGeneratorAsync, |
| 6 | + type InferGeneratorError, |
| 7 | + type InferGeneratorReturn, |
| 8 | + Result as ResultBase, |
| 9 | +} from "./result.js"; |
| 10 | + |
| 11 | +export { AsyncResult } from "./result.js"; |
| 12 | + |
| 13 | +export namespace Result { |
| 14 | + export type Error<E> = ResultBase<never, E>; |
| 15 | + export type Ok<V> = ResultBase<V, never>; |
| 16 | + export type InferError<T> = T extends AsyncResult<any, infer Error> |
| 17 | + ? Error |
| 18 | + : T extends Result<any, infer Error> |
| 19 | + ? Error |
| 20 | + : never; |
| 21 | + export type InferValue<T> = T extends AsyncResult<infer Value, any> |
| 22 | + ? Value |
| 23 | + : T extends Result<infer Value, any> |
| 24 | + ? Value |
| 25 | + : T; |
| 26 | + export type InferResultFromGenerator<T> = T extends Generator | AsyncGenerator |
| 27 | + ? IfGeneratorAsync< |
| 28 | + T, |
| 29 | + AsyncResult<InferGeneratorReturn<T>, InferGeneratorError<T>>, |
| 30 | + Result<InferGeneratorReturn<T>, InferGeneratorError<T>> |
| 31 | + > |
| 32 | + : never; |
| 33 | +} |
| 34 | + |
| 35 | +export type Result<Value, Err> = |
| 36 | + | ([Value] extends [never] ? never : Result.Ok<Value>) |
| 37 | + | ([Err] extends [never] ? never : Result.Error<Err>); |
| 38 | + |
| 39 | +export const Result: { |
| 40 | + ok: typeof ResultBase.ok; |
| 41 | + error: typeof ResultBase.error; |
| 42 | + assertOk: typeof ResultBase.assertOk; |
| 43 | + assertError: typeof ResultBase.assertError; |
| 44 | + isResult: typeof ResultBase.isResult; |
| 45 | + isAsyncResult: typeof ResultBase.isAsyncResult; |
| 46 | + all: typeof ResultBase.all; |
| 47 | + allCatching: typeof ResultBase.allCatching; |
| 48 | + wrap: typeof ResultBase.wrap; |
| 49 | + try: typeof ResultBase.try; |
| 50 | + fromAsync: typeof ResultBase.fromAsync; |
| 51 | + fromAsyncCatching: typeof ResultBase.fromAsyncCatching; |
| 52 | + gen: typeof ResultBase.gen; |
| 53 | + genCatching: typeof ResultBase.genCatching; |
| 54 | + [Symbol.hasInstance]: (instance: unknown) => boolean; |
| 55 | +} = { |
| 56 | + ok: ResultBase.ok, |
| 57 | + error: ResultBase.error, |
| 58 | + isResult: ResultBase.isResult, |
| 59 | + isAsyncResult: ResultBase.isAsyncResult, |
| 60 | + all: ResultBase.all, |
| 61 | + allCatching: ResultBase.allCatching, |
| 62 | + wrap: ResultBase.wrap, |
| 63 | + try: ResultBase.try, |
| 64 | + fromAsync: ResultBase.fromAsync, |
| 65 | + fromAsyncCatching: ResultBase.fromAsyncCatching, |
| 66 | + gen: ResultBase.gen, |
| 67 | + genCatching: ResultBase.genCatching, |
| 68 | + assertOk: ResultBase.assertOk, |
| 69 | + assertError: ResultBase.assertError, |
| 70 | + [Symbol.hasInstance](instance: unknown) { |
| 71 | + return instance instanceof ResultBase; |
| 72 | + }, |
| 73 | +}; |
0 commit comments