How can I set a superior class in TypeScript when collecting various different objects in one array that inherit from the same class so that TypeScript doesn't show an error?
I'm trying it like this:
interface IVehicle{
modelName: string
}
interface ICar extends IVehicle{
numberOfDoors: number,
isDropTop: boolean
}
interface IBike extends IVehicle{
hasDynamo: boolean
}
var vehicles: IVehicle[] =
[
{
modelName: "carModelName", // Error
numberOfDoors: 4,
isDropTop: true
},
{
modelName: "bikeModelName",
hasDynamo: true
}
]
Doing it this way, I'm getting errors.
I'm just able to add objects of the superior interface IVehicle if I don't want any errors shown.