Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Class Inheritance In TypeScript

What are ways to get around the problem of only being allowed to extend at most one other class.

class Bar {

  doBarThings() {
    //...
  }

}

class Bazz {

  doBazzThings() {
    //...
  }

}

class Foo extends Bar, Bazz {

  doBarThings() {
    super.doBarThings();
    //...
  }

}

This is currently not possible, TypeScript will give an error. One can overcome this problem in other languages by using interfaces but solving the problem with those is not possible in TypeScript.

Suggestions are welcome!

like image 613
nourikhalass Avatar asked Dec 29 '15 14:12

nourikhalass


People also ask

Does TypeScript have multiple inheritance?

An inherited derived class acquires the properties and behaviors of the base class. TypeScript supports single inheritance and multilevel inheritance. We can not implement hybrid and multiple inheritances using TypeScript.

Can we extend more than one class in TypeScript?

In TypeScript, we can't inherit or extend from more than one class, but Mixins helps us to get around that. Mixins create partial classes that we can combine to form a single class that contains all the methods and properties from the partial classes.

How do I inherit a class in TypeScript?

TypeScript supports inheritance like ES6. To inherit a class, you use the extends keyword. For example the following Employee class inherits the Person class: class Employee extends Person { //.. }

Can you implement multiple interfaces in TypeScript?

An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.


1 Answers

Not really a solution to your problem, but it is worth to consider to use composition over inheritance anyway.

Prefer composition over inheritance?

like image 181
spierala Avatar answered Oct 04 '22 07:10

spierala