interface Parent {
id: number;
}
interface ChildA extends Parent {
desc: string;
}
interface ChildB extends Parent {
eatable: boolean;
}
let arr: Array<Parent> = []
arr.push({
id: 1,
desc: "test"
});
arr.push({
id: 2,
eatable: false
});
I have two different types with shared properties in my webapp, and i'd like to be able to have instances of these be stored in an array, to loop through when i only need to display the shared properties. How can i do this? The above code gives a type error.
Update #1:
How come i am not able to do the above, but i am able to do the following? This gives me, type-wise, exactly the same as the top example.
let arr: Parent[] = []
let tmp_arr: (ChildA | ChildB)[] = [];
arr = tmp_arr;
tmp_arr.push({
id: 1,
desc: "test"
});
tmp_arr.push({
id: 2,
eatable: false
});
for (let child of arr) {
console.log(child.id);
}
//(console: 1, 2)