Sub topic 7
Building on Existing Shapes with `extends`
This chapter assumes you know basic TypeScript annotations, JavaScript objects, and how to run the TypeScript compiler. You will use interfaces, required properties, and methods.
Extending an interface
An interface describes the required shape of an object. When one interface extends another, the new interface inherits all members from the original interface and can add more members.
interface Person {
name: string;
email: string;
}
interface Employee extends Person {
employeeId: number;
department: string;
}
const employee: Employee = {
name: "Grace Hopper",
email: "[email protected]",
employeeId: 42,
department: "Engineering",
};Employee includes name and email because it extends Person. It also requires employeeId and department. An Employee value must satisfy every requirement from both interfaces.
You can extend more than one interface:
interface HasId {
id: number;
}
interface HasTimestamps {
createdAt: Date;
updatedAt: Date;
}
interface Order extends HasId, HasTimestamps {
total: number;
}
const order: Order = {
id: 1001,
createdAt: new Date(),
updatedAt: new Date(),
total: 49.99,
};The child interface receives members from all its parent interfaces. If parent interfaces declare the same property, their declarations must be compatible.
Extending versus intersections
An intersection type uses & to combine types:
type HasId = {
id: number;
};
type HasTimestamps = {
createdAt: Date;
updatedAt: Date;
};
type Order = HasId & HasTimestamps & {
total: number;
};This produces a type with all the members from HasId, HasTimestamps, and the final object type. For ordinary object shapes, the result is often similar to interface extension.
| Approach | Typical use | Important feature |
|---|---|---|
| `interface Order extends Base` | Public object contracts | Can be extended and declaration-merged |
| `type Order = Base & Extra` | Composed or computed types | Works with unions and other type expressions |
The two forms can differ when properties conflict. An interface must reject incompatible overrides:
interface Base {
value: string;
}
// interface Invalid extends Base {
// value: number; // error
// }An intersection may create an impossible property:
type Conflicting = { value: string } & { value: number };
// Conflicting["value"] is effectively neverA value cannot normally be both a string and a number, so this intersection is difficult to use.
A practical decision
Prefer extends for a clear inheritance relationship between named object contracts. It communicates that the child is a specialized form of the parent.
Prefer an intersection when you are assembling types conditionally or combining aliases with unions, mapped types, or other type expressions. Both choices follow TypeScript's structural typing: a value matches when it has the required members, regardless of the type's name.
Exercise
Which declaration correctly creates an Admin shape with all User properties plus a permissions array?
Show answer
interface Admin extends User { permissions: string[]; }