Solución
solution.tsTypeScript
export function makeSound(type: 'dog' | 'cat', name: string): string {
// Crea la instancia correspondiente y retorna la descripción del sonido
class Animal {
name = '';
constructor(name: string) {
this.name = name;
}
sound() {
return '...';
}
}
class Dog extends Animal {
override sound() {
return 'Guau'
}
}
class Cat extends Animal {
override sound() {
return 'Miau'
}
}
let whatAnimalSays = '';
if(type === 'dog') {
const newDog = new Dog(name);
whatAnimalSays = `${name} dice ${newDog.sound()}`;
}
if(type === 'cat') {
const newCat = new Cat(name);
whatAnimalSays = `${name} dice ${newCat.sound()}`
}
return whatAnimalSays;
}0respuestas