Type Transformations Workshop (6 exercises)

Create a Union of Strings with All Possible Permutations of Two Unions

Here we have a Sandwich type that's currently assigned to unknown

We also have a couple of union types, BreadType and Filling, that have several options as members:


Your challenge is to determine every possible sandwich we could make.

Or to put it in TypeScript terms, we want Sandwich to be a union of every permutation of the unions BreadType and Filling such that our test passes:

This exercise explores a really interesting property of unions inside of template literals.

As a hint, don't try too hard to create some kind of wild mapped type.

It may seem tricky, but the solution might surprise you!


[0:00] We're now going to examine the effect of unions inside template literals. Here, we have a sandwich type that we're trying to grab rye sandwich with cheese, rye sandwich with ham, rye sandwich with salami. We're basically taking all of these options and trying to map over them to create all of the permutations of them.
[0:19] You might think this is pretty tricky, but the solution might surprise you. I would say, don't try too hard to create some crazy map type out of this. There's interesting properties of template literals that this exercise explores. Good luck.