Result
Result is a type that represents an operation that can succeed (Ok) or fail (Err), without using exceptions. Inspired by Rust's Result<T, E>.
Remarks
Provides a fluent and type-safe API for error handling, allowing you to chain operations, transform values, and handle success/failure cases explicitly.
Examples
ts
// Basic creation
const success = Result.ok(42)
const failure = Result.err(new Error('failed'))ts
// Transformation and chaining
const result = Result.ok(21)
.map(x => x * 2)
.andThen(x => Result.ok(x + 10))
.unwrap() // 52ts
// Error handling with try/catch
const parsed = Result.fromTry(() => JSON.parse('{"a":1}'))
if (parsed.isOk()) {
console.log(parsed.unwrap()) // {a: 1}
}ts
// Async/await
const user = await Result.fromPromise(
async () => fetch('/api/user').then(r => r.json())
)ts
// Combining multiple Results
const [a, b, c] = Result.all([
Result.ok(1),
Result.ok(2),
Result.ok(3)
]).unwrap() // [1, 2, 3]