@zerothrow/core
Version:
Core ZeroThrow functionality - Rust-style Result<T,E> for TypeScript
105 lines (95 loc) • 6.16 kB
text/typescript
import { ZeroError, Result as Result$1, Ok as Ok$1, Err as Err$1, ErrorCode as ErrorCode$1, ErrorContext as ErrorContext$1, ok, wrap } from './platform/index.cjs';
/**
* Pipe multiple operations together with full type inference
*
* @example
* const result = pipe(
* parseNumber,
* validateRange,
* formatOutput
* )(input);
*/
declare function pipe$1<T extends readonly [
(arg: unknown) => unknown,
...Array<(arg: unknown) => unknown>
]>(...fns: T): (input: Parameters<T[0]>[0]) => ReturnType<T[number]>;
/**
* Collect all Results - fail if any fail, succeed with array if all succeed
* Optimized to avoid unnecessary allocations
*/
declare function collect$1<T, E extends Error = ZeroError>(results: Array<Result$1<T, E>>): Result$1<T[], E>;
/**
* Async version of collect for Promise<Result> arrays
*/
declare function collectAsync$1<T, E extends Error = ZeroError>(promises: Array<Promise<Result$1<T, E>>>): Promise<Result$1<T[], E>>;
/**
* Get the first successful Result from an array of Result-producing functions
* Evaluates lazily - stops on first success
* Returns custom error if all fail
*/
declare function firstSuccess$1<T, E extends Error = ZeroError>(attempts: Array<() => Result$1<T, E>>): Result$1<T, E>;
/**
* Core Exports - Single source of truth for all public ZeroThrow APIs
* This file defines the clean, standardized names for the ZeroThrow namespace
*/
type Result<T, E extends globalThis.Error = ZeroError> = Result$1<T, E>;
type Ok<T> = Ok$1<T>;
type Err<E extends globalThis.Error> = Err$1<E>;
type ErrorCode = ErrorCode$1;
type ErrorContext = ErrorContext$1;
interface Async<TValue, TError extends globalThis.Error = ZeroError> extends globalThis.Promise<Result<TValue, TError>> {
andThen<UValue>(fn: (value: TValue) => Result<UValue, TError> | Async<UValue, TError>): Async<UValue, TError>;
map<UValue>(fn: (value: TValue) => UValue): Async<UValue, TError>;
mapErr<FError extends globalThis.Error>(fn: (error: TError) => FError): Async<TValue, FError>;
orElse(fallback: () => Result<TValue, TError>): Async<TValue, TError>;
unwrapOr(fallback: TValue): globalThis.Promise<TValue>;
unwrapOrThrow(): globalThis.Promise<TValue>;
tap(fn: (value: TValue) => void): Async<TValue, TError>;
tapErr(fn: (error: TError) => void): Async<TValue, TError>;
finally(fn: (value?: TValue) => void): Async<TValue, TError>;
void(): Async<void, TError>;
}
declare function err(error: globalThis.Error): Result<never, globalThis.Error>;
declare function err(code: string): Result<never, ZeroError>;
declare function err(code: string, message: string): Result<never, ZeroError>;
declare function attempt<T>(fn: () => T): Result<T, ZeroError>;
declare function attempt<T>(fn: () => Promise<T>): Promise<Result<T, ZeroError>>;
declare function attempt<T>(fn: () => T, mapError: (e: unknown) => ZeroError): Result<T, ZeroError>;
declare function attempt<T>(fn: () => Promise<T>, mapError: (e: unknown) => ZeroError): Promise<Result<T, ZeroError>>;
declare function attempt<T>(operations: Array<() => T | Promise<T>>): Promise<Result<T[], ZeroError>>;
declare function attempt<T>(operations: Array<() => T | Promise<T>>, mapError: (e: unknown) => ZeroError): Promise<Result<T[], ZeroError>>;
declare function tryAsync<T>(fn: () => Promise<T>): Promise<Result<T, ZeroError>>;
declare function enhance<TValue, TError extends globalThis.Error = ZeroError>(promise: globalThis.Promise<Result<TValue, TError>>): Async<TValue, TError>;
declare function fromAsync<TValue, TError extends globalThis.Error = ZeroError>(fn: () => globalThis.Promise<Result<TValue, TError>>): Async<TValue, TError>;
declare const pipe: typeof pipe$1;
declare const collect: typeof collect$1;
declare const collectAsync: typeof collectAsync$1;
declare const firstSuccess: typeof firstSuccess$1;
declare function isResult<T = unknown, E extends globalThis.Error = ZeroError>(value: unknown): value is Result<T, E>;
declare function isOk<T = unknown>(value: unknown): value is Ok<T>;
declare function isErr<E extends globalThis.Error = ZeroError>(value: unknown): value is Err<E>;
type coreExports_Async<TValue, TError extends globalThis.Error = ZeroError> = Async<TValue, TError>;
type coreExports_Err<E extends globalThis.Error> = Err<E>;
type coreExports_ErrorCode = ErrorCode;
type coreExports_ErrorContext = ErrorContext;
type coreExports_Ok<T> = Ok<T>;
type coreExports_Result<T, E extends globalThis.Error = ZeroError> = Result<T, E>;
declare const coreExports_ZeroError: typeof ZeroError;
declare const coreExports_attempt: typeof attempt;
declare const coreExports_collect: typeof collect;
declare const coreExports_collectAsync: typeof collectAsync;
declare const coreExports_enhance: typeof enhance;
declare const coreExports_err: typeof err;
declare const coreExports_firstSuccess: typeof firstSuccess;
declare const coreExports_fromAsync: typeof fromAsync;
declare const coreExports_isErr: typeof isErr;
declare const coreExports_isOk: typeof isOk;
declare const coreExports_isResult: typeof isResult;
declare const coreExports_ok: typeof ok;
declare const coreExports_pipe: typeof pipe;
declare const coreExports_tryAsync: typeof tryAsync;
declare const coreExports_wrap: typeof wrap;
declare namespace coreExports {
export { type coreExports_Async as Async, type coreExports_Err as Err, type coreExports_ErrorCode as ErrorCode, type coreExports_ErrorContext as ErrorContext, type coreExports_Ok as Ok, type coreExports_Result as Result, coreExports_ZeroError as ZeroError, coreExports_attempt as attempt, coreExports_collect as collect, coreExports_collectAsync as collectAsync, coreExports_enhance as enhance, coreExports_err as err, coreExports_firstSuccess as firstSuccess, coreExports_fromAsync as fromAsync, coreExports_isErr as isErr, coreExports_isOk as isOk, coreExports_isResult as isResult, coreExports_ok as ok, coreExports_pipe as pipe, attempt as try, coreExports_tryAsync as tryAsync, coreExports_wrap as wrap };
}
export { type Err as E, type Ok as O, type Result as R, attempt as a, type ErrorCode as b, coreExports as c, type ErrorContext as d, err as e, tryAsync as t };