Skip to content

.tip

· one of the small things

Make switches exhaustive with never

A default branch that assigns the value to never stops compiling the moment someone adds a case you didn't handle.

switch (result.kind) {
  case "text": return renderText(result);
  case "table": return renderTable(result);
  default: {
    const _exhaustive: never = result;
    return _exhaustive;
  }
}

The terminal on this site has a dozen result kinds, and this line has caught every one I forgot.

typescript