Introduction
@praha/byethrow is a lightweight, tree-shakable library that brings the Result type to JavaScript and TypeScript.
Instead of throwing exceptions, functions return a value that is either a success or a failure.
The possibility of failure becomes part of the type, so the compiler can make sure it is handled.
At a Glance
With throw, a function's signature tells you nothing about how it can fail:
With byethrow, every expected failure appears in the return type, and results can be composed step by step without try/catch:
Features
- Tree-shakable: Every function is an independent export, so your bundle only includes what you use.
- Plain objects: A
Resultis a plain object, not a class instance. It is easy to log, serialize, and debug. - One API for sync and async: Every function accepts both
Result<T, E>andPromise<Result<T, E>>. - Composable: Build readable pipelines with
pipeand combinators such asmap,andThen, andandThrough. - Focused: A small set of Result-centric functions, without aliases or confusing variants.
- Fully type-safe: Every function is covered by type tests, so success and error types are inferred precisely.
When to Use byethrow
byethrow works best for failures that are part of your application's normal behavior, such as:
- API calls that can fail because of network or server errors
- Input validation where you want to report one or all problems to the user
- File operations that can fail because of missing files or permissions
- Parsers that must reject invalid input gracefully
- Business rules such as "the post has already been deleted" or "the user has no permission"
You don't need to wrap every possible error in a Result.
Truly unexpected errors, such as a lost database connection, can still be thrown and handled by your infrastructure.
See Result vs throw for how to draw that line.
Next Steps
- Why byethrow?: The problems byethrow solves and how it compares to other approaches
- Quick Start: Install the package and write your first
Result-based code - Tutorial: Learn every function step by step
