Set theory
The main idea
In TypeScript, types can be viewed as sets of values. So:
stringis the set of all stringsnumberis the set of all numbers"hello"is a subset ({ "hello" })neveris the empty set (contains no values)unknownis the universe (the set of all possible values)
Against this backdrop, the | and & operators behave exactly like set operations.
1. Union (|) = The union of sets
A union combines all possible values from both sets. That is, the value can belong to at least one of the combined types.
type T = string | number;In set terms:
T = Strings ∪ NumbersT includes all strings and all numbers.
Examples:
let x: string | number;
x = "hello"; // an element of the set of strings
x = 42; // an element of the set of numbers
x = true; // not part of the union2. Intersection (&) = The intersection of sets
An intersection is the values that belong to both types at once.
type T = A & B;In set terms:
T = A ∩ BT includes only the values that fit both type A and type B at the same time.
Example: string & number
Strings ∩ Numbers = ∅The empty set: in TypeScript that is never.
type Impossible = string & number; // neverExample: intersecting objects
type A = { name: string };
type B = { age: number };
type C = A & B; // { name: string; age: number }This is an "intersection of sets of objects", where the values must satisfy both conditions, so they contain both fields.
3. Mathematical analogy
| TypeScript operation | Mathematical analog | Symbol | Meaning |
|---|---|---|---|
A | B | Union of sets | ∪ | "OR" - an element in at least one of the sets |
A & B | Intersection of sets | ∩ | "AND" - an element in both sets at once |
never | Empty set | ∅ | Contains no elements |
unknown | Universe (all values) | Ω | Contains everything |
extends | Subset | ⊆ | Type A is a subtype of B |
A extends B ? X : Y | Membership condition | - | If A ⊆ B, then X, otherwise Y |
4. Visual analogy (Venn diagrams)
A = { string }
B = { number }Union:
_______ _______
( ) ( )
\ / \ /
\ A /___________\ B /All elements from A or B.
Intersection:
_______ _______
( ) ( )
\ /¯¯¯¯¯¯¯¯\ /
\ A / ∩ \ B /
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯Only the shared part.
5. Subsets and "extends"
extends in TypeScript literally corresponds to the concept of a "subset":
type A = "a" | "b";
type B = "a" | "b" | "c";
type Test = A extends B ? true : false; // true (A ⊆ B)But not the other way around:
type Test2 = B extends A ? true : false; // false (B ⊄ A)6. Intersection and union on concrete values
type X = "a" | "b";
type Y = "b" | "c";
type Union = X | Y; // "a" | "b" | "c" -> ∪
type Intersection = X & Y; // "b" -> ∩| Type | Math | Value |
|---|---|---|
| Union | "a" | "b" | "c" | union of all variants |
| Intersection | "b" | only the shared part |
7. Intuitive comparison
| Concept | Union (|) | Intersection (&) |
|---|---|---|
| Logical meaning | "OR" | "AND" |
| Sets | union | intersection |
| Typing | the value must be at least one of the types | the value must satisfy all the types |
| Often used for | variants, states, enum-like types | combining interfaces, mixin types |
| Example | "A" | "B" | {A} & {B} |
8. Why intersecting objects is not the same as intersecting sets of values
It's important to understand:
- For primitives, intersection means a shared value.
- For objects, intersection means combining the fields, because the object must satisfy all the constraints at once.
This is logically equivalent to intersecting sets of predicates, not the values themselves.
SUMMARY
| TypeScript | Set theory | Meaning |
|---|---|---|
A | B | A ∪ B | Union of types |
A & B | A ∩ B | Intersection of types |
never | ∅ | Empty set |
unknown | Ω | All possible values |
extends | ⊆ | Checking a subtype / subset |
Simple definition:
Union (
\|) is the union of types: the value belongs to at least one set.Intersection (
&) is the intersection of types: the value must belong to all sets at once.These are exactly the same operations as union (∪) and intersection (∩) in set theory.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.