Sub topic 9
Intersections or Interface Extends
Assume you know basic TypeScript type aliases, interfaces, object types, and function annotations. This chapter compares two ways to build a larger object type from smaller object types: intersection types with &, and interface inheritance with extends.
The same shape in two forms
An intersection type combines every requirement from its operands. A value of type Person & Employee must satisfy both Person and Employee.
An interface can inherit members from another interface with extends. The resulting interface contains the inherited members and its own members.
type Person = {
name: string;
};
type Audited = {
createdAt: Date;
};
type AuditedPerson = Person & Audited;
interface Employee extends Person {
employeeId: string;
}
const articleAuthor: AuditedPerson = {
name: "Mina",
createdAt: new Date()
};
const employee: Employee = {
name: "Mina",
employeeId: "E-104"
};
function printPerson(person: Person): string {
return person.name;
}
console.log(printPerson(articleAuthor));
console.log(printPerson(employee));Both AuditedPerson and Employee describe objects with combined members. The syntax differs because the mechanisms solve slightly different design problems.
| Feature | Intersection with `&` | Interface `extends` |
|---|---|---|
| Combines existing types | Yes | Mostly interfaces and object-like types |
| Can express a union of combinations | Yes | No, an interface cannot extend a union |
| Supports declaration merging | No | Yes |
| Often useful for computed or generic types | Yes | Less flexible |
| Reports incompatible inherited members | When the type is used | When the interface is declared |
Choose `extends` for named object designs
Use extends when you are designing a clear hierarchy of related object contracts. The child interface communicates an intentional relationship.
interface User {
id: string;
displayName: string;
}
interface Moderator extends User {
canDeletePosts: boolean;
}Moderator is a named kind of User. A function accepting User can accept a Moderator because the child includes all required members of the parent.
Interfaces also support declaration merging. Separate declarations with the same interface name are combined by TypeScript.
interface Settings {
theme: "light" | "dark";
}
interface Settings {
language: string;
}
const settings: Settings = {
theme: "dark",
language: "en"
};This can help library authors add members through module augmentation. In application code, accidental merging can make a type harder to find and understand, so use it deliberately.
Choose intersections for composition
Use & when you need to compose types that come from different sources, including type aliases, mapped types, and generic types.
type Identified<T> = T & {
id: string;
};
type Product = {
name: string;
price: number;
};
type StoredProduct = Identified<Product>;
const product: StoredProduct = {
id: "p-7",
name: "Notebook",
price: 12
};An intersection is also useful when a function adds fields to an input type:
function addId<T>(value: T, id: string): T & { id: string } {
return { ...value, id };
}
const stored = addId({ name: "Pen" }, "p-8");
stored.name.toUpperCase();
stored.id.toUpperCase();The return type says that the result has the original properties and an id.
Conflicts behave differently than inheritance
If two intersected object types declare the same property with incompatible types, that property becomes never, meaning no value can satisfy it.
type TextValue = { value: string };
type NumericValue = { value: number };
type ImpossibleValue = TextValue & NumericValue;
// { value: never }By contrast, an interface reports the conflict at the declaration site:
interface TextRecord {
value: string;
}
// This declaration is rejected because the inherited property types conflict.
// interface InvalidRecord extends TextRecord {
// value: number;
// }This difference affects error location. extends catches an invalid relationship while the interface is declared. An intersection can construct a type that looks valid until you try to create a value.
A practical decision rule
Use extends when the type has a stable name and a clear parent-child relationship. Use an intersection when you are assembling capabilities, adding fields generically, or combining types that are not part of one inheritance hierarchy.
You can also combine the approaches. An interface may extend a base interface, while a type alias adds a separate capability with &.
Exercise
Which declaration should you choose for a CachedUser type that combines a User object with { cachedAt: Date }, and why?
Show answer
Use type CachedUser = User & { cachedAt: Date } when User is being composed with an additional capability. Use interface CachedUser extends User { cachedAt: Date } instead if you want a named interface hierarchy. Both require all members from both parts.