Exhaustive Checks
Use never in the default branch so a new union member causes a compile error.
Example
type A = "a" | "b";
function f(x: A) {
switch (x) {
case "a": return 1;
case "b": return 2;
default: { const _n: never = x; return _n; }
}
}
console.log(f("b"));Result
2
Exercise
Impossible type.
const n: = x;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.