diff --git a/src/_internals/utils.ts b/src/_internals/utils.ts index 04196144..479b4789 100644 --- a/src/_internals/utils.ts +++ b/src/_internals/utils.ts @@ -21,12 +21,6 @@ export type ExtractErrAsyncTypes ? E : never } -export type InferOkTypes = R extends Result ? T : never -export type InferErrTypes = R extends Result ? E : never - -export type InferAsyncOkTypes = R extends ResultAsync ? T : never -export type InferAsyncErrTypes = R extends ResultAsync ? E : never - /** * Short circuits on the FIRST Err value that we find */ diff --git a/src/index.ts b/src/index.ts index 00dcdc8b..cb338313 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,21 @@ -export { Result, ok, Ok, err, Err, fromThrowable, safeTry } from './result' +export { + Err, + Ok, + Result, + err, + fromThrowable, + ok, + safeTry, + InferErrTypes, + InferOkTypes, +} from './result' export { ResultAsync, - okAsync, errAsync, fromAsyncThrowable, fromPromise, fromSafePromise, + okAsync, + InferAsyncErrTypes, + InferAsyncOkTypes, } from './result-async' diff --git a/src/result-async.ts b/src/result-async.ts index 6afcb581..2e54883f 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -5,6 +5,8 @@ import type { IsLiteralArray, MemberListOf, MembersToUnion, + InferErrTypes, + InferOkTypes, } from './result' import { Err, Ok, Result } from './' @@ -13,12 +15,22 @@ import { combineResultAsyncListWithAllErrors, ExtractErrAsyncTypes, ExtractOkAsyncTypes, - InferAsyncErrTypes, - InferAsyncOkTypes, - InferErrTypes, - InferOkTypes, } from './_internals/utils' +/** + * Extract the ok type from a `ResultAsync` type + * @example + * type OkType = InferAsyncOkTypes> // number + */ +export type InferAsyncOkTypes = R extends ResultAsync ? T : never + +/** + * Extract the error type from a `ResultAsync` type + * @example + * type ErrType = InferAsyncErrTypes> // string + */ +export type InferAsyncErrTypes = R extends ResultAsync ? E : never + export class ResultAsync implements PromiseLike> { private _promise: Promise> diff --git a/src/result.ts b/src/result.ts index 3faa8886..e990edce 100644 --- a/src/result.ts +++ b/src/result.ts @@ -1,15 +1,26 @@ -import { errAsync, ResultAsync } from './' +import { errAsync, ResultAsync, InferAsyncErrTypes } from './' import { createNeverThrowError, ErrorConfig } from './_internals/error' import { combineResultList, combineResultListWithAllErrors, ExtractErrTypes, ExtractOkTypes, - InferAsyncErrTypes, - InferErrTypes, - InferOkTypes, } from './_internals/utils' +/** + * Infers the ok types from a Result + * @example + * type OkType = InferOkTypes> // number + */ +export type InferOkTypes = R extends Result ? T : never + +/** + * Infers the error types from a Result + * @example + * type ErrType = InferErrTypes> // string + */ +export type InferErrTypes = R extends Result ? E : never + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Result { /**