When trying to access attributes of a union type that is not present on all types, how can it be done correctly? I keep encountering a TypeScript error stating "property ... does not exist on type..." even when checking if the attribute exists (see example below)
interface Car {
wheels: 4,
spareWheels: 1,
}
interface Bicycle {
wheels: 2,
}
type Vehicle = Car | Bicycle
getWheelCount(vehicle: Vehicle): number => {
return vehicle.spareWheels ? vehicle.spareWheels + vehicle.wheels : vehicle.wheels
}
The documentation (https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) offers a solution using 'typeof', but this approach doesn't seem to work for custom types.