Type Transformations Workshop (8 exercises)

Transform a Discriminated Union into a Union

Consider this discriminated union called Fruit:


Your challenge is to turn this discriminated union into a “regular” union.

This isn’t as challenging as it may seem!

The solution should follow this general outline:

First, use the key remapping technique to iterate over Fruit, transforming it into an intermediary object. Then turn that object into a union with help from keyof.


[0:00] [0: 00] The goal of this exercise is to take a discriminated union and turn it into a union. This seems like a really, really challenging task until you realize that, "OK, we can use the key remapping to iterate over this."
[0:12] [0: 13] There is one really nice, beautiful solution to this. You'll need to use key remapping. You'll need to use the creation of that intermediary object and then accessing it with key of. Yeah, you'll need to use all of the pieces that you've used so far to come together for this solution.

[0:31] [0: 31] Good luck.