Type Transformations Workshop (9 exercises)

Resolve an Object’s Values as Read-only Literals

Here we have a non-TypeScript object called programModeEnumMap, that maps from one kind of enum to another:

Imagine that the uppercase keys are what's coming from the backend, and the lowercase strings are what we want to use on the frontend.

Below the programModeEnumMap in the exercise code are several types, starting with GroupProgram:

From this line it looks like the type of GroupProgram would be "group", but when we hover over it we can see that it's actually just string.


Your challenge is to add an annotation to the programModeEnumMap object that ensures that resolves these values as constants with the literal values.

Reference the Everyday Types section of the TypeScript Docs for help!


[0:00] In this exercise, we're looking at this programModeEnumMap, which is an object which maps from one sort of enum to another. Not a TypeScript enum, but you might think like a backend enum, for instance. We can imagine that the uppercase one is the version that we've got on the backend. This is the one we actually want to use on the frontend, for instance.
[0:21] Now there's an issue here, which is that you'd think that GroupProgram would actually be typed. We're accessing typeof programModeEnumMap GROUP. You'd think that this would be typed as group here, as this const, but it's not. It's actually typed as a string. This is true for all of these. They're all typed to string. Actually, what we want is for them to be typed as their proper values here.

[0:46] Your job here is to work out what kind of annotation you can add to this object to make it resolve these values as constants and make sure that they're actually resolved as their literal values.