Unions and Indexing 10 exercises

Resolve an Object’s Values as Literal Types

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

export const programModeEnumMap = {
GROUP: "group",
ANNOUNCEMENT: "announcement",
ONE_ON_ONE: "1onl",
SELF_DIRECTED: "selfDirected",
PLANNED_ONE_ON_ONE: "plannedlonl",

Loading exercise


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.