Reputation: 4464
Consider the following:
class A {
commonString: string = "common";
}
class B extends A {
bString: string = "b";
}
class C extends A {
cString: string = "c";
}
It seems to me like a perfectly ordinary example of inheritance - one of the most basic, in fact. And yet, if I'm reading this documentation right, TypeScript's inheritance mechanism does not support this pattern? Consider the following two sources:
https://www.javatpoint.com/typescript-inheritance - "When more than one subclass is inherited from a single base class, then this type of inheritance is known as hierarchical inheritance. ... TypeScript does not support hierarchical inheritance."
https://www.w3spoint.com/inheritance-typescript - after listing the five types of inheritance the other article listed, says "TypeScript only support Single and Multilevel Inheritance."
I find this difficult to believe, for two reasons:
b: B = B()
behaves as expected: if I access b.bString
it's fine, if I access b.cString
it errors, if I store b: A = new B()
, it works but compile-errors if I access b.bString
(but at runtime bString
is in fact present). It behaves exactly as I'd expect.So, what's going on here? Have I misunderstood "hierarchical inheritance"? Are the given articles wrong? Does typescript claim not to support it, but really it does? Has my compiler been blessed by gnomes?
More to the point, can I trust the usual "B extends A, and also C extends A" to continue to work as expected?
Upvotes: 5
Views: 1010
Reputation: 1
When javatpoint say Hierarchical inheritance they refer to inheriting two classes at the same time which is actually not possible in the case of Typescript.
Upvotes: 0
Reputation: 330086
Yes, of course TypeScript supports "hierarchical inheritance", although I've never heard this term before (or at least not in a way that contrasts with "single inheritance"). Instead of consulting javatpoint.com and w3spoint.com, I'd suggest using The TypeScript Handbook and the microsoft/TypeScript GitHub repository as more authoritative sources of information about TypeScript features.
If you look at the TypeScript Handbook section on class inheritance, you'll find the following example code:
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name);
}
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
which clearly shows that Animal
has two subclasses, Snake
as Horse
; this is a hierarchy of classes and it behaves exactly as you would and do expect.
I really can't say whether javatpoint.com and/or w3spoint.com are generally reliable sources of information and that this is an unfortunate but rare lapse in accuracy, or whether they are completely unreliable, or somewhere in between. From my unscientific random sampling of content from those sites, I'd guess that the authors are not necessarily native speakers of English, and that even if the source material is correct, it might not be communicated clearly enough to be relied upon.
Upvotes: 5