I'd like to declare an Array of items with a common base interface, but can't figure out how.
I have a base interface and several child interfaces extending the base:
interface Animal {
name: string;
birthdate: Date;
}
interface Bird extends Animal {
featherColor: string;
}
interface Dog extends Animal {
furColor: string;
}
I would like to do something like this:
interface AnimalList {
animals: Array<? extends Animal>
}
// define an example
const list: AnimalList = {
animals: [
{name: 'Bobby', birthdate: new Date(), furColor: 'brown'},
{name: 'Bobby', birthdate: new Date(), featherColor: 'green'},
]
}
How can I declare an array like that? Please note: I cannot use classes as the definition of the interfaces will be done with an OpenApi generator.
I tried Animal[], but that will produce:
TS2322: Type '{ name: string; birthdate: Date; furColor: string; }' is not assignable to type 'Animal'.
Object literal may only specify known properties, and 'furColor' does not exist in type 'Animal'.
TS2322: Type '{ name: string; birthdate: Date; furColor: string; }' is not assignable to type 'Animal'. Object literal may only specify known properties, and 'furColor' does not exist in type 'Animal'.