Loading video player…

Pinning Options With Literal Types

In this lesson, you pinned Harbor Line’s port names down so that a misspelled port can’t reach the departures board.

Key Points

  • A literal type is one exact value. A const holding "Gull Island" has the type "Gull Island", while a let with the same value has the type string, because a let can change later.
  • A union of literal types is a fixed set of options. type Port = "Port Wren" | "Gull Island" | "Heron Key" | "Tern Rock"; accepts those four names and nothing else, so the check rejects "Heron Cay" with Argument of type '"Heron Cay"' is not assignable to parameter of type 'Port'. The check only reports the typo, though. Node strips the types without checking them, so node ports.ts still runs and prints Kittiwake to Heron Cay.
  • Object properties widen. const crossing = { id: "HL-0815", to: "Heron Key" }; gives to the type string, because you can still change a property of a const object. A Crossing type, type Crossing = { id: string; to: Port };, keeps to a Port. You can still change it, but only to another Port.
  • Array items widen too, and as const pins them. Without it, the ports list is a string[]. With it, the list is readonly ["Port Wren", "Gull Island", "Heron Key", "Tern Rock"], so it can’t be changed and each item keeps its literal type. A misspelled name in the list would still fail the check. It works the same way after an object literal: each property keeps its literal type and becomes readonly, so { to: "Heron Key" } as const has the readonly property to: "Heron Key".
  • as const doesn’t freeze anything. It changes only what the checker knows. Node strips it with the other types, so Object.isFrozen(ports) prints false.

Final Code

Here’s ports.ts as the lesson leaves it. The Port union names the four ports, the Crossing type keeps a crossing’s to property a Port, and as const keeps each name in the list at its literal type, so every call to boardLine() gets a real port:

Language: TypeScript Filename: ports.ts
type Port = "Port Wren" | "Gull Island" | "Heron Key" | "Tern Rock";

type Crossing = { id: string; to: Port };

function boardLine(to: Port) {
  return `Kittiwake to ${to}`;
}

const crossing: Crossing = { id: "HL-0815", to: "Heron Key" };
console.log(boardLine(crossing.to));

const ports = ["Port Wren", "Gull Island", "Heron Key", "Tern Rock"] as const;
for (const port of ports) {
  console.log(boardLine(port));
}

Run the check, then the file. The check is clean, and the board shows the crossing’s port followed by all four ports:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

$ node ports.ts
Kittiwake to Heron Key
Kittiwake to Port Wren
Kittiwake to Gull Island
Kittiwake to Heron Key
Kittiwake to Tern Rock

00:00 In the last lesson, you wrote unions for values that really vary. A port name is always a string, but only four strings are real ports, the ones Harbor Line sails to.

00:11 Here’s the function behind the departures board. It takes the port a boat is sailing to, typed as a string. The second call misspells Heron Key as Heron Cay.

00:22 Run the check, and it’s clean. Run the file, and the board shows a port that Harbor Line never sails to. Any string gets through, typo and all.

00:34 You’ve met a type that allows only one value. Hover over a const that holds Gull Island, and its type is that exact string, in quotes. Hover over a let with the same value, and you get string instead.

00:48 A let can change later, so the checker widens it to any string.

00:53 A type that is one exact value is called a literal type. You write it the same way as the value.

01:02 One port isn’t enough, but you already know how to say “or”. Join the four literal types with the bar, and give the union a name, Port, to use anywhere a port goes.

01:14 Then change the parameter’s type from string to Port, and run the check again. This time, there’s an error on line 8. Heron Cay’s literal type isn’t one of the four, so Port doesn’t accept it.

01:28 The check only reports it, though. Run the file anyway, and Node still prints Heron Cay, because it strips the types without checking them. Fix the spelling, and the check is clean.

01:40 The board shows Heron Key.

01:44 The board’s crossings come as objects. Here’s the eight fifteen crossing, with an id and the port it sails to. The next line passes its port to the board.

01:54 Your turn. Think about why the let widened, and what you can still change on an object held in a const. Pause the video and predict whether the check accepts this call.

02:06 It doesn’t. The argument is a plain string, and Port doesn’t accept that.

02:12 Hover over crossing to see why. Both of its properties are plain strings. The const stops crossing from pointing at another object, but you can still change a property.

02:24 So the checker widens each property, like a let.

02:29 The fix is to say what a crossing is. A Crossing type gives the to property the type Port, and the annotation holds the object to that type. You can still change the port, but the type says it can only change to another Port.

02:45 The check is clean, and the board shows Heron Key.

02:51 The board also lists every port, so add a list of the four names and a loop that passes each one to the board. Run the check, and it rejects the port in the loop.

03:02 Hover over the list, and it’s an array of strings. Items in an array can change too, so they widen.

03:11 Add as const after the list. It tells the checker two things: the list can’t change, and each name keeps its literal type. A misspelled name in the list would still fail the check.

03:23 The hover now shows both, a readonly list of the four exact names. The check is clean, and the board lists all four ports. Watch out, though. As const changes only what the checker knows.

03:38 Node strips it with the other types, so the array itself isn’t locked. Object.isFrozen on the list prints false.

03:47 A literal type is one exact value, and a union of literals is a fixed set of options. A const that holds a literal keeps it. A let, an object property and an array item widen a string literal to plain string, unless a type or as const pins them.

04:05 Next, in Exercise: Type the Timetable, you’ll type Harbor Line’s ports and timetable on your own.

Become a Member to join the conversation.