3

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:

  1. It's nonsense. Combining common fields and methods is basically THE REASON you use classes. If all you need is to ensure adherence to a contract, just use interfaces.
  2. It appears to be false? The example code above is code I wrote into a typescript file, and it seems to work fine. 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?

2
  • 1
    Those articles seem really off to me. In what universe does "single inheritance" place any restrictions on the number of subclasses a given parent class can have?
    – jcalz
    Feb 9, 2021 at 2:02
  • I have just also been confused by the information found on avatpoint.com and I wanted to ask the same question you already did.
    – vich
    Aug 27, 2021 at 10:21

2 Answers 2

3

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.

Playground link to code

0

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.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 20, 2022 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.