function divide(a: number, b: number): Result<number, DomainError> {
if (b === 0) {
return Failure.create(new DomainError('DIVISION_BY_ZERO', 'Cannot divide by zero'));
}
return Success.create(a / b);
}
const result = divide(10, 2);
if (result.isSuccess) {
console.log(result.value); // 5
} else {
console.error(result.error.message);
}
Represents the result of an operation that can succeed or fail.
Result is a type-safe way to handle errors without throwing exceptions. It forces explicit error handling at compile time.