Handling Missing Values

In the last exercise, you gave each of Harbor Line’s boat statuses its own type, so you can’t write a status incorrectly. Some values might not be there at all, though. At the Port Wren check-in desk, a passenger gives a name that isn’t on the list, or a driver asks for a car deck lane that the crew hasn’t assigned yet.

In this lesson, you’ll predict which values the checker treats as possibly missing, and you’ll handle each one with a check, ?., or ??, choosing the fallback on purpose. Start with lookups, the most common source of missing values.

Handle a Lookup That Finds Nothing

Code that uses a lookup’s result needs a plan for when the search comes back empty. The check-in desk keeps the day’s bookings in a list, using JavaScript you could have written before this course. Create bookings.ts in your Harbor Line project:

Language: TypeScript Filename: bookings.ts
const bookings = [
  { kind: "foot", passenger: "Amara Osei" },
  { kind: "car", passenger: "Tom Becker", lane: "Lane 2" },
  { kind: "bike", passenger: "Priya Nair", lane: null },
];

A car or bike booking has a car deck lane, and Priya’s is null because the deck crew assigns lanes only when boarding is called. A foot booking has no lane at all. Describe these shapes for the checker with types at the top of the file:

Language: TypeScript Filename: bookings.ts
 1type FootBooking = {
 2  kind: "foot";
 3  passenger: string;
 4};
 5type VehicleBooking = {
 6  kind: "car" | "bike";
 7  passenger: string;
 8  lane: string | null;
 9};
10type Booking = FootBooking | VehicleBooking;

The kind property is the union’s tag, as status was for the crossings. Line 8 deliberately lets Priya’s lane be null. In JavaScript, undefined usually means that nothing was found or given, which is why an optional property or parameter, marked with ?, has undefined in its type.

null is a value that someone set on purpose to say “nothing here yet.” So lane is string | null instead of lane?: string, because a car booking must have a lane property, though its value may be null.

Next, the desk needs a lookup that turns a passenger’s name into their booking for the boarding slip. .find() returns the first match, or undefined when nothing matches, so the result has the type Booking | undefined. That’s strict null checking, part of the strict setting. It makes null and undefined types of their own, allowed only where a type includes them.

Handle the missing booking first, with an early return like the narrowing video’s for a missing delay. Below the types, on line 12, add : Booking[] to the bookings list you already typed. Without it, the checker only infers the list’s shape from its entries. With it, every entry must match one of the two booking types, so the checker rejects a car booking without a lane. Then add the lookup below 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

Become a Member to join the conversation.