Looking for some guidance with typescript. Even after implementing a type guard (and including the '?' symbol), I'm still encountering errors in the code snippet below. Is the syntax correct or should I make changes to the tsconfig file?
interface Bird {
type: "bird";
speed: number;
flyingSpeed?: number;
}
interface Snail {
type: "snail";
speed: number;
crawlingSpeed?: number;
}
function accelerateAnimal(animal: Snail | Bird): void {
switch (animal.type) {
case "bird":
speed = speed + animal.flyingSpeed;
break;
case "snail":
speed = speed + animal.crawlingSpeed;
break;
default:
break;
}
}
accelerateAnimal({type: "bird", speed: 0, flyingSpeed: 3});