Skip to content

Exhaustiveness checking — never catches the forgotten case

Level: 201 · for TypeScript users modelling data with unions

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: Once every member of a union has its case, what reaches default is never; assign it to a never variable there, and adding a triangle member later turns the forgotten case into TS2322 on that line, naming the triangle type.

Keywords: exhaustiveness checking, never

What the finished page will answer

  • What does tsc report at const forgotten: never = shape after a triangle member is added and no case handles it?
  • Why does a function with return type number report TS2366 when its switch misses a member, even without a never variable?
  • Does shape satisfies never in default work as the same check, and what does TS1360 say?
  • What should default do at run time when a value outside the type arrives anyway, for example from JSON.parse?
  • Why does a default that returns a fallback value switch the check off, like Rust's _ arm?

Examples it will need

  • exhaustiveness_never_tserror.ts — TS2322 naming the unhandled triangle type at the never assignment, and TS2366 for a second function with no default
  • exhaustiveness_runtime_fallback_ts.ts — the Error thrown by the default branch when a shape parsed from JSON has a kind the type does not list

See also

Sources to start from