Giving Each State Its Own Type

The last lesson showed that only a difference in shape keeps two ticket types apart, whatever their names. Harbor Line’s crossings need that difference too. The old JavaScript gave every crossing the same object with a dozen optional properties, and that’s how a canceled crossing once showed as boarding on the departures board.

In this lesson, you’ll model a crossing’s four states so that each requires its own properties and the checker rejects a record that mixes two states. You’ll also watch the checker catch a bug in a helper an AI assistant suggested. Start with the type you’d write today for the old object.

Type the Old Crossing as It Is

Typing old JavaScript starts with the objects it already has. A crossing has an id, a boat, and a departure time, plus a status: scheduled, boarding, delayed, or canceled. A delayed crossing also has a new departure time, and a canceled one has a reason.

The natural type is a literal union for the status and optional properties for the other two. Create crossings.ts in your Harbor Line project, with the old crossing cut down to six properties:

Language: TypeScript Filename: crossings.ts
 1type Status = "scheduled" | "boarding" | "delayed" | "cancelled";
 2
 3type Crossing = {
 4  id: string;
 5  boat: string;
 6  departs: string;
 7  status: Status;
 8  newTime?: string;
 9  reason?: string;
10};

The "cancelled" literal keeps the spelling of Harbor Line’s old data, while the board’s text says canceled, and the checker holds every record and every case you handle to that spelling.

Lines 8 and 9 mark newTime and reason as optional with ?, since most crossings have neither.

One morning, the Puffin’s 11:20 crossing was delayed to 11:50, then canceled for a gale warning. The old code changed status and left newTime in place. Add the record after a blank line:

Language: TypeScript
12const hl1120: Crossing = {
13  id: "HL-1120",
14  boat: "Puffin",
15  departs: "11:20",
16  status: "cancelled",
17  newTime: "11:50",
18};

This record is canceled yet leaves at 11:50, with no reason. Before you run npm run check, predict whether the checker accepts it.

The check is clean:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

Each property is typed correctly on its own, but the type can’t say which properties go with which status. Each ? is a separate yes-or-no choice, so four statuses times two choices for newTime times two for reason make sixteen accepted combinations, where Harbor Line has four real ones. Nor does the type tell a reader that reason belongs to canceled crossings only.

To catch the Puffin’s record, a type has to tie each property to its status.

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.