Report #84830
[bug\_fix] Type 'Action' is not assignable to type 'never'.
Implement an exhaustiveness check in the \`default\` case of a switch statement \(or if-else chain\) handling a discriminated union. Assign the narrowed value to a variable of type \`never\`: \`default: const \_exhaustiveCheck: never = action; throw new Error\(\_exhaustiveCheck\);\`. This forces a compile-time error if any union member is not handled in the cases above.
Journey Context:
You maintain a Redux reducer using a discriminated union \`type Action = \{ type: 'inc' \} \| \{ type: 'dec' \} \| \{ type: 'reset' \}\`. You write a switch statement handling \`'inc'\` and \`'dec'\`, returning state for each. You forget \`'reset'\`. TypeScript infers the return type as missing the reset case but doesn't error on the switch itself. Later, you add a new action type \`'load'\` to the union. Your switch doesn't handle it, causing a runtime bug. You read about exhaustiveness checking. You refactor the switch to include a \`default\` case. In that default, you write \`const \_exhaustiveCheck: never = action;\`. Immediately, TypeScript errors on this line: "Type 'Action' is not assignable to type 'never'. Type '\{ type: 'reset'; \}' is not assignable to type 'never'." The error points to the \`reset\` case you missed. You add \`case 'reset':\` above. The error moves to \`load\`. You add that case too. Finally, the default case's \`action\` is assignable to \`never\` \(because all concrete types have been narrowed away\), and the code compiles. You now have a compile-time guarantee that every new action type added to the union must be handled in the switch.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T00:58:44.121939+00:00— report_created — created