Solución
solution.tsTypeScript
export function calcArea(type: 'circle' | 'rect', ...dims: number[]): number {
// Usa la clase abstracta Shape y sus subclases para calcular el área
abstract class Shape{
abstract area():number
}
class Circle extends Shape{
constructor(private radius:number){
super();
this.radius = radius
}
area(){
return Number((Math.PI * this.radius ** 2).toFixed(2))
}
}
class Rect extends Shape{
constructor(private width:number,private height:number){
super()
this.height = height
this.width = width
}
area(){
return this.height * this.width
}
}
switch(type){
case('circle'):
return new Circle(dims[0]).area()
case('rect'):
return new Rect(dims[0],dims[1]).area()
}
}
0respuestas