Result<T, E> represents a value that is either a success (Ok<T>) or a failure (Err<E>). Every Result carries its error type in the signature — no implicit throw, no silent null. Both branches share the same set of chainable methods.
Creating Results
chas.ok(value)
Wraps a value in an Ok result.
The success value to wrap.
A result in the
Ok state containing value.chas.err(error)
Wraps an error in an Err result.
The error value to wrap.
A result in the
Err state containing error.chas.okAsync(value)
Wraps a value (or Promise of a value) in a ResultAsync that resolves to Ok.
The success value or promise to wrap.
A
ResultAsync that resolves to Ok(value).chas.errAsync(error)
Wraps an error (or Promise of an error) in a ResultAsync that resolves to Err.
The error value or promise to wrap.
A
ResultAsync that resolves to Err(error).chas.tryCatch(fn, mapErr?)
Calls a synchronous function that may throw and wraps the outcome in a Result. Returns Ok on success or Err if the function throws.
The synchronous function to call.
Maps the thrown value to your error type. When omitted, the error is typed as
unknown.Ok(returnValue) or Err(mappedError).chas.fromPromise(promise, mapErr?)
Wraps a Promise in a ResultAsync. Resolves to Ok on fulfillment or Err on rejection.
The promise to wrap.
Maps the rejection reason to your error type. When omitted, the error is typed as
unknown.A
ResultAsync wrapping the promise outcome.Instance methods
EveryResult<T, E> has the following methods regardless of whether it is Ok or Err.
.isOk()
Type guard that narrows to Ok<T>.
true if the result is Ok<T>, false otherwise. When true, TypeScript narrows the type so result.value is accessible..isErr()
Type guard that narrows to Err<E>.
true if the result is Err<E>, false otherwise. When true, TypeScript narrows the type so result.error is accessible..map(fn)
Transforms the Ok value. Err passes through unchanged.
Function applied to the
Ok value.Ok(fn(value)) or the original Err..mapErr(fn)
Transforms the Err error. Ok passes through unchanged.
Function applied to the
Err error.The original
Ok or Err(fn(error))..andThen(fn)
Chains another Result-returning function (flatMap / monadic bind). If Ok, calls fn with the value and returns its result. Err short-circuits.
Function that receives the
Ok value and returns a new Result.The result of
fn, or the original Err..orElse(fn)
Recovers from an Err by calling fn with the error and returning its result. Ok passes through unchanged.
Function that receives the
Err error and returns a recovery Result.The original
Ok, or the result of fn..tap(fn)
Runs a side effect when Ok. Returns the original result unchanged.
Side-effect function called with the
Ok value.The original result, unmodified.
.tapErr(fn)
Runs a side effect when Err. Returns the original result unchanged.
Side-effect function called with the
Err error.The original result, unmodified.
.match({ ok, err })
Exhaustively handles both branches and returns a value. Unlike .map, this always produces a non-Result value.
Handler called when the result is
Ok.Handler called when the result is
Err.The return value of whichever handler ran.
.unwrap()
Returns the Ok value, or throws the Err error.
The
Ok value..unwrapOr(defaultValue)
Returns the Ok value, or defaultValue if Err.
Fallback value returned when the result is
Err.The
Ok value or the default..unwrapErr()
Returns the Err error, or throws if Ok.
The
Err error..toOption()
Discards the error and converts to an Option<T>. Ok becomes Some, Err becomes None.
Some(value) when Ok, or None when Err..pipe(fn1, fn2, ...)
Passes the result through a sequence of functions. Each function receives the output of the previous one. Up to 9 functions are supported with full type inference.
Functions to apply in order.
The output of the last function.
.context(ctx)
Attaches a debug label or metadata object to the error when Err. Context entries are stored in error._context as an array, most recent first. No-op when Ok.
A description string or metadata object for the current step.
The same result with context prepended to
error._context (if Err)..catchTag(target, handler)
Catches a specific tagged error variant by its _tag string or error factory, calls handler with it, and removes that tag from the error union type. Unmatched tags pass through unchanged.
The
_tag string to match, or an error factory created with chas.defineErrs.Recovery function. Return a new
Result to replace the caught error.A result with the matched tag removed from the error union.
.tapTag(target, handler)
Runs a side effect for a specific tagged error, leaving the result unchanged. Use this for logging or telemetry on individual error variants.
The
_tag string to match, or an error factory.Side-effect function called when the tag matches.
The original result, unmodified.