Skip to main content

Multiple inheritance

1. Short answer

Yes, you can! This uses the extends keyword, followed by several interfaces separated by commas.


Example

javascript
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 C now includes properties from all three interfaces: a, b, and c.


2. How it works

  • TypeScript combines all properties from interfaces A and B.
  • 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

javascript
interface X { value: string; } interface Y { value: string; } interface Z extends X, Y { flag: boolean; } const z: Z = { value: "ok", flag: true }; // types match

Example of conflicting properties

javascript
interface X { value: string; } interface Y { value: number; } interface Z extends X, Y {} // Error: types are incompatible

TypeScript cannot merge string and number into 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:

javascript
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, };

Circle combines everything: coordinates, color, and radius.


4. Inheriting interfaces with methods

javascript
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 duck object must implement all the methods from CanFly, CanSwim, and Duck.


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 (&):

javascript
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 };

interface can only extend other interfaces via extends. If you need to combine a type and an interface, use &.


6. Example from a real project

javascript
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:

javascript
interface 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.