I have a scenario I need help with.
Let's say we have two interfaces, Cats
and Dogs
. How can I create an array that can store both Cats
and Dogs
?
interface Cats {
name: string;
age: number;
}
interface Dog {
owner: string;
}
const cat1: Cats = {
name: "Jimmy",
age: 5,
}
const dog1: Dogs = {
owner: "Bobby",
}
// The line below is not functioning as expected
const animalsList: Array<Cats> | Array<Dogs> = [cat1, dog1];
The variable animalsList
should be able to hold instances of both Cats
and Dogs
, but I am encountering errors like
"Type Dogs
cannot be assigned to type Array<Cats>
"