Intersection Types & keyof — TypeScript

A & B (an intersection) means the value must have every property of A and every property of B: Point = Position & Color needs x, y and color. A | B (a union)…

Re-enrolling at school needs two documents at once: a copy of a parent's ID card and your diploma. Bring only one, and you're sent home. That's different from a counter that says "bring your ID card or your student card", where one is enough.

TypeScript has both. The "or" is the union |, which you already know. The "and" is an intersection type, written with &: it combines several types into one, and the value has to satisfy all of them at once. This lesson also covers keyof, which takes the property names of a type as a union, like the list of field names on a form.

Combine two types with &

type Position = {
  x: number;
  y: number;
};

type Color = {
  color: string;
};

type Point = Position & Color; // must have x, y, and color

const point: Point = { x: 1, y: 2, color: "red" }; // passes
const noColor: Point = { x: 1, y: 2 };             // error
error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'Point'.
  Property 'color' is missing in type '{ x: number; y: number; }' but required in type 'Color'.

& versus |

A union | (which you already know) means "one of them". An intersection & means "all of them".

Object Position & Color Position | Color
{ x: 1, y: 2, color: "red" } passes passes
{ x: 1, y: 2 } error, color missing passes as Position
{ color: "red" } error, x and y missing passes as Color
{ x: 1 } error, y missing error, y missing

So { x: 1, y: 2 } is refused as Position & Color, but passes as Position | Color because it's already a complete Position.

Watch out when two types have a property with the same name and different types. In type A = { x: number } & { x: string }, x must be a number and a string at once. No value is both, so A["x"] is never, the type with no values at all.

keyof and T[K]

keyof turns a type into the union of its property names:

type Key = keyof Point;      // "x", "y", or "color"
type ColorType = Point["color"]; // string
type XType = Point["x"];         // number

const k: Key = "color"; // passes

getValue: a key that surely exists

Combine keyof with a generic and you get a function that only accepts keys that really exist:

function getValue<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

getValue({ x: 1, y: 2, color: "red" }, "color"); // type string, value "red"
getValue({ x: 5, y: 10, color: "blue" }, "x");   // type number, value 5
getValue({ x: 1, y: 2, color: "red" }, "z");     // error
error TS2345: Argument of type '"z"' is not assignable to parameter of type '"color" | "x" | "y"'.

⚠️ Misreading &, and a key that's hard-coded

TL;DR: A & B (an intersection) means the value must have every property of A and every property of B: Point = Position & Color needs x, y and color. A | B (a union) needs just one of them. The same property with clashing types becomes never. keyof T is the union of T's property names, and T[K] is the type of property K. getValue<T, K extends keyof T>(obj: T, key: K): T[K] returns obj[key] with the exact type, and refuses a key that doesn't exist, like "z".

⚡ & means "and"

Write:

type Point = Position & Color;

const point: Point = { x: 1, y: 2, color: "red" }; // passes
const noColor: Point = { x: 1, y: 2 };
Guess first: does noColor pass?

Rejected, color is missing. With the type Position | Color, the same object passes, because it's already complete as a Position.

Meaning: The intersection Position & Color must have every property of both types: x, y and color. A union | only needs one of them.

⚡ keyof: a list of property names

Write:

type Key = keyof Point;          // "x", "y", or "color"
type ColorType = Point["color"]; // string

Meaning: keyof Point is a union of its property names, so "z" is rejected. Point["color"] takes that property's type, not its value.

⚡ A key that surely exists

Write:

function getValue<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

getValue({ x: 5, y: 10, color: "blue" }, "x"); // number, 5

Meaning: K extends keyof T limits key to the object's own property names, and T[K] becomes the result's type. obj[key] reads the property that was sent. Don't write obj["color"], because that always gives the colour.

What you will learn