There are some patterns in the typed functional world that are quite handy. The most prominent pattern is that of using Union and Product Types to model data to avoid bugs.

The title of this blog post is in fact lifted from a great talk given by Richard Feldman:

Now the above talk showcases a language called Elm.

Can you believe that the app built in Elm hasn't had a single runtime exception? It still blows my mind!

But thanks to TS we have similar abilities that we can leverage!

Let's see some examples with Union Types -

Assume this is an online car seller and as a dev, we modelled the Car type as:

type Car = {
	isElectric: boolean;	
	isCombustion: boolean;
	frunkSpace?: number;
	bootSpace: number;

Now let's write a function to calculate the total storage space in the Car: