I am facing a scenario where I have two objects named DeviceA and DeviceB, both belonging to the same parent class called Device.
interface Device {
shared: string
}
interface DeviceA extends Device {
attributeA: string[]
}
interface DeviceB extends Device {
attributeB: number
}
Within my component, there is an array containing instances of DeviceA and DeviceB.
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
devices: (DeviceA | DeviceB)[] = [];
}
I am looking for guidance on how to iterate through this array in my template. The desired outcome is as follows:
<div *ngFor="let device of devices">
<div *ngIf="'attributeA' in device">
<span *ngFor="let str of device.attributeA">{{ str }}</span>
</div>
<div *ngIf="'attributeB' in device">
<span>{{ device.attributeB }}</span>
</div>
</div>
The issue arises during compilation with the error message: Property 'attributeA' does not exist on type 'DeviceA | DeviceB'
Any suggestions on how to properly handle typecasting in this situation?