Suggest an editImprove this articleRefine the answer for “Set theory”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)In TypeScript, **types can be viewed as sets of values**: `string` is the set of all strings, `number` is the set of all numbers, `never` is the empty set, and `unknown` is the universe (the set of all possible values). **Key point:** `union (|)` is the union of sets (∪), and `intersection (&)` is the intersection of sets (∩), exactly as in set theory.Shown above the full answer for quick recall.Answer (EN)Image## 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 = Strings ∪ Numbers ``` `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 = A ∩ B ``` `T` includes only the values that fit **both type A and type B** at the same time. --- ### Example: `string & number` ```javascript Strings ∩ Numbers = ∅ ``` 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 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) ```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" -> ∩ ``` | 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.