After sending a request to a remote server, I am returned with a JSON array. However, the array can contain two different types of JSON objects:
[
{
"country": "USA",
"caseCount": 561,
"caseDates": [],
},
{
"country": "Canada",
"errorDescription": "Error"
}
]
To handle this, I have created two interfaces and a variable that can hold either of these types:
export interface CountryData {
country: string;
caseCount: number;
caseDates: any[];
}
export interface ErrorData {
country: string;
errorDescription: string;
}
countryData!: Observable<(CountryData| ErrorData)[]>;
When trying to display this data in my template with
*ngFor="let data of countryData | async"
, it only displays values common to both interfaces, such as the country
field. I would like to modify this behavior to render values based on the type of the data
object.