TypeScript Class Inheritance guide with Abstract Methods and Concrete Implementation Confusion
Does anyone know how to This might be a silly question, but I've searched everywhere and can't find a clear answer. This might be a silly question, but I'm working with an scenario with TypeScript where I'm trying to implement an abstract class that has an abstract method. However, when I extend this abstract class and provide a concrete implementation in the child class, I get a runtime behavior indicating that the abstract method is not implemented, even though it appears to be. Here's a simplified version of what I have: ```typescript abstract class Shape { abstract getArea(): number; } class Circle extends Shape { constructor(private radius: number) { super(); } getArea(): number { return Math.PI * this.radius * this.radius; } } const circle = new Circle(5); console.log(circle.getArea()); ``` When I run this code, it correctly calculates the area of the circle as expected. However, when I try to extend `Shape` with another class like this: ```typescript class Rectangle extends Shape { constructor(private width: number, private height: number) { super(); } // Missing implementation of getArea() here } ``` I get the following behavior when I try to instantiate `Rectangle`: ``` behavior: Class 'Rectangle' incorrectly implements interface 'Shape'. Property 'getArea' is missing in type 'Rectangle' but required in type 'Shape'. ``` I've checked to ensure that all methods are correctly defined in each class, but I must be missing something when trying to implement `Rectangle`. From my understanding, I should be able to leave out the implementation of `getArea()` in `Rectangle` if it's not supposed to be used directly. Could anyone clarify why this is happening and how to resolve this? I've also tried adding a concrete implementation for `getArea()` in `Rectangle`, but I keep running into confusion regarding the design pattern of having multiple child classes inheriting from the same abstract class. I'm using TypeScript version 4.4.3. This is part of a larger application I'm building. I'd really appreciate any guidance on this. For context: I'm using Typescript on Ubuntu. Has anyone else encountered this? Thanks in advance! Thanks in advance!