types as sets
A way of thinking about types as sets of possible values, so unions are combined sets and intersections are overlaps.
In more detail
string is the set of all strings, "GET" is a set with one member, unknown is the set of everything and never is the empty set. This view explains why an intersection of string and number is never, since no value is in both sets.
Related terms
- union type — A type that allows one of several listed types or values, written with a vertical bar, such as int | None in Python or "small" | "large" in TypeScript.
- intersection type — A type written A & B that requires a value to satisfy both types at once, combining their properties.
- top type — A type that every other type fits into, so it can hold any value; in TypeScript both unknown and any play this role.
- never (type) — The TypeScript type with no values, used for code that can never be reached or functions that never return.
Taught in
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Union Types, Intersections And Property Conflicts — Production TypeScript