I would like to model an interface that has array that accepts objects as long as they extend a particular interface. I'm still learning Generics, so please bear with me.
Here's my "base" interface:
export interface IEntry {
sys: {
contentType: {
. . .
}
}
}
and my extended interface:
export interface IWorkSample extends IEntry {
fields: {
. . .
}
}
Here's the interface that has the array:
export interface IEntries {
items: IEntry[]; <- doesn't allow for IWorkSample or future interfaces
limit: number;
skip: number;
sys: {
type: string;
};
total: number;
}
I can of course do:
items: IWorkCategory[] | IWorkSample[];
but then I have to keep adding types.
I'd like to be able to write the IEntries interface to allow items to contain objects that extend IEntry. I'm reading the Generics Documentation, but obviously coming up short.
I can use items: any[], but that of course doesn't guarantee the objects will have properties I'll expect in the future. Can someone please help me out with an example that makes sense?