Available in: Português | Español
A lightweight, Rust-inspired Result type for Javascript and Typescript. Handle success and error cases explicitly without exceptions.
- 🦀 Rust-inspired API - Familiar
Result<T, E>pattern - 🎯 Type-safe - Full Typescript support with excellent inference
- 📦 Zero dependencies - Lightweight and focused
- 🔗 Chainable - Fluent API with
map,andThen, and more - ⚡ Tree-shakeable - Optimized bundle size
- 🛡 No exceptions - Safe error handling without try-catch
npm install @eriveltonsilva/result.js// ES6 - Recommended
import { Result } from '@eriveltonsilva/result.js'
// ES6 - Default Import
import Result from '@eriveltonsilva/result.js'
// CommonJS
const { Result } = require('@eriveltonsilva/result.js')// Create Results
const success = Result.ok(42)
const failure = Result.err(new Error('Something went wrong'))
// Check and unwrap
if (success.isOk()) {
console.log(success.unwrap()) // 42
}
// Chain operations
const doubled = Result.ok(21)
.map((x) => x * 2)
.andThen((x) => Result.ok(x + 10))
.unwrap() // 52
// Pattern matching
const result = Result.ok(42)
.match({
ok: (value) => value * 2,
err: (error) => error.message,
}) // 84
// Handle errors safely
const result = Result.fromTry(
() => JSON.parse('invalid'),
(error) => new Error(`Invalid JSON: ${error}`)
) // Error: Invalid JSON: SyntaxError: Unexpected token, "invalid" is not valid JSONFor comprehensive guides, API reference, and advanced usage patterns, see the complete documentation.
Learn more:
See CHANGELOG.md for a detailed list of changes in each release.
Contributions are welcome! Please read our Contributing Guide for details.
MIT © Erivelton Silva
Inspired by:
