Skip to content

QuizNot attempted

Where never appears

In an exhaustive switch over a discriminated union, what is the type of the value in the default branch?
type Shape = { kind: 'circle'; r: number } | { kind: 'square'; s: number };
function area(s: Shape) {
switch (s.kind) {
case 'circle': return Math.PI * s.r ** 2;
case 'square': return s.s ** 2;
default: const check = s; // type?
}
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • never
  • Each case narrows s by its kind
  • once both union members are handled nothing is left, so in default the type is never.
  • Writing const _exhaustive: never = s turns 'someone added a kind' into a compile error.
  • Not Shape: narrowing has already removed both members.
  • Not unknown: narrowing removes types, it never widens to the top type.
  • Not { kind: string }: TS doesn't invent a widened shape for unhandled cases.