There is a question on SO about creating an array with different types, but I want to create an array with multiple complex types. The answer in the linked to question recommends using a union type, but I don't think that will suffice for a complex type, as typescript documentation says
If we have a value that has a union type, we can only access members that are common to all types in the union.
I want to create an array of Reports. However some of the reports have different properties from each other, and also several in common. I'm worried if I create it as below then somewhere in the code I might not be able to access the different properties on the subreports.
What's the recommended way to create an array of complex types in Typescript?
interface Reports extends Array<Report>{}
interface Report {
id: number;
timestamp: number;
type: string;
subreport: SubReportA | SubReportB | SubReportC
}
interface SubReportA {
values: []
}
interface SubReportB {
random_property: number;
other_random_name: number;
}
interface SubReportC{
values: [];
name: string;
}