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?

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"? 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, 2022 in TypeSript by Nina
• 3,060 points

edited Dec 6, 2023 by Soumya 671 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, 2022 in TypeSript by Nina
• 3,060 points
2,107 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, 2022 in TypeSript by Nina
• 3,060 points
3,096 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, 2022 in TypeSript by Nina
• 3,060 points
873 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, 2022 in TypeSript by Nina
• 3,060 points
14,000 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, 2022 in TypeSript by Nina
• 3,060 points
567 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,020 points
814 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,370 points
2,907 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,020 points
1,002 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
1,106 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP