Skip to main content

Set theory

The main idea

In TypeScript, types can be viewed as sets of values. So:

  • string is the set of all strings
  • number is the set of all numbers
  • "hello" is a subset ({ "hello" })
  • never is the empty set (contains no values)
  • unknown is 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.

javascript
type T = string | number;

In set terms:

javascript
T = StringsNumbers

T includes all strings and all numbers.

Examples:

javascript
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 union

2. Intersection (&) = The intersection of sets

An intersection is the values that belong to both types at once.

javascript
type T = A & B;

In set terms:

javascript
T = AB

T includes only the values that fit both type A and type B at the same time.


Example: string & number

javascript
StringsNumbers =

The empty set: in TypeScript that is never.

javascript
type Impossible = string & number; // never

Example: intersecting objects

javascript
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 operationMathematical analogSymbolMeaning
A | BUnion of sets"OR" - an element in at least one of the sets
A & BIntersection of sets"AND" - an element in both sets at once
neverEmpty setContains no elements
unknownUniverse (all values)ΩContains everything
extendsSubsetType A is a subtype of B
A extends B ? X : YMembership condition-If A ⊆ B, then X, otherwise Y

4. Visual analogy (Venn diagrams)

javascript
A = { string } B = { number }

Union:

javascript
_______ _______ ( ) ( ) \ / \ / \ A /___________\ B /

All elements from A or B.

Intersection:

javascript
_______ _______ ( ) ( ) \ /¯¯¯¯¯¯¯¯\ / \ A / ∩ \ B / ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Only the shared part.


5. Subsets and "extends"

extends in TypeScript literally corresponds to the concept of a "subset":

javascript
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:

javascript
type Test2 = B extends A ? true : false; // false (B ⊄ A)

6. Intersection and union on concrete values

javascript
type X = "a" | "b"; type Y = "b" | "c"; type Union = X | Y; // "a" | "b" | "c" -> ∪ type Intersection = X & Y; // "b" -> ∩
TypeMathValue
Union"a" | "b" | "c"union of all variants
Intersection"b"only the shared part

7. Intuitive comparison

ConceptUnion (|)Intersection (&)
Logical meaning"OR""AND"
Setsunionintersection
Typingthe value must be at least one of the typesthe value must satisfy all the types
Often used forvariants, states, enum-like typescombining 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

TypeScriptSet theoryMeaning
A | BA ∪ BUnion of types
A & BA ∩ BIntersection of types
neverEmpty set
unknownΩAll possible values
extendsChecking 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.