How Big Can a Union Be?
Let's explore the upper limits of union types in TypeScript.
We start with an Alphabet
type, which is, as the name suggests, a union of all the letters in the alphabet:
type Alphabet = "a" | "b" | "c" | // etc...
Using some advanced type syntax, we can create a type that is `T
Transcript
00:00 How big can a union get in TypeScript? Well, we've got this alphabet type, which is basically a massive union type of all of the letters in the alphabet. So 26 in there. And we've got a type down the bottom here, which is too big. This is using some advanced type syntax to basically create a,
00:18 what it's trying to do is it's creating a string where each of the letters in the alphabet can potentially be in each slot here. So it's like four letters long, each of the ones of the alphabet. And you might think TypeScript will probably be able to handle this. Well, if you think about it, it's like 26 here, times 26, times 26,
00:38 times 26. So 26 to the power of four. And that creates a union type that is too complex to represent. So TypeScript sometimes just sort of like goes, oh, please don't make me do this. Why are you making me do this? Because usually a better way to just represent this is just with a string. You know, you don't need to be too precise here.
00:59 And so there is an upper limit on how big a union can be in TypeScript. And you'll sometimes hit this error, but usually only when you're doing something that you probably shouldn't be doing. Anyway, I thought I'd include this because it's pretty fun.