There are instances where TypeScript will determine that two types, when intersected, do not have any compatible values. This situation of an empty intersection is known as never
, indicating that it is impossible to provide a value that satisfies both types:
type Bread = {
shape: "loafy"
};
type Car = {
shape: "carish"
};
// Contradiction1: Resolves to 'never'
type Contradiction1 = Bread & Car;
Interestingly, this behavior appears inconsistent at times. If the conflicting properties are nested within another type, TypeScript may overlook them, causing unexpected outcomes:
// Encapsulate the conflicting types in new types
type Garage = { contents: Car };
type Breadbox = { contents: Bread };
// Contradiction2: Intersection of Garage and Breadbox
// Expected Outcome: Should lead to 'never'
type Contradiction2 = Garage & Breadbox;
Is this a bug in TypeScript? The behavior raises questions about why TypeScript handles these scenarios in such a manner.