Trying to store both custom types, Graphic
and Asset
, in the same array is proving to be a challenge. The goal is to access them and retain their individual type information.
const trail: Array<Graphic | Asset> = [];
for (let index = 0; index < this.props.graphics.length; index++) {
const graphic = this.props.graphics[index];
trail.push(graphic);
if (index <= this.assets.length - 1) trail.push(this.assets[index]);
}
// However, an error occurs when trying to access .artist on type Graphic | Asset
if (typeof trail[0].artist != "undefined") {
}
An approach like the following could potentially work, but it has not been successful thus far.
enum TrailType {
Graphic,
Asset
}
interface Trail {
type: TrailType
element: Graphic | Asset
}
Is it only feasible to achieve this when the types have at least one shared property?