Here's an example of what I'm asking.
Say that we have two interfaces, Cats
and Dogs
. How can I make an array that can hold 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 doesn't work how I think it would work
const animalsList: Array<Cats> | Array<Dogs> = [cat1, dog1];
The variable animalsList
should be able to have both Cats
and Dogs
in it, but I am getting errors like
"Type Dogs
can not be assigned to type Array<Cats>
"
Here is a full sample:
You can try it on https://www.typescriptlang.org/play