JS Built-ins
TypeScriptadvanced

Type Guard β€” exhaustiveCheck

πŸ“– Usage Example
function exhaustiveCheck(x: never): never {
  throw new Error('Unexpected value: ' + x)
}
// Place at the default branch to catch unhandled union members

An exhaustive check using never ensures all union members are handled at compile time.

Type Guards: Exhaustive Switch

The never type is useful for exhaustiveness checking.
If a case reaches the never branch, TypeScript will emit a compile error.

Challenge: Implement exhaustiveCheck(x: never): never β€” a utility that throws at runtime while signaling exhaustiveness to TypeScript.

// Used in a switch default:
default: throw exhaustiveCheck(shape)
πŸ’‘ Hints (1)
  • β€’ This function should always throw β€” its return type is never.