TypeScript for Application Developers
LESSON 1.2

Unions and narrowing

Learn key techniques and workflows in TypeScript for Application Developers.

10m
intermediate
20,968 students

Overview

A discriminated union plus a check on the discriminant is the workhorse pattern of typed application code. Narrowing is how the compiler follows your control flow.

In this lesson you will:

  • Model alternatives as a discriminated union
  • Narrow with typeof, in, and discriminant checks
  • Make a switch exhaustive with a never assertion

Pro Tip

An exhaustive switch that assigns the leftover case to never turns a future missing branch into a compile error.

Resources