So here's my problem, let's say I have some types of animals:
interface IAnimal {
type: string;
legs: number;
}
interface IChicken extends IAnimal {
eggsPerDay: number;
}
interface ICow extends IAnimal {
milkProduction: number;
}
...
doSomethingWithAnimal(animal: IChicken|ICow): void {
switch(animal.type){
case 'cow':
alert(`the milk production is ${animal.milkProduction}`);
}
}
The problem that I have is typescript doesn't know if it's a cow or a chicken. Is there a way that once I know for sure that the animal is of type ICow that I can safely access those additional attributes?
I know that it's possible to do const cow = animal as ICow but wouldn't that needlessly copy my variable? The example that I provided is an oversimplification since the problem that I'm actually trying to solve has ten derived classes and the function is called a ton, so I want to keep it as efficient as possible.