The `typescript-result` package provides a robust, type-safe `Result` type, inspired by Rust's `Result` enum, for managing operations that can succeed or fail without relying on traditional `try-catch` blocks or throwing exceptions. It aims to transform chaotic error handling into elegant, functional code by enforcing explicit error handling at compile time. The current stable version is 3.5.2, with active development indicated by recent beta releases (e.g., v3.6.0-beta.3) that frequently introduce new features and fixes. Key differentiators include explicit `Ok` and `Err` variants, methods like `isOk()`, `isError()`, `map()`, `mapErr()`, and the recently added `match()` for basic pattern matching on error types. It targets modern Node.js environments (`>=18`) and is designed for seamless integration into TypeScript projects, improving code predictability and maintainability compared to implicit exception propagation.
npm install typescript-resultVerified import paths — ran on the pinned version, not inferred.
Demonstrates asynchronous `Result` usage, basic error checking with `isOk()`, accessing `value` or `error`, and handling different error types using the `match()` method.
Use `if (result.isOk()) { ... } else { ... }` blocks or methods like `result.mapErr(() => ...)` and `result.match().when(...)` to safely handle both success and error cases.Upgrade to `typescript-result@3.5.2` or higher to ensure that errors within error transformation callbacks are properly caught and encapsulated within the `Err` variant.
Update to `typescript-result@3.4.1` or later if you are using generators with `Result` types to ensure correct functionality. Manual iteration or workarounds were necessary on older versions.
Upgrade to `typescript-result@3.5.1` or later. If upgrading isn't immediately possible, explicit type assertions (`as Result<T, E>`) might be necessary to guide the TypeScript compiler.
Always use `if (result.isOk()) { /* access result.value */ }` or other safe methods (`map`, `andThen`, `match`) to ensure you are operating on an `Ok` variant.Ensure that the variable you are calling `unwrap()` on is indeed a `Result` instance. In TypeScript, this is usually caught at compile-time. If it's a valid `Result` but `Err`, the error will be `Error: Called unwrap() on an Err value`.
Refactor your code to explicitly handle the `Err` case using `isOk()`, `mapErr()`, `orElse()`, or `match()` before attempting to access the successful `value`. Only use `unwrap()` when you are absolutely certain the `Result` will always be `Ok` (e.g., after validation).
Switch to ES Module import syntax: `import { Result } from 'typescript-result';`. Ensure your project's `package.json` has `"type": "module"` or use a bundler that transpiles correctly.No dependency data recorded yet.