Sub topic 8
Conflicting Members and Intersections That Become `never`
Assume you know basic TypeScript type aliases, object types, unions, intersections, and function annotations. This chapter focuses on what happens when an intersection asks one value to satisfy incompatible requirements.
An intersection requires every member
The intersection operator (&) combines types. A value of type A & B must be assignable to both A and B.
type Named = {
name: string;
};
type Timestamped = {
createdAt: Date;
};
type Article = Named & Timestamped;
const post: Article = {
name: "TypeScript types",
createdAt: new Date(),
};post has both required properties. The intersection does not mean “either shape.” That is the purpose of a union (|). It means “all of these requirements at once.”
This difference matters when the requirements disagree.
Primitive intersections can become `never`
never is the type with no possible values. It represents an impossible result.
A string cannot also be a number, so their intersection is impossible:
type Impossible = string & number;
declare const value: Impossible;
const n: never = value;Impossible reduces to never. No runtime value can satisfy both string and number.
The same idea applies to incompatible literal types:
type OppositeDirections = "north" & "south";A value cannot be both literal "north" and literal "south", so OppositeDirections is also effectively never.
A union expresses alternatives. An intersection expresses simultaneous requirements:
| Type expression | Meaning | Column |
|---|---|---|
| `string \ | number` | A value can be a string or a number |
| `string & number` | A value must be both, which is impossible | |
| `{ id: string } & { active: boolean }` | An object must have both properties |
Conflicting object properties
Object intersections can contain a property whose type becomes never:
type StringId = {
id: string;
};
type NumericId = {
id: number;
};
type ConflictingId = StringId & NumericId;
type IdType = ConflictingId["id"]; // neverThe resulting object would need an id that is both a string and a number. Since no such value exists, the id property has type never.
In practice, you cannot create a useful value of ConflictingId:
type ConflictingId = {
id: string;
} & {
id: number;
};
// Error: no value can provide an id that is both string and number.
// const item: ConflictingId = { id: "abc" };The whole object type is not always displayed as the literal type never. TypeScript may represent it as an object with an impossible property. The important behavior is the same: constructing a valid value is impossible.
Not every overlap is a conflict. Identical property types remain valid:
type HasName = { name: string };
type AlsoHasName = { name: string };
type Person = HasName & AlsoHasName;
const person: Person = { name: "Mina" };A narrower type can also satisfy a wider one:
type AnyId = { id: string | number };
type StringOnlyId = { id: string };
type SafeId = AnyId & StringOnlyId;
const safeId: SafeId = { id: "abc" };Here, the effective id type is string, because string satisfies both requirements.
Detecting impossible intersections
You can use a conditional type to test whether a type is exactly never:
type IsNever<T> = [T] extends [never] ? true : false;
type ImpossibleCheck = IsNever<string & number>; // true
type PossibleCheck = IsNever<{ id: string } & { active: boolean }>; // falseThe brackets are important. Conditional types normally distribute over unions, and wrapping T in a tuple prevents that behavior. That makes the test work correctly for never.
Why `interface extends` behaves differently
An interface extending another interface must declare compatible properties. TypeScript reports the conflict immediately:
interface BaseRecord {
id: string;
}
// Error: the id properties are incompatible.
// interface BadRecord extends BaseRecord {
// id: number;
// }An intersection type can be written even when its result is unusable:
type BadRecord = BaseRecord & {
id: number;
};Use interface extends when you want incompatible redeclarations rejected while defining the type. Use intersections when you intentionally compose types, especially when the source types are generic or assembled conditionally. In either case, check overlapping properties carefully.
Exercise
What is the effective type of the code property in { code: string } & { code: number }, and why?
Show answer
It is never, because one value would have to be both a string and a number.