Installation
Install better-result and import its ESM-only TypeScript API.
Requirements
- TypeScript 5 or newer is recommended.
- The package is ESM-only.
- It has no runtime dependencies and does not require a specific server or browser runtime.
npm install better-resultpnpm add better-resultbun add better-resultyarn add better-resultImport values and types
import {
Result,
TaggedError,
matchError,
type Result as ResultType,
type InferOk,
type InferErr,
} from "better-result";
Result is both the namespace-like object that contains constructors/combinators and the name of the union type. Alias the type to ResultType when a file uses both heavily.
const findUser = (id: string): ResultType<User, UserNotFound> => {
// ...
};
Runtime support
better-result uses standard JavaScript classes, generators, async generators, AbortSignal, and ESM. Your runtime or build target must support the specific feature you use. Generator composition requires generator support; retry cancellation requires AbortController/AbortSignal.
Verify the install
import { Result } from "better-result";
const answer = Result.ok(42);
if (answer.status === "ok") {
console.log(answer.value);
}
Run your normal type-checker. No plugin, transform, or global type declaration is required.