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

Modeling Data With TypeScript (Summary)

You’ve finished Modeling Data With TypeScript. Harbor Line’s crossings started as one object for every state, and they ended as a union with one member per state that the checker keeps complete.

What You Can Do Now

  • Model data with named types, unions and literal types so impossible combinations can’t be written
  • Narrow a union safely
  • Handle null and undefined
  • Make the checker report a case that isn’t handled

From One Object to a Union per State

The old JavaScript kept every crossing in one object. A canceled crossing kept its gate, so the old board printed 11:20 Puffin to Heron Key: boarding at gate B.

You named the shapes, gave each state a literal tag, and joined them into a union:

Language: TypeScript
type Crossing =
  | ScheduledCrossing
  | BoardingCrossing
  | DelayedCrossing
  | CancelledCrossing;

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 You’ve finished Modeling Data With TypeScript. You can now model data with named types, unions and literal types so impossible combinations can’t be written.

00:10 You can narrow a union safely, handle null and undefined, and make the checker report a case that isn’t handled.

00:19 You started from Harbor Line’s old crossing, one object for every state. A canceled crossing kept its gate, so the board said boarding. You named the shapes, gave each state a literal tag, and joined them into a union.

00:36 Foot and bike tickets shared a shape. The checker ignores names and compares shapes, so a tag called kind tells them apart.

00:46 In a switch on the tag, the checker narrows each case to one member. At run time, the switch shows the canceled crossing as canceled. A search for a missing crossing gets the fallback you chose.

01:00 When crossings could sell out, you added a full state. The check listed both switches, the board’s and the ticket desk’s.

01:09 The checker reads your code first, and Node strips the types and runs it unchecked. Give each state its own member with a literal tag, and check the tag before you read a state’s data.

01:21 Put a never check in every switch on a tag. Handle a missing value where it appears, on purpose. And read each error from the top before you fix anything.

01:33 For a teammate deciding on types, there’s JavaScript vs TypeScript: When to Add Types. The path’s next course builds on the same habits.

01:44 Thanks for working through this course.

Become a Member to join the conversation.