Type Transformations Workshop (8 exercises)

Map Over a Union to Create an Object

We start with several routes in a union of strings called Route and an empty type helper called RoutesObject:


Your challenge is to update the RoutesObject so that it becomes an object where both the keys and the values are equal to the members of the union:

Conceptually, you’ll map over the Route union and turn its members into both the keys and values of an object. As you might guess, this is called a Mapped Type.

Reference the Mapped Type section of the TypeScript Handbook for help!


[0:00] [0: 00] We're now getting into the section of Total TypeScripts that's about object types.
[0:04] [0: 04] Here we've got a bunch of different routes in a string union. We want to create an object out of them where both the keys and the values are equal to the members of the union. That means, conceptually, we need to map over each one of these members of this union and put them in both the key and the value.

[0:23] [0: 24] This is what's called a mapped type. That's what you're going to be needing to look in the TypeScript documentation for or look at articles online to help you with. You need to be changing the route object into a map type which maps over each route and puts it in the key and the value.

[0:38] [0: 38] Good luck.