A monads library implemented in TypeScript with an object-oriented approach, designed to handle errors and side effects in a functional and elegant way.
This library provides complete, typed implementations of the most useful monads for TypeScript development:
Represents a value that can be successful (Right) or an error (Left). Ideal for error handling without exceptions.
Represents a value that may exist (Some) or not exist (None). Safely replaces the use of null and undefined.
Encapsulates operations that may throw exceptions, converting them into Success or Failure.
Represents asynchronous computations with lazy evaluation.
Encapsulates side effects in a pure way, allowing composition before execution.
Combines Either with asynchronous operations for error handling in async code.
Type Safety: Fully typed with TypeScript
Railway Oriented Programming: Supports patterns like andThen and orElse
Composition: map, flatMap, fold methods for elegant transformations
Combinators: combineWith method to combine multiple monads
No exceptions: Explicit and functional error handling
Zero dependencies: No external dependencies
npm install @leanmind/monadsimport { Either } from '@leanmind/monads';
const divide = (a: number, b: number): Either<string, number> => {
if (b === 0) {
return Either.left('Division by zero');
}
return Either.right(a / b);
};
const result = divide(10, 2)
.map(x => x * 2)
.fold({
ifRight: x => `Result: ${x}`,
ifLeft: err => `Error: ${err}`,
});TypeScript
NPM
ESM and CommonJS
Husky (Git hooks)
Semantic Release
The project includes:
Complete README with examples for each monad
Documentation for all methods
Real-world usage examples
Railway Oriented Programming guides