Type Transformations Workshop (8 exercises)

Map an Object to a Union of Tuples

We start with a Values interface:

The goal is to create a union of tuples containing the key/value pairs:

We are already halfway there with this ValuesAsUnionOfTuples type:

In the above code, the keys of Values are mapped over. From there, a tuple is created that includes the key along with its value that is retrieved with help from an indexed access type.

The problem is that this creates an object instead of the tuple that we want it to create:


Your challenge is to update the ValueAsUnionOfTuples type so that it actually creates a union.


[0:00] [0: 00] In this exercise, what we're trying to get to is we have Values here and we're trying to end up with values as a union of tuples. We're trying to grab email string, firstName string, or lastName string so this ends up as a union type.
[0:16] [0: 17] What I've done is I've got you half of the way there. What we're doing is we're creating an object out of this, and we're saying K in keyof Values is...The syntax is horrible because we've got square brackets on one side, square brackets on the other, but this one creates a tuple here.

[0:34] [0: 34] We're grabbing the key as the first one and then Values K as the second. What we get is we get email is "email", string, firstName, "firstName", string, lastName, "lastName", string.

[0:44] [0: 45] Your challenge is to work out how we can take this and turn it into a union here.