Skip to content

QuizNot attempted

Mutually exclusive props need never

Without any ?: never members, does this compile?
type SignProps =
| { value: number; isPositive: boolean }
| { value: number; isNegative: boolean }
| { value: number; zero: boolean };
const p: SignProps = { value: 1, isPositive: true, zero: true };
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Yes: for a union, a property is only "excess" if no member declares it; add isNegative?: never; zero?: never etc. to forbid mixing
  • The literal is assignable to the first member structurally, and excess-property checking against a union only flags keys that appear in none of the members, so zero slips through
  • giving each member the other flags as ?: never makes any second flag a type error, which is how you encode 'exactly one of'.
  • Not 'must match exactly one member': unions are not exclusive.
  • Not 'zero is an excess property': zero exists on a member, so it is not excess.
  • Not 'true literals would fix it': the flag type is irrelevant
  • the missing never keys are the problem.