I'm trying to discover a pattern for combining multiple interfaces into one abstract class. Presently I can combine multiple interfaces via implements
, but an interface cannot declare a constructor. When I must introduce a constructor I'm forced to use an abstract class. When I use a abstract class I must re-declare the entire composite interface! Surely I'm missing something?
interface ILayerInfo { a: string; } interface ILayerStatic { b(): string; } class Layer implements ILayerInfo, ILayerStatic { constructor(info: ILayerInfo); a: string; b(): string; }
ANSWER: Use new
:
interface Layer extends ILayerInfo, ILayerStatic { new(info: ILayerInfo); } // usage: new Layer({ a: "" });
Multiple inheritance has been a controversial issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said ...
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.
Interfaces contain only public constant definitions and method headers. Interfaces can only extend other interfaces, not classes. Interfaces can extend multiple interfaces, and classes can implement multiple interfaces.
Java doesn't support multiple inheritances in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritances.
Declaring a constructor on the same interface as the instance members doesn't really make much sense -- if you're going to pass in a type dynamically to use in a constructor, it's the static side of the class that would be restricted. What you would want to do is probably something like this:
interface Colorable { colorize(c: string): void; } interface Countable { count: number; } interface ColorCountable extends Colorable, Countable { } interface ColorCountableCreator { new(info: {color: string; count: number}): ColorCountable; } class ColorCounted implements ColorCountable { count: number; colorize(s: string) { } constructor(info: {color: string; count: number}) { // ... } } function makeThings(c: ColorCountableCreator) { var results: ColorCountable[]; for(var i = 0; i < 10; i++) { results.push(new c({color: 'blue', count: i})); } return results; } var items = makeThings(ColorCounted); console.log(items[0].count);
See also How does typescript interfaces with construct signatures work?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With