Throwing errors with the `throw` keyword in TypeScript/Node.js can break code flow unpredictably and lacks type safety. A `Result<T>` class (adapted from Vladimir Khorikov's C# implementation) offers a cleaner alternative: factory methods return either a success value or a failure with an error message, letting the compiler enforce error handling without try-catch blocks. The class provides `isSuccess`, `isFailure`, `error`, `getValue()`, and a static `combine()` method for validating multiple Results at once. The post also covers when throwing errors is still appropriate — writing library code for unknown consumers, or handling truly unexpected failures like database connectivity issues.

9m read timeFrom khalilstemmler.com
Post cover image
Table of contents
Why throwing errors purposefully might not always be the best optionIntroducing the Result classWhen to throw errors purposefullyConclusionAdditional reading
1 Impression