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

Narrowing a Union Branch by Branch

In this lesson, you read the old crossing’s optional delay safely, so a crossing with no report no longer shows as undefined min late and a delay of 0 shows as on time.

Key Points

  • An optional property reads as a union with undefined. const delay = crossing.delayMinutes; has the type number | undefined, so passing it to lateLine(minutes: number) gets Argument of type 'number | undefined' is not assignable to parameter of type 'number'. The checker is right. Without a check, node delays.ts prints HL-1120: undefined min late.
  • The checker follows your checks. Inside if (delay) { ... }, hovering over delay shows number. The checker followed your check into the branch and left undefined behind. That’s narrowing, as you saw with a typeof check in Writing Union Types.
  • A truthiness check drops zero too. The checker accepts the call inside if (delay). But 0 is falsy, so a crossing whose skipper reported a delay of 0 lands in the same branch as a missing report and shows HL-0940: no report yet.
  • Check what you mean. if (delay !== undefined) lets only a missing delay fail the test, so HL-0940: 0 min late shows. A typeof delay === "number" check narrows delay to number the same way. Both tests are plain JavaScript, so they stay in the file and still run after Node strips the types.
  • An early return narrows what comes after it. After if (delay === undefined) { return ...; }, hovering over delay on the next line shows number, because undefined can’t get past the return. A second check, delay === 0, returns on time for HL-0940.

Final Code

Here’s delays.ts at the end of the lesson. The early return on delay === undefined handles the missing report first, so the checker knows delay is a number on every line after it. The delay === 0 check then turns a reported 0 into on time before the last line builds the late note:

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 exercise, you typed Harbor Line’s timetable. The old crossing objects still have an optional delay that you need to read safely. Here’s the old crossing type, cut down to two of its many properties: an id and the delay in minutes.

00:16 Each id ends in the departure time. The skipper reports each delay from the boat. The skipper of the nine forty reported 0, so it leaves on time.

00:26 The last crossing has no report yet, so its delay isn’t there at all.

00:31 For the board, delayNote builds each crossing’s line with a helper that takes a number. Hover over delay, and its type is number | undefined, as for any optional property.

00:43 Run the check, and the checker flags the call to the helper. undefined isn’t a number. The checker is right, too. Run the file anyway, and the last crossing is undefined minutes late.

00:57 In JavaScript, you’d check the delay first. Wrap the call in an if statement that tests delay, and return no report yet when the test fails. Run the check, and it’s clean.

01:11 Hover over delay inside the if, and its type is number now. The checker followed your check into the branch and left undefined behind. That’s narrowing, the same thing a typeof check did for you in the union lesson.

01:25 Your turn. Look at the three crossings in the list. Pause the video and predict what the board shows for the nine forty crossing. It shows no report yet. But that crossing’s skipper did report.

01:39 A delay of 0 means it’s on time.

01:43 The if statement tests whether delay is truthy, and 0 is falsy, like a missing value. So 0 lands in the same branch as a missing report.

01:52 The checker accepts the call inside, because the test does keep undefined out of the branch. It can’t know that 0 was meant to go in. Check what you mean instead.

02:04 Change the test so it rules out undefined and nothing else. Now only a missing delay fails it. The check is clean, and the hover inside the branch shows number, as before.

02:16 Run the file, and the nine forty now shows 0 minutes late. The typeof check from the union lesson works here too. Both tests are plain JavaScript, so they stay in the file and still run after Node strips the types.

02:33 0 minutes late reads oddly on a departures board. It should say on time.

02:39 Deal with the missing report first, so the rest of the function only sees numbers, 0 included. If delay is undefined, return no report yet right away.

02:50 Hover over delay on the next line, and its type is number again. The checker knows that undefined can’t get past that return.

02:58 Then compare delay with 0, and return on time. The last line passes delay to the helper, and the check stays clean. Run the file, and the board shows 20 minutes late, on time, and no report yet.

03:14 The checker reads each check the way JavaScript runs it. It narrows the type inside each branch and past each early return, and a hover shows you what it knows.

03:24 When 0 is real data, test against undefined, not truthiness.

03:30 Next, in the lesson Narrowing Objects, Dates and Lists, you’ll narrow unions whose members are objects, dates and lists.

Become a Member to join the conversation.