Lesson 1.2

Unions and narrowing

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.

Duration
10m
Level
Intermediate
Students
21k 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

Resources

TypeScript handbook

The official reference for the tools used in this lesson.