Multiple inheritance
1. Short answer
Yes, you can! This uses the
extendskeyword, followed by several interfaces separated by commas.
Example
interface A {
a: string;
}
interface B {
b: number;
}
interface C extends A, B {
c: boolean;
}
const obj: C = {
a: "Hello",
b: 42,
c: true,
};Interface
Cnow includes properties from all three interfaces:a,b, andc.
2. How it works
- TypeScript combines all properties from interfaces
AandB. - If the inherited fields do not conflict, everything is fine.
- If the same property is declared in several interfaces, TypeScript checks whether their types are compatible.
Example of compatible properties
interface X {
value: string;
}
interface Y {
value: string;
}
interface Z extends X, Y {
flag: boolean;
}
const z: Z = { value: "ok", flag: true }; // types matchExample of conflicting properties
interface X {
value: string;
}
interface Y {
value: number;
}
interface Z extends X, Y {} // Error: types are incompatibleTypeScript cannot merge
stringandnumberinto a single property with the same name.
3. Inheritance can be combined with your own fields
You can extend several interfaces and add new properties:
interface Position {
x: number;
y: number;
}
interface Shape {
color: string;
}
interface Circle extends Position, Shape {
radius: number;
}
const c: Circle = {
x: 0,
y: 0,
color: "red",
radius: 10,
};
Circlecombines everything: coordinates, color, and radius.
4. Inheriting interfaces with methods
interface CanFly {
fly(): void;
}
interface CanSwim {
swim(): void;
}
interface Duck extends CanFly, CanSwim {
quack(): void;
}
const duck: Duck = {
fly: () => console.log("Flying"),
swim: () => console.log("Swimming"),
quack: () => console.log("Quack!"),
};The
duckobject must implement all the methods fromCanFly,CanSwim, andDuck.
5. You can combine interfaces and type
If you have a type, an interface cannot directly inherit from it,
but you can use an intersection of types (&):
type Timestamped = { createdAt: Date };
interface Entity {
id: number;
}
interface User extends Entity, Timestamped { // not allowed directly
name: string;
}
// alternative via &:
type UserFull = Entity & Timestamped & { name: string };
interfacecan only extend other interfaces viaextends. If you need to combine atypeand aninterface, use&.
6. Example from a real project
interface Identifiable {
id: number;
}
interface Timestamped {
createdAt: Date;
updatedAt: Date;
}
interface User extends Identifiable, Timestamped {
name: string;
email: string;
}
const u: User = {
id: 1,
createdAt: new Date(),
updatedAt: new Date(),
name: "Tim",
email: "tim@example.com",
};A single interface (
User) combines identification data and timestamps.
Summary
In TypeScript, an interface can inherit from several other interfaces:
javascriptinterface C extends A, B, D { ... }This allows you to:
- combine structures without duplicating code;
- build a "hierarchy of contracts";
- extend interfaces with methods, properties, and options;
- compose behavior (for example,
CanFly,CanSwim,CanRun).
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.