Why does an intersection of two 'incompatible' primitive types, like `string & number`, resolve to 'never'?
Short answer
An intersection type requires a value to satisfy ALL constituent types SIMULTANEOUSLY — since no value can be both a string AND a number at the same time (they're fundamentally incompatible primitive shapes), the compiler correctly determines that no such value could ever exist…
Advertisement
AModel answer
An intersection type requires a value to satisfy ALL constituent types SIMULTANEOUSLY — since no value can be both a string AND a number at the same time (they're fundamentally incompatible primitive shapes), the compiler correctly determines that no such value could ever exist, so the intersection resolves to the 'impossible value' type, never.
type Impossible = string & number; // never
// let x: Impossible = 'hello'; // Error: Type 'string' is not assignable to type 'never'Real-world example
Understanding a confusing 'never' type appearing unexpectedly after intersecting two incompatible generic type parameters.
Likely follow-ups
- Under what more realistic circumstances (like generic constraints) might you accidentally produce a `T & U` intersection that resolves to never?
Related topics