
The PROBLEM With Error Handling in Typescript (and how to fix it) | Ethan Niser YouTube Video Summary
In this video, Ethan Niser explains the limitations of error handling within Typescript, noting that while Typescript empowers code, it faces challenges in ensuring type safety, particularly in error handling due to Javascript's flexible exception throwing. While exceptions in Javascript have merit for removing the need to handle errors by default, they often lead to program crashes. Ethan explores how the distinction between expected and unexpected errors impacts error management, and explores using the 'Effect' library for Typescript to solve these issue.
Detailed Summary:
Typescript's Error Handling Problem
Ethan notes that Typescript empowers our code in so many ways, but its type safety has limits. These limits are most apparent when it comes to error handling. It's really not Typescript's fault though, exceptions are just a part of JavaScript. Any code can throw any value at literally any time. Any code can throw any value at literally any time and points out exceptions have merits as they remove the need to handle errors by default, and they usually crash the entire program as soon as they occur.
Expected and Unexpected errors
Ethan explains that it really comes down to the difference between expected and unexpected errors. Expected errors are, of course, expected. We create them purposely as part of our logic and meaningfully handle them where required. He highlights this means we know what types they are because we create them.
Exception limitations with Typescript
Ethan highlights that the Typescript team has rejected any error proporsal again and again, that it comes down to the difference between expected and unexpected errors. Unexpected errors are just that, unexpected and explains it can happen at anytime for any reason and of course we're not going to know their type, it's going to be unknown. The problem is that by default Javascript conflates these two, unexpected errors and expected errors are sent through the same channel.
Addressing Error Handling
Discussing other languaes, he explains that languages such as Go or Rust have panic functions, which do a better job of identifying unexpected errors. This means that when we catch, we could get an expected error, but we also could get an unexpected error. And so, because the unexpected error is of type unknown, that any time we have some information about the expected error, the type is just going to be unknown or the type of the expected error.
Handling errors as values
Ethan then uses the divide function to explain, with a short example, of how this approach may be implemented. He uses Result types, Success, Failure and notes that the example now uses class and that that is simply the fastest way to declare both a type and value constructor at the same time.
Improving the code - Functional Approach
Ethan says these issues can be addressed if we take a kind of more functional approach. He highlights that they added two methods to both the Success and Failure classes. They're called map and flatMap. He then points out that Map is a function that allows us to operate on the inner success value in even in both cases.
Effect JS library for Error Handling
This code is from a library called effect.js, and has a ton of amazing features outside of just this beautiful compositional error handling. Effect handles expected errors and unexpected errors completely separately.