Typescript hierarchical inheritance

0 votes

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?

Jul 5 in TypeSript by Nina
• 3,020 points
21 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer
0 votes
1 answer

TypeScript Object assign gives me an error property assign does not exist on type ObjectConstructor

For TypeScript 2.1 and higher, you can ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
70 views
0 votes
1 answer

Cast a JSON Object to a TypeScript class

I had the same issue and this ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
92 views
0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
24 views
0 votes
1 answer

TypeScript error TS2304: cannot find name ' require'

Here's a really easy work around: declare var ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
1,511 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
13 views
0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,080 points
248 views
0 votes
1 answer

Cannot access web3 object with typescript and ethereum

You still need to instantiate it first. ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,310 points
1,861 views
0 votes
1 answer

How is inheritance in C++ different than that in Java?

The purpose of inheritance is same for ...READ MORE

answered Feb 6, 2019 in Java by Priyaj
• 58,080 points
330 views
0 votes
1 answer

how to refer to a parent method in python?

If you know you want to use ...READ MORE

answered Apr 22, 2019 in Python by SDeb
• 13,300 points
344 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP