What is the correct way to access attributes of a union type that is not present on all of the types? I seem to always get a typescript "property ... does not exist on type..." error, even if I do a check to make sure 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 ) has a solution to use 'typeof', but that doesn't work for custom types.