Go-inspired, type-safe error handling for TypeScript
- Go-Like Multiple Return Values: Simulate Go's multiple return value pattern with a type-safe
Resulttype - Comprehensive Error Handling: Handle errors explicitly and avoid silent failures
- Error Chaining: Track the source of errors through the call stack
npm install rsltsimport { ok, err, Result } from "rslts";
function divide(a: number, b: number): Result<number> {
if (b === 0) {
return err(new Error("Division by zero"));
}
return ok(a / b);
}
const result = divide(10, 2);
if (!result.success) {
return console.error(result.error.message);
}
console.log(result.value); // 5Result<T, E>: A type that represents either a successful value or an errorok(value): Create a successful resulterr(error): Create an error result- Exhaustive Checking: TypeScript forces you to handle both success and error cases
MIT License