May 06, 2024



All About Errors

Errors in TypeScript aren’t great. If you live and breathe the language, you might not get what I mean, so I’ll explain how errors are handled in TypeScript and compare this to a language I think does it better.

In TypeScript much of your code can throw errors, but this is not explicit. Function return values make no reference to possible errors; there is instead an implicit understanding that one might be thrown. So you wrap the concerning code in a try block, and in the catch below you discriminate between all of the possible errors that could’ve been the cause.

In Golang this behavior is explicit. The error is returned as a value, and you have to deal with it. This is why you’ll see Go code littered with if err != nil. This granular style is preferable to TypeScript's less disciplined "remember the try/catch; then guess what happened" approach.

The Primagen has a great video discussing this, and the web dev Twitter topic-du-jour is Effect, a library trying to bring better error handling to TypeScript applications.

Experimenting with different languages and understanding how they solve problems is a great way to broaden your horizons and hit on better solutions. If you find that another language does something better, it doesn’t mean that you need to switch over immediately. There’s a lot to like about TypeScript and much to be said for having years of experience with it. Maybe the answer is instead projects like Effect that seek to improve a language by bringing the best from elsewhere.