Exhaustiveness Checking, also known as totality, is a feature often seen in pattern-matching languages such as Haskell and OCaml. It ensures the typechecker covers all cases. TypeScript supports exhaustiveness checking in various cases. E.g., when you specify the expected return type of a function.
type Weekday = 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri';
type Day = Weekday | 'Sat' | 'Sun';
// Without a return type - this works
function getNextDay(w: Weekday) {
switch(w) {
case 'Mon':
return 'Tue';
}
}
// Error. Not all path returns a valid type
function getNextDay(w: Weekday): Day {
switch(w) {
case 'Mon':
return 'Tue';
}
}