Today I Learned - Iterating Through Union Types
In a previous post, we cover on how using union types in TypeScript is a great approach for domain modeling because it limits the possible values that a type can have.
For example, let's say that we're modeling a card game with a standard deck of playing cards. We could model the domain as such.
With this modeling, there's no way to create a Card
such that it has an invalid Rank
or Suite
.
With this definition, let's create a function to build the deck.
This code works, however, I don't like the fact that I had to formally list the option for both Rank
and Suite
as this means that I have two different representtions for Rank
and Suite
, which implies tthat if we needed to add a new Rank
or Suite
, then we'd need to add it in two places (a violation of DRY).
Doing some digging, I found this StackOverflow post that gave a different way of defining our Rank
and Suite
types. Let's try that new definition.
In this above code, we're saying that ranks
cannot change (either by assignment or by operations like push
). With that definition, we can say that Rank
is some entry in the ranks
array. Similar approach for our suites
array and Suite
type.
I prefer this approach much more because we have our ranks and suites defined in one place and our code reads cleaner as this says Here are the possible ranks and Rank can only be one of those choices.
Limitations
The main limitation is that it only works for "enum" style unions. Let's change example and say that we want to model a series of shapes with the following.
To use the same trick, we would need to have an array of constant values. However, we can't have a constant value for any of the Shape
s because there are an infinite number of valid Circle
s, Square
s, and Rectangle
s.