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.
chas.err(error)
Wraps an error in an Err result.
chas.okAsync(value)
Wraps a value (or Promise of a value) in a ResultAsync that resolves to Ok.
chas.errAsync(error)
Wraps an error (or Promise of an error) in a ResultAsync that resolves to Err.
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.
Maps the thrown value to your error type. When omitted, the error is typed as
unknown.chas.fromPromise(promise, mapErr?)
Wraps a Promise in a ResultAsync. Resolves to Ok on fulfillment or Err on rejection.
Maps the rejection reason to your error type. When omitted, the error is typed as
unknown.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.
.mapErr(fn)
Transforms the Err error. Ok passes through unchanged.
.andThen(fn)
Chains another Result-returning function (flatMap / monadic bind). If Ok, calls fn with the value and returns its result. Err short-circuits.
.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..tap(fn)
Runs a side effect when Ok. Returns the original result unchanged.
.tapErr(fn)
Runs a side effect when Err. Returns the original result unchanged.
.match({ ok, err })
Exhaustively handles both branches and returns a value. Unlike .map, this always produces a non-Result value.
.unwrap()
Returns the Ok value, or throws the Err error.
.unwrapOr(defaultValue)
Returns the Ok value, or defaultValue if Err.
.unwrapErr()
Returns the Err error, or throws if Ok.
.toOption()
Discards the error and converts to an Option<T>. Ok becomes Some, Err becomes None.
.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.
.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.
.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.