This library aims to provide a robust, type-safe error handling system for TypeScript that closely mirrors Rust's error handling patterns. The goal is to make error handling more explicit and help developers write more reliable code by forcing them to handle potential errors at compile time.
- Generic
Result<T, E>
type representing either success (Ok<T>
) or failure (Err<E>
) - Type-safe error handling with compile-time checks
- No runtime overhead compared to traditional try/catch
- Full TypeScript type inference support
Ok<T, E>(value: T)
- Create a successful resultErr<T, E>(error: E)
- Create an error resultunwrap<T, E>(result: Result<T, E>)
- Get value or panicexpect<T, E>(result: Result<T, E>, message: string)
- Get value or panic with messagematch<T, E, U>(result: Result<T, E>, onOk: (value: T) => U, onErr: (error: E) => U)
- Pattern matchingmap<T, E, U>(result: Result<T, E>, fn: (value: T) => U)
- Transform success valuemapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F)
- Transform error valueandThen<T, E, U>(result: Result<T, E>, fn: (value: T) => Result<U, E>)
- Chain operations
ResultAsync<T, E>
type alias forPromise<Result<T, E>>
- Async versions of core functions:
andThenAsync
mapAsync
mapErrAsync
- Utility functions for working with promises:
toResult
- Convert Promise to ResultfromResult
- Convert Result to Promise
- Base error interface for standardization
- Common error types:
ParseError
ValidationError
NetworkError
IoError
- Error type composition utilities
- Support for custom validation types (like Rust's
Guess
) - Type-safe validation with compile-time checks
- Common validation types:
NonEmptyString
PositiveNumber
EmailAddress
Url
panic(message: string)
function for unrecoverable errors- Custom panic handler support
- Stack trace preservation
- Development vs production modes
- Set up project structure and build system
- Implement core Result type and basic functions
- Add type tests and runtime tests
- Create basic documentation
- Implement async support
- Add error type system
- Create validation type system
- Implement panic system
- Add comprehensive tests
- Write comprehensive API documentation
- Create usage examples
- Add best practices guide
- Create migration guide from try/catch
- Add TypeScript compiler plugin for better type inference
- Create ESLint rules for enforcing patterns
- Add VS Code extension for better DX
- Create integration examples with popular frameworks
rust-errors/
├── src/
│ ├── core/
│ │ ├── result.ts
│ │ ├── panic.ts
│ │ └── types.ts
│ ├── async/
│ │ ├── result-async.ts
│ │ └── utils.ts
│ ├── errors/
│ │ ├── base.ts
│ │ └── common.ts
│ ├── validation/
│ │ ├── types.ts
│ │ └── common.ts
│ └── index.ts
├── tests/
│ ├── core/
│ ├── async/
│ ├── errors/
│ └── validation/
├── examples/
│ ├── basic/
│ ├── async/
│ └── validation/
├── docs/
│ ├── api/
│ ├── guides/
│ └── best-practices/
├── package.json
├── tsconfig.json
├── .eslintrc.js
└── README.md
-
Type Safety
- Use strict TypeScript settings
- No any types except in specific utility functions
- Comprehensive type tests
-
Performance
- No unnecessary runtime overhead
- Minimal dependencies
- Efficient memory usage
-
API Design
- Consistent with Rust patterns
- Intuitive TypeScript/JavaScript usage
- Clear error messages
-
Documentation
- Comprehensive JSDoc comments
- Clear examples for each feature
- Best practices and anti-patterns
-
Testing
- 100% test coverage
- Property-based testing
- Performance benchmarks
- Create initial project structure
- Implement core Result type
- Add basic utility functions
- Create first set of tests
- Write initial documentation