Chapter 1: Types Are Sets, and Compatibility Is Structural
Runnable examples: examples/ch01-types-are-sets.ts
The one mental model you need
A type is a set of values.
stringis the set of all strings."red"is a set with exactly one value.{ x: number }is the set of all values that have a numericxproperty (and possibly anything else).unknownis the set of every value.neveris the empty set.
“Is A assignable to B?” means “is every value of A also a value of B?”, or in set terms, “is A a subset of B?”.
type Red = "red";
type _ok = Expect<IsAssignable<Red, string>>; // {"red"} is a subset of all strings
type _no = Expect<Equal<IsAssignable<string, Red>, false>>; // but not the other way
Almost every confusing compiler error becomes readable once you ask: which set is bigger?
Structural, not nominal
In languages like Java or C#, a class is compatible with an interface only if it says implements. TypeScript checks shape:
interface Point2D { x: number; y: number }
class Vector {
readonly x: number;
readonly y: number;
constructor(x: number, y: number) { this.x = x; this.y = y; }
}
length(new Vector(3, 4)); // fine: Vector has x and y
Vector never mentions Point2D. It does not need to.
Why “more properties” means “smaller set”
{ x: number; y: number; z: number } describes fewer values than { x: number; y: number }, because each extra requirement filters values out. So the 3D type is a subtype (a subset) of the 2D type, and you can pass a 3D point where a 2D point is expected.
This is the opposite of what many people expect: the type with more properties is the smaller set.
Excess property checks: the exception you will hit daily
const point3d = { x: 1, y: 2, z: 3 };
length(point3d); // OK
length({ x: 1, y: 2, z: 3 }); // Error: Object literal may only specify known properties
Both values have the same shape, so why does only the second fail? Because TypeScript applies an extra, non-set-theoretic check to fresh object literals: if you write a literal directly where a type is expected, an unknown property is almost certainly a typo (colour instead of color). Once the object is stored in a variable, it is no longer “fresh”, and the normal structural rule applies.
Practical consequences:
- Excess property checks catch typos in config objects and options bags.
- They do not make types “exact”. Any non-literal object with extra properties is still accepted.
- If you need to reject extra keys at runtime (for example in API input), validate at runtime.
Unions and intersections are set operations
type Primary = "red" | "green" | "blue";
type Warm = "red" | "orange" | "yellow";
type Either = Primary | Warm; // union: values in either set
type Both = Primary & Warm; // intersection: values in both sets, here just "red"
For object types, intersection combines requirements:
type Person = { name: string } & { age: number }; // must have both
That is why & on objects looks like “merge”: a value in both sets must satisfy both sets of properties. And string & number is never, because no value is both.
Top, bottom and the impostor
| Type | Set | Assignable from | Assignable to |
|---|---|---|---|
unknown |
everything | everything | only unknown and any |
never |
nothing | nothing (except never) |
everything |
any |
not a set; turns checking off | everything | everything |
any breaks the set model in both directions, which is exactly why it is dangerous: it lets wrong values in and lets them flow anywhere. Chapter 10 returns to this.
Optional is not the same as “may be undefined”
With exactOptionalPropertyTypes (enabled in this repository’s tsconfig.json):
type MaybeNickname = { nickname?: string };
const a: MaybeNickname = {}; // OK: property missing
const b: MaybeNickname = { nickname: undefined }; // Error: present but undefined
Without the flag, both are allowed. The difference matters for code that uses "nickname" in obj or Object.keys, and for APIs where “not provided” and “explicitly cleared” mean different things.
Check yourself
- Is
{ a: string; b: number }assignable to{ a: string }? Which set is bigger? - Why does
let x: { a: string } = { a: "1", b: 2 }fail, while assigning the same object from another variable works? - What is
"a" & string? What is"a" | string? - Why is
neverassignable tostring?