Sub topic 8
Combining Types with Intersections
Assume you know basic TypeScript annotations, JavaScript objects, and how to run the TypeScript compiler. This chapter focuses on intersection types and how they compare with interface extension.
The working model
An intersection type uses the & operator to combine two or more types:
type Named = {
name: string;
};
type Identified = {
id: number;
};
type User = Named & Identified;
const user: User = {
name: "Ada",
id: 1,
};User must have every property from Named and every property from Identified. In this example, it must contain both name and id.
An intersection does not mean “one type or the other.” That is a union, written with |. An intersection means “all requirements at the same time.”
type Timestamped = {
createdAt: Date;
};
type UserRecord = User & Timestamped;
const record: UserRecord = {
name: "Ada",
id: 1,
createdAt: new Date(),
};TypeScript checks intersections structurally. The value matches because it has the required properties, regardless of the name of the object that produced it.
flowchart TD
A[Named: name] --> C[User intersection]
B[Identified: id] --> C
C --> D[User requires name and id]Intersection versus interface extension
Interfaces use extends to build a new interface from an existing one:
interface Named {
name: string;
}
interface Employee extends Named {
employeeId: number;
}
const employee: Employee = {
name: "Grace",
employeeId: 7,
};An intersection creates a type alias instead:
type EmployeeRecord = Named & {
employeeId: number;
};Both forms require name and employeeId. Use extends when you are modeling a related, named object hierarchy. Use an intersection when you are composing types that may come from different sources or when you need to combine aliases.
| Situation | Common choice |
|---|---|
| A public object contract has a clear base and derived relationship | `interface extends` |
| Several independent capabilities must be combined | Intersection with `&` |
| You need a union, tuple, or conditional type | Type alias |
| You want declaration merging | Interface |
Common errors
A property with incompatible types can make an intersection impossible to use:
type HasStringId = {
id: string;
};
type HasNumberId = {
id: number;
};
type Impossible = HasStringId & HasNumberId;
// No value can normally satisfy Impossible:
// id would need to be both string and number.The conflicting id property is effectively never, a type with no possible value. If two types describe the same property, make sure their types agree before intersecting them.
Intersections also do not automatically remove optionality. If one part requires a property, the combined type requires it:
type OptionalNote = {
note?: string;
};
type RequiredNote = {
note: string;
};
type FinalNote = OptionalNote & RequiredNote;
const item: FinalNote = { note: "Ready" };The required version wins because every FinalNote must satisfy both parts.
Choosing the right composition
Prefer an intersection when the parts represent independent concerns, such as identity, timestamps, permissions, or audit data. Prefer interface extension when the types form a stable conceptual hierarchy that readers will understand as a base and a specialized form.
Exercise
What properties must an object have to satisfy { title: string } & { done: boolean }, and what happens if the two sides both define id with incompatible types?
Show answer
It must have both a string title and a boolean done. If both sides define id with incompatible types, the intersection usually makes id impossible to provide because it must satisfy both types.