Intersection Types
An intersection type, written A & B, describes a value that satisfies both types at once. For object types this means the result has every member of both.
Detailed Explanation
Composition beats inheritance in most JavaScript code, and intersections are how TypeScript types composition. Instead of building a tall class hierarchy so a record can have timestamps, you describe timestamps once and & them onto whatever needs them.
The name causes trouble, so it is worth pinning down. Think of a type as a set of values. A & B is the set of values belonging to both sets. A value that is both a Named and a Timestamped must carry the fields of each. So intersecting types gives you the union of their members while giving you a smaller set of values. Union types do the reverse: more values allowed, fewer members guaranteed. If & and | ever feel backwards, it is because you are counting members instead of values.
Intersections are order-independent. A & B and B & A are the same type, unlike interface C extends A, B where declaration order matters for error reporting. They also flatten, so nesting them changes nothing.
When the same property appears in both halves, the property types are themselves intersected. { id: string } & { id: number } gives id the type string & number. No value is both a string and a number, so that is never, and the whole object type becomes impossible to construct. TypeScript does not warn you at the point of declaration. The error appears later, at the line that tries to create a value, and it reads as a confusing "not assignable to type 'never'". An interface using extends catches the same conflict at the declaration instead, which is one concrete argument for interfaces when you are modelling inheritance-like relationships.
Optional members follow the same merge rule: { retries?: number } & { retries: number } requires retries, because one half insists on it.
Four places intersections earn their keep:
- adding a shared field set (audit columns, tenant IDs) to many record types
- extending a component's props with the props of the element it renders
- attaching properties to a function type, which is exactly what
Object.assignreturns - branding a primitive with a phantom property so structurally identical types stop being interchangeable
The costs are mostly ergonomic. Intersections of many large object types make hover tooltips and error messages long, because the compiler prints the whole chain rather than a single name. Deeply nested intersections are one of the common causes of slow type-checking in large projects. And because a conflicting intersection is silently uninhabitable rather than rejected, a typo in one half can produce a type nobody can satisfy and an error message pointing somewhere else entirely.
Code Examples
1. Composing record shapes from small reusable pieces.
// 1. Composing record shapes from small reusable pieces.
type Timestamps = { createdAt: Date; updatedAt: Date };
type SoftDeletable = { deletedAt: Date | null };
type Invoice = { id: string; cents: number } & Timestamps & SoftDeletable;
const invoice: Invoice = {
id: "inv_918",
cents: 4999,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null, // required: every member of every half must be present
};2. Conflicting members collapse to never, and the error surfaces late.
// 2. Conflicting members collapse to never, and the error surfaces late.
type LegacyRow = { id: number };
type ModernRow = { id: string };
type Row = LegacyRow & ModernRow; // id is number & string, which is never
const row: Row = { id: 1 };
// Error: Type 'number' is not assignable to type 'never'.
// Nothing flagged the declaration of Row itself; no value can ever satisfy it.3. Function plus properties — the type Object.assign actually produces.
// 3. Function plus properties — the type Object.assign actually produces.
type Req = { url: string };
type Middleware = ((req: Req, next: () => void) => void) & { label: string };
const withLogging: Middleware = Object.assign(
(req: Req, next: () => void) => {
console.log(req.url);
next();
},
{ label: "logging" }, // Object.assign is typed to return target & source
);
withLogging({ url: "/checkout" }, () => {});
console.log(withLogging.label); // "logging"Common Gotchas
Intersecting different primitives gives `never`
`string & number` has no possible value. The declaration compiles, and only the first attempt to produce a value fails, often in a different file.
`&` reads like "and" but grows the member list
`Cat & Dog` is not "a cat or a dog"; it is something that is both at once. When you meant "one of these", the operator you want is `|`.
Property merging is recursive
nested objects on both sides are intersected too, so `{ meta: { a: string } } & { meta: { b: number } }` requires both `a` and `b` on `meta`.
Large intersections cost compile time and readability
every assignability check walks the whole chain, and tooltips print it in full. Give a frequently used intersection a named alias so both humans and error messages have something short to refer to.