Intersection of primitives
Short answer
If you combine incompatible types, for example:
type Impossible = string & number;The result is the type never.
Why this happens
The & (intersection) operator means "both at the same time".
TypeScript tries to build a type that matches all the combined types at once.
stringmeans "any string"numbermeans "any number"
But a value cannot be a string and a number at the same time,
so the intersection is empty, and TypeScript infers the type never.
Example 1. A direct intersection of incompatible types
type A = string & number; // => never
let x: A;
// x = "text"; error
// x = 123; error
// x = null; errorThe type
Aequalsnever, meaning the value is impossible. TypeScript will not let you assign anything.
Example 2. An intersection of partially compatible types
Sometimes part of the properties overlaps, and TypeScript keeps only the shared fields.
type A = { id: number; name: string };
type B = { id: number; age: number };
type C = A & B;
/*
C = {
id: number; // shared field
name: string; // from A
age: number; // from B
}
*/
const person: C = { id: 1, name: "Tim", age: 25 }; // OKEverything is compatible here, TypeScript just merged the properties.
Example 3. An intersection with conflicting property types
type A = { id: number };
type B = { id: string };
type C = A & B;
// id must be both number and string -> incompatibleNow C["id"] = number & string -> never.
Therefore the whole type is:
type C = { id: never };It is impossible to create a valid value:
const obj: C = { id: 123 }; // error
const obj2: C = { id: "123" }; // errorExample 4. An intersection of union types
type A = string | number;
type B = number | boolean;
type C = A & B; // => numberHere TypeScript computes the common part of the two sets:
- A = { string, number }
- B = { number, boolean }
- Intersection = { number }
Logical comparison
| Operation type | Symbol | Logic |
|---|---|---|
| ` | ` (union) | OR |
& (intersection) | AND | Takes only what fits everything at once |
Summary
| Expression | Result | Explanation |
|---|---|---|
string & number | never | incompatible types |
{ id: number } & { id: string } | { id: never } | conflict on a field |
{ name: string } & { age: number } | { name: string; age: number } | compatible |
| `(string | number) & (number | boolean)` |
In short:
If you combine types that cannot exist at the same time, TypeScript infers
never- a type with no possible values.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.