Intersection types
Overview
Unions say “either this or that”. Intersections say “this and that”: a value of type A & B must satisfy both types at once. For object types, that means it has all the properties of both. Intersections let you build big types from small, reusable pieces.
Combining object types
type User = { id: string; name: string };
type Timestamps = { createdAt: Date; updatedAt: Date };
type SavedUser = User & Timestamps;
const u: SavedUser = {
id: "u1",
name: "Asha",
createdAt: new Date(),
updatedAt: new Date(),
};Leave anything out and TypeScript complains, just as for a single object type. Hovering over SavedUser shows User & Timestamps; the properties appear in completions as if they were declared together.
Building from pieces
Intersections encourage small, focused types that you combine as needed:
type Identified = { id: string };
type Timestamped = { createdAt: Date; updatedAt: Date };
type SoftDeletable = { deletedAt: Date | null };
type Product = Identified & Timestamped & { name: string; price: number };
type Comment = Identified & Timestamped & SoftDeletable & { text: string };Change Timestamped once, and every type that includes it updates.
A common use is adding properties to a type you get from elsewhere — a library’s props, a database row, an API response:
type ButtonProps = React.ComponentProps<"button"> & { variant: "primary" | "ghost" };
type ApiUser = UserFromDb & { links: { self: string } };Intersections in functions
Spreading objects together produces an intersection naturally:
function withTimestamps<T extends object>(value: T, now: Date): T & Timestamped {
return { ...value, createdAt: now, updatedAt: now };
}(The <T extends object> part is a generic, coming up in the next chapter.) Object.assign(a, b) is typed the same way.
Union vs intersection
The names can be confusing, because they describe sets of values, not sets of properties:
A | B (union) |
A & B (intersection) |
|
|---|---|---|
| Accepts | values of A or B | values that are A and B |
| You can use | only what both have in common (until you narrow) | everything from both |
| For object types | fewer guaranteed properties | more required properties |
A union is a bigger set of values with fewer guarantees; an intersection is a smaller set of values with more guarantees.
When intersections become never
Some intersections can’t have any values:
type Impossible = string & number; // never — nothing is both
type Weird = "a" & "b"; // neverWith object types, a conflicting property becomes impossible:
type A = { id: number };
type B = { id: string };
type C = A & B; // { id: never } — you can never create oneTypeScript doesn’t report an error when you declare C; you only find out when you try to create a value. Compare interface:
interface A { id: number }
interface B extends A { id: string }
// error TS2430: Interface 'B' incorrectly extends interface 'A'.
// Types of property 'id' are incompatible.
// Type 'string' is not assignable to type 'number'.That’s the practical difference between the two: extends checks for conflicts up front; & silently produces never. For building object hierarchies, interface … extends gives clearer errors; for combining types ad hoc — including types that aren’t plain objects — use &.
Intersections with primitives: branded types
There’s one deliberate use of intersecting a primitive with an object: branding. It gives a plain string or number a distinct type, so you can’t mix up two IDs:
type UserId = string & { readonly __brand: "UserId" };
type OrderId = string & { readonly __brand: "OrderId" };
function asUserId(id: string): UserId {
return id as UserId; // the brand exists only in the type system
}
function getUser(id: UserId) { … }
const orderId = "o-17" as OrderId;
getUser(orderId);
// error TS2345: Argument of type 'OrderId' is not assignable to parameter of type 'UserId'.At run time they’re ordinary strings; in the type system they can’t be confused. Use this sparingly, for values where a mix-up would be costly (IDs, currencies, validated emails).
Remember:
&means “has everything from both”. Use it to compose object types from small pieces — and watch for conflicts, which quietly turn intonever.
Compose a blog post
Define small reusable pieces: Identified (id: string), Timestamped (createdAt and updatedAt, both Date) and Authored (author: { name: string }). Define Draft (title, body) and build Post as Draft & Identified & Timestamped & Authored. Write publish(draft: Draft, author: string, now: Date): Post (id = the title lower-cased with spaces replaced by -) and edit(post: Post, changes: Partial<Draft>, now: Date): Post that returns a new post with updated fields and updatedAt. Print the post’s id, title and whether updatedAt changed. Show with // @ts-expect-error that a Post without author is rejected, and that type Impossible = string & number can’t hold any value.
// Reusable pieces
// Draft and Post
// publish() and edit()// Reusable pieces
type Identified = { id: string };
type Timestamped = { createdAt: Date; updatedAt: Date };
type Authored = { author: { name: string } };
// Draft and Post
type Draft = { title: string; body: string };
type Post = Draft & Identified & Timestamped & Authored;
// publish() and edit()
function publish(draft: Draft, author: string, now: Date): Post {
return {
...draft,
id: draft.title.toLowerCase().replace(/\s+/g, "-"),
createdAt: now,
updatedAt: now,
author: { name: author },
};
}
function edit(post: Post, changes: Partial<Draft>, now: Date): Post {
return { ...post, ...changes, updatedAt: now };
}
const post = publish({ title: "Hello TypeScript", body: "First post!" }, "Asha", new Date("2026-12-01"));
const edited = edit(post, { title: "Hello again, TypeScript" }, new Date("2026-12-04"));
console.log(post.id); // hello-typescript
console.log(edited.title, "by", edited.author.name); // Hello again, TypeScript by Asha
console.log(edited.updatedAt > post.updatedAt); // true
// @ts-expect-error -- author is missing
const incomplete: Post = { id: "x", title: "x", body: "", createdAt: new Date(), updatedAt: new Date() };
type Impossible = string & number; // never
// @ts-expect-error -- no value is both a string and a number
const nothing: Impossible = "text";Summary
A & Bis a type whose values have everything from A and everything from B.- Intersections compose object types from small reusable pieces, like
Post = Draft & Identified & Timestamped. - A union (
A | B) is “either”; an intersection (A & B) is “both at once”. - Intersections of incompatible types (
string & number) or conflicting properties becomenever. - For object hierarchies,
interface … extendsreports conflicts more clearly; intersections work with any type.