...

/

Solution: TypeScript Inheritance

Solution: TypeScript Inheritance

Review the solution to the challenge of implementing inheritance in TypeScript.

We'll cover the following...

The solution to the previous challenge is given below:

Press + to interact
abstract class Shape {
constructor(public name: string) {}
abstract calculateArea(): number;
}
class Circle extends Shape {
constructor(public radius: number, name: string) {
super(name);
}
calculateArea(): number {
return Number((Math.PI * this.radius ** 2).toFixed(2));
}
}
class Square extends Shape {
constructor(public sideLength: number, name: string) {
super(name);
}
calculateArea(): number {
return this.sideLength ** 2;
}
}
class Triangle extends Shape {
constructor(public base: number, public height: number, name: string) {
super(name);
}
calculateArea(): number {
return 0.5 * this.base * this.height;
}
}

Explanation

  • Lines 1–4: We define an abstract class named Shape. It has a constructor that accepts a ...