Mastering Error Handling with Container Types in TypeScript
The Struggle is Real: Dealing with Null and Undefined
We’ve all been there – stuck in a never-ending battle with null and undefined values in our code. It’s a problem that’s as old as computer science itself. Phil Karlton didn’t exactly say it, but we can all agree that dealing with emptiness is hard.
The Concept of Emptiness
When we annotate a variable with a type, it can hold either a value of that type, null, or undefined. This means we must consistently apply defensive programming techniques to avoid errors like “Cannot read property ‘XYZ’ of undefined.” But even with defensive programming, things can go wrong. False negatives can occur, and APIs often use null and undefined inconsistently.
Introducing the Maybe Data Type
Wouldn’t it be nice if we could consistently handle emptiness without false negatives? That’s where the Maybe data type comes in. Also known as Option, Maybe encapsulates the idea of a value that might not be there. A Maybe value can either be Just some value or Nothing.
Using Maybe in Practice
Let’s take a look at how we can use Maybe in a real-world scenario. We’ll create a User parser for our API result, using a UserJson type to represent what we get from the server and a User type to represent our domain model.
Extracting Values from Maybe
Now that we’ve created Maybe instances, let’s see how we can use them in our logic. We’ll create an HTML representation of the list of users from the server, representing them with cards.
Maybe-fying Existing APIs
To complete our application, we need to render our user cards. We’ll create a Maybe-fied version of the getElementById function to avoid dealing with null.
The Either Data Type: Handling Errors with Elegance
Errors are an essential part of software development. Defining failure consistently is vital to making our programs easier to reason about. The Either data type abstracts away if and try-catch statements, reducing the number of intermediate variables needed to transport the final result.
Using Either in Practice
Let’s create a getUserById service that searches for a user by ID from a JSON file. We’ll use Either to keep errors under control.
Either Composition
Our getUserById function is a sequence of actions where the next depends on the outcome of the previous. Each step does something that may fail and passes the result to the next one.
Using Our getUserById Service
Our service returns a UserJson buried two levels deep, one in an Either and the other in a Maybe. Let’s extract this valuable information from our container types.
Conclusion
We’ve learned that container types like Maybe and Either provide APIs to safely operate with values. By using these types, we can write more functional, clean, and comfortable code to read and reason about.