Lets say I have two interfaces which have two the same members id, and name:
export interface InterfaceA {
id: number;
name: string;
//some other members
}
export interface InterfaceB {
id: number;
name: string;
//some other members
}
I would like to get collection of elements of both types to populate some combobox. I need id, name and type of every element, so I've made following class
export class AssignableDevice {
id: number;
name: string;
type: string;
constructor(device: InterfaceA | InterfaceB) {
this.id = device.id;
this.name = device.name;
this.type = typeof device; //still returns "object"
}
}
// in onInit method :
ngOnInit() {
super.ngOnInit();
this.dataService.getInterfaceA().subscribe((data) => {
data.forEach((element) => this.devices.push(new AssignableDevice(element as InterfaceA)));
});
this.dataService.getInterfaceB().subscribe((data) => {
data.forEach((element) => this.devices.push(new AssignableDevice(element as InterfaceB)));
})
}
But problem is I always get "object" in "AssignableDevice" class constructor, and I have no idea why is this happening. I can achieve my goal by using some enum, but I wonder, why this solution is not working, and how could achieve this. I'd rather not make any changes in InterfaceA or InterfaceB.