JS Built-ins
TypeScriptintermediate

Interface β€” Exhaustive Switch

πŸ“– Usage Example
type Dir = 'N' | 'S' | 'E' | 'W'
// switch with default assigned to never
// will cause a compile error if any member is unhandled

An exhaustive switch assigns the default to never, causing a compile error if a union member is unhandled.

Exhaustive Unions with switch

A switch over a discriminated union's type field is exhaustive when all cases are handled.

Challenge: Implement processStatus(status) for all three union variants.

processStatus({ type: 'ok', value: 'data' })           // β†’ 'OK: data'
processStatus({ type: 'error', message: 'not found' }) // β†’ 'Error: not found'
processStatus({ type: 'pending' })                     // β†’ 'Pending'
πŸ’‘ Hints (1)
  • β€’ TypeScript narrows status inside each case branch.