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

Handling Every Case With never

In this lesson, you wrote a loudspeaker announcement for every crossing state and made the checker report a state that the switch doesn’t handle.

Key Points

  • A switch without a check stays silent. With a fifth member, DepartedCrossing, added to the Crossing union and no case for it, the check is still clean. No case matches "departed", so announcement() reaches its end, and node announcements.ts prints undefined for the Kittiwake’s 07:00 crossing.
  • Each case narrows to one member. Inside case "delayed":, hovering over crossing shows DelayedCrossing, the one member with that tag, so that line may read crossing.newTime. Each case takes the member with its tag out of play, so the default branch gets only the tags that no case lists. With four cases, crossing in const unhandled = crossing; has the type DepartedCrossing there.
  • never is the type with no values. You can store something in a never constant only when its type is never as well. When you annotate the constant, as in const unhandled: never = crossing;, the line says that no crossing should get this far. While a member has no case, the check reports it on that line: Type 'DepartedCrossing' is not assignable to type 'never'. The file still runs, because Node strips the types without checking them, so it still prints undefined for the Kittiwake.
  • An exhaustive switch handles every member. With case "departed": added, hovering over crossing in the default branch shows never, because no member is left. The checker knows that no crossing can reach the default. There, crossing has the type never, which fits the never constant, so the check is clean. Node strips : never with the other types, so the default keeps a plain assignment. The never check happens before the run, when the checker reads your code.

Final Code

Here’s announcements.ts with all five states, each with its case, and the never check in the default branch. If Harbor Line adds another state later and the switch has no case for it, the check reports it on that line instead of the loudspeaker saying undefined:

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 In the last lesson, you gave each crossing state its own type. The delay announcement handled one state and lumped the other three together. The loudspeaker needs a line for every state.

00:12 Here’s the crossing union from that lesson, and a function that builds one announcement. It switches on the status tag, with one case for each of the four states.

00:21 Each case returns its own line. With four crossings in a list, run the check, and it’s clean. Run the file, and each one gets its announcement.

00:33 Now Harbor Line wants a fifth state, for a boat that has already left. Declare a departed member type and add it to the union. Then add a departed crossing to the list, for the Kittiwake at seven o’clock.

00:46 You haven’t touched the switch. Run the check, and it’s still clean. Run the file, and the first announcement, for the Kittiwake, is undefined.

00:57 No case matched departed, so the function reached its end and returned undefined instead of a line. The checker didn’t complain, because nothing in this switch says that every state needs a case.

01:10 To make the checker notice, look at what it knows inside the switch. At the top, crossing can be any of the five members. Hover over crossing in the delayed case.

01:21 It’s DelayedCrossing, the one member with that tag, so that line may read the new time. Each case takes the member with its tag out of play. Scheduled, boarding, delayed and canceled are all handled.

01:35 Add a default branch after the last case. It only gets the tags that no case lists. Store crossing there in a constant, and hover over crossing on that line.

01:45 Its type is DepartedCrossing, the one member left over.

01:51 TypeScript has a type with no values at all, called never. You can store something in a never constant only when its type is never as well. Annotate the constant as never, so that this line tells the checker no crossing should get this far.

02:08 Run the check, and there’s an error on line 37. A DepartedCrossing doesn’t fit into a never constant. The error names the state your switch doesn’t handle, at the line that checks for leftovers.

02:23 The file still runs, though. Node strips the types without checking them, so the Kittiwake’s announcement is still undefined on the loudspeaker.

02:32 Add the missing case before the default, so a departed crossing gets its own line: the boat has left. Your turn. Pause the video and predict the type of crossing in the default branch now.

02:45 It’s never, because no member is left. The checker knows no crossing can reach the default. There, crossing’s type is never, so it fits the never constant, and the check is clean.

02:59 A switch that handles every member of a union like this is called exhaustive.

03:04 Run the file, and the Kittiwake has left. All five crossings get a line. When Node strips the types, the default keeps a plain assignment. The never check happened earlier, when the checker read your code.

03:19 Switch on the tag, and each case takes one member of the union. Put a never check in the default, and a state added later without a case is an error at that line, instead of a silent undefined on the loudspeaker.

03:32 Next, in Exercise: Model the Boat Status, you’ll model the boats’ service status and handle each one.

Become a Member to join the conversation.