1. Concept library
  2. JavaScript & TypeScript
  3. Module 4: Narrowing and control flow

Concept

Exhaustiveness checks

With a never-typed default branch, the compiler errors when a new union member is added and a switch does not handle it. It turns "did I update every switch?" into a build error.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how a never-typed default branch turns a missed case into a build error
  2. Add an exhaustive check to a switch over a union
  3. Predict what happens when a new member is added to the union

In practice

A few lines that show Exhaustiveness checks doing its job.

TypeScript
type Status = "idle" | "loading" | "done";

function label(status: Status): string {
  switch (status) {
    case "idle": return "Ready";
    case "loading": return "Working";
    case "done": return "Finished";
    default: {
      const unreachable: never = status;   // errors if a case is missing
      return unreachable;
    }
  }
}

The common mistake

Watch for

Writing default: return "Unknown". It compiles forever, so when "failed" is added to the union the function quietly returns the wrong label and nothing tells you.

What to know first

Your roadmap puts these before Exhaustiveness checks, unless you already know them.

How it stays learned

Once a session shows you understand Exhaustiveness checks, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled
Previous in the module← Discriminated unions