In my Angular project I have a list of reports. The list/array has a base class because the list can contain different types of reports. But although the list is of type: ReturnReport
the actuall items in the list can be of type: SafetyReport
or DraftSafetyReport
. The reason for these 2 objects is that they contain different data but needed to be shown in 1 and the same datatable.
export abstract class ReturnReport {
}
export class DraftSafetyReport extends ReturnReport {
}
export class SafetyReport extends ReturnReport {
id?: string;
status?: string;
safetyReportSummary!: SafetyReportSummary;
}
In my component I have the following so both types of reports can be in the list:
reportList$: BehaviorSubject<ReturnReport[]> = new BehaviorSubject<ReturnReport[]>([]);
So far so good, and I understand how it all works, but I'm facing an issue when rendering in my HTML.
<tbody *ngIf="(reportList$ | async) as reportlist" class="">
<tr *ngFor="let report of reportlist" class="h-12">
<td>{{report.safetyReportSummary.occurrenceDate | date: 'dd-MM-yyyy'}}</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
The error is:
Property 'safetyReportSummary' does not exist on type 'ReturnReport'.
I know why, because the type of the list is ReturnReport. So the property safetyReportSummary
does not exist. But how do I achieve this? Should I look in the corner of ng-template or ng-templateOutlet? Do I somehow cast the object before I render a specific HTML
I use Angular 15.2.3.
Any questions, please let me know! Thank you!