Skip to content
Justice Tech School

Exhaustive Checks

Assigning the leftover value to never in a default case makes the compiler complain when a new union member is added but not handled.

Example
type Light = "red" | "yellow" | "green";
function next(l: Light): Light {
  switch (l) {
    case "red": return "green";
    case "green": return "yellow";
    case "yellow": return "red";
    default: { const _x: never = l; return _x; }
  }
}
console.log(next("red"));

Result

green

Exercise

Impossible value type.

const x:  = l;

Quiz Test

Why assign to never?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.