I currently have the following setup:
class Dog extends Animal{}
class Animal{}
This is a simple inheritance structure. Now, I have another class that contains all types of animals.
class Farm{
animals: Animal[];
}
At a certain point in my application, I need to loop through the array of animals. When I come across a dog, I want to assign it to a variable:
_.forEach(farm.animals, (animal:Animal)=>{
if(animal instanceof Dog){
this.dog = animal;
}
}
However, I am encountering two errors. Firstly, the 'instanceof' operator is not functioning properly and showing an error stating that the right-hand side of the argument is null.
The second error states that type 'Animal' is not assignable to type 'Dog'. The same issue occurs when I declare 'Animal' as an interface.