UNPKG

esresult

Version:

A tiny, TypeScript-first, result/error handling utility.

76 lines (75 loc) 3.31 kB
declare type BareTuple<T> = Omit<T, Exclude<keyof Array<unknown>, typeof Symbol.iterator>>; export declare type Result<V = void, E = never> = (V extends never ? never : Result.Value<V>) | (E extends never ? never : Result.Error<E, V>); export declare namespace Result { type Any = Result<unknown, unknown>; type OrThrown<V = void> = Result<V, Result.Error.ThrownType>; type Async<V = void, E = never> = Promise<Result<V, E>>; namespace Async { type Any = Async<unknown, unknown>; type OrThrown<V = void> = Async<V, Result.Error.ThrownType>; } /** @deprecated Use Async.Any instead. */ type AsyncAny = Async.Any; /** @deprecated Use Async.OrThrown instead. */ type AsyncOrThrown<V = void> = Async.OrThrown<V>; interface Value<V> extends BareTuple<[value: V]> { value: V | never; error: undefined; or(value: V): V; orUndefined(): V | undefined; orThrow(): V; } namespace Value { type Any = Value<unknown>; } /** @deprecated Use Value.Any instead. */ type ValueAny = Value.Any; interface Error<E, V = never> { error: { type: E extends [type: unknown, meta?: unknown] ? E["0"] : E; meta: E extends [type: unknown, meta: unknown] ? E["1"] : undefined; cause: unknown; } | never; or(value: V): never; orUndefined(): never | undefined; orThrow(): never; } namespace Error { type Any = Omit<Error<unknown>, "error"> & { error: { type: unknown; meta: unknown; cause: unknown; }; }; type Thrown<V = never> = Error<Result.Error.ThrownType, V>; type ThrownType = { thrown: unknown; }; } /** @deprecated Use Error.Any instead. */ type ErrorAny = Error.Any; /** @deprecated Use Error.Thrown instead. */ type ErrorThrown<V = never> = Error.Thrown<V>; } export declare function Result<VALUE>(value: VALUE): Result<VALUE, never>; export declare namespace Result { export var error: typeof ResultError; export var fn: <F extends (...args: any[]) => any>(fn: F) => (...args: Parameters<F>) => Wrap<ReturnType<F>>; var _a: <F extends (...args: never[]) => any>(fn: F) => Wrap<ReturnType<F>>; export { _a as try }; } declare type ResultErrorTuple<TYPE extends string | object = string | object, META extends Record<string, unknown> = Record<string, unknown>> = [type: TYPE, meta: META]; declare type ResultErrorOptions = { cause: unknown; }; declare function ResultError<ERROR extends string | number | boolean | object | ResultErrorTuple<_TYPE>, _TYPE extends string | object>(error: ERROR, options?: ResultErrorOptions): Result<never, ERROR>; declare namespace ResultError { var prototype: any; var thrown: typeof ResultErrorThrown; } declare function ResultErrorThrown<V = never>(thrown: unknown): Result.Error.Thrown<V>; declare type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N; declare type IsAny<T> = IfAny<T, true, false>; declare type Wrap<T> = IsAny<T> extends true ? Result.OrThrown<unknown> : T extends Promise<any> ? Result.Async.OrThrown<IsAny<Awaited<T>> extends true ? unknown : Awaited<T>> : Result.OrThrown<T>; export default Result;