Presently, I have developed the following interface structure:
export interface Vehicle {
id: number;
type: 'car' | 'airplane';
jet_engines?: number;
wheels?: number;
}
My aim is to restrict cars from having the jet_engines
property and airplanes from possessing the wheels
property.
The desired usage scenario is as follows:
const func = (vehicle: Vehicle) => {
if (vehicle.type === 'airplane') {
// TypeScript should be informed about the existence of `jet_engines` within `vehicle`.
}
}
I am looking for a solution that does not involve defining separate interfaces like this:
const func = (vehicle: Car | Airplane) => {
Is there a way to achieve this objective while preserving the structure of the Vehicle
interface without converting it into a type?