Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Deriving a Union From a List

In this video, you made the league’s list of age groups the one place the age groups are written, and read the AgeGroup type off it, so the type follows every change to the list. Then you did the same for the match status object.

Key Points

  • A hand-written union drifts from its list. When a volunteer adds "U14" to the registration form’s list for next season but not to type AgeGroup = "U8" | "U10" | "U12", npm run check rejects the Brookside Ospreys’ sign-up. To the type, "U14" isn’t an age group. Node still runs the file and prints both lines, because the check and the run are separate steps.
  • as const keeps each item’s literal type. With it, typeof AGE_GROUPS in a type position is readonly ["U8", "U10", "U12", "U14"], a tuple of exactly the four strings that the checker treats as read-only.
  • Index the list’s type with number. A list’s items sit at number indexes, so (typeof AGE_GROUPS)[number] asks for the type of an item at any index: the union of all four strings, "U10" | "U12" | "U14" | "U8" in the checker’s own order (a union’s order doesn’t matter). The parentheses say typeof comes first, then the index. TypeScript reads typeof AGE_GROUPS[number] the same way without them.
  • The type follows the list. With type AgeGroup = (typeof AGE_GROUPS)[number], the Ospreys’ "U14" passes, because the list says so, and Kestrel Park’s "U16" gets an error whose message spells out the four groups the type allows.
  • Keep as const. Without it, the list’s type is string[], so the index gives plain string. Every string passes, and the check comes back clean for "U16".
  • An object works the same way, indexed by its keys. typeof MatchStatus reads the object, the value, and gives its type. keyof on that type gives the object’s keys, the names "Scheduled", "Played" and "Postponed". Indexing the object’s type with them, (typeof MatchStatus)[keyof typeof MatchStatus], gives its values, "scheduled", "played" and "postponed". After typeof, MatchStatus means the object.
  • The rule: write the list once, as const, and read its type off it. For a list, index its type with number. For an object, index it with its own keys.

The Code

The age groups, with the type read off the list:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your types now read their facts off other types and values. One copy is still left, and it’s the age groups. In this video, you’ll make the list of age groups the only place they’re written.

00:12 Here’s a small version of the problem, in one file. A hand-written union says which age groups exist. Below it, the registration form’s list says the same thing again, and the form prints it as its options.

00:27 Next season, the league starts an under-14 group. A volunteer adds U14 to the list, and signs up the Brookside Ospreys.

00:36 Run npm run check, and the checker rejects the Ospreys. To the type, U14 isn’t an age group. Node still runs the file and prints both lines, because the check and the run are separate steps.

00:50 The list and the type have drifted apart.

00:54 Start from the list. Add as const, as the match status object has it. It keeps each item’s literal type. Then typeof AGE_GROUPS, in a type position, reads the list’s type.

01:07 It’s a tuple of exactly these four strings, which the checker treats as read-only.

01:14 You’ve read one property’s type with brackets, as with Team’s coach. A list’s items sit at number indexes, so put the type number in the brackets. That asks for the type of an item at any index.

01:27 The answer is a union of all four strings, in the checker’s own order. A union’s order doesn’t matter. Replace the hand-written union with this line under the list.

01:37 Now AgeGroup is the item type of the list. Check again, and the Ospreys’ line passes. U14 is an age group now, because the list says so.

01:50 Try a group the league doesn’t have. Kestrel Park in U16 gets an error, and the message spells out the four groups the type allows. Back on line 1, say you delete as const and keep everything else.

02:04 Your turn: pause the video and predict whether npm run check still reports an error for Kestrel Park.

02:16 It doesn’t. The check comes back clean. Without as const, the list’s type is an array of strings, so the index gives plain string, and every string passes.

02:28 So keep as const. It’s what leaves four strings for the type to read. Kestrel Park was only a test, so delete its line.

02:38 The match status object still has its type written by hand beside it. An object’s values sit under its keys, not at number indexes. First, typeof MatchStatus reads the object, the value, and gives its type.

02:53 Then keyof, on that type, gives its keys, the three names on the left. Index the object’s type with those keys, and you get its values, the three lowercase strings.

03:07 That becomes the new type line. After typeof, MatchStatus means the object. The check is clean, and Node still prints postponed, as before. Here’s the rule: write the list once, as const, and read its type off it.

03:25 For a list, index its type with number. For an object, index it with its own keys.

03:32 Next, in Exercise: Derive the Positions and Fields, you’ll give the players’ positions and the fields you sort players by one source each, so their types can’t drift either.

Become a Member to join the conversation.