Skip to the content.

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.

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

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

  1. Is { a: string; b: number } assignable to { a: string }? Which set is bigger?
  2. Why does let x: { a: string } = { a: "1", b: 2 } fail, while assigning the same object from another variable works?
  3. What is "a" & string? What is "a" | string?
  4. Why is never assignable to string?
Answers 1. Yes. The type with more properties is the smaller set, so it is a subset of `{ a: string }`. 2. Excess property checks apply only to fresh object literals. 3. `"a" & string` is `"a"`; `"a" | string` is `string` (the union absorbs the subset). 4. The empty set is a subset of every set.

Next: Chapter 2, How TypeScript Infers Types