Closed samseburn closed 1 month ago
function speak(animal: Animal): string {
switch (animal.type) {
case 'dog':
return animal.bark();
case 'cat':
return animal.meow();
case 'bird':
return animal.chirp();
default:
return '';
}
}
function speak(animal: Animal): string {
if ('bark' in animal) {
return animal.bark();
} else if ('meow' in animal) {
return animal.meow();
} else if ('chirp' in animal) {
return animal.chirp();
}
return 'Unknown animal sound';
}
function speak(animal: Animal): string {
switch (animal.type) {
case 'dog':
return animal.bark();
case 'cat':
return animal.meow();
case 'bird':
return animal.chirp();
default:
return '';
}
}
type Animal =
| { type: 'dog'; bark: () => string }
| { type: 'cat'; meow: () => string }
| { type: 'bird'; chirp: () => string };
function speak(animal: Animal): string { //2. if(animal.type === 'dog') return animal.bark(); if(animal.type === 'cat') return animal.meow(); if(animal.type === 'bird') return animal.chirp(); }
📚 139p - 145p 📌
function speak(animal: Animal): string { // 여기에 타입 좁히기를 활용하여 각 동물에 맞는 소리를 반환하는 코드를 작성하세요. }