I have a base Type
that is extended upon by two interfaces. I then have another interface which is a Union
of the two Types.
Later on I process these type to replace the Number
date property which an actual Date
object, so I have a type for that too.
However, upon trying to read the lens
property in my component (after a condition I thought should differentiate it as a Camera
, I get an error.
Property 'lens' does not exist on type 'DatedDevice'.
export enum DeviceType {
Camera = 'camera-device',
Phone = 'phone-device'
}
interface DeviceBase {
id: string;
date: number;
}
export interface Camera extends DeviceBase {
lens: string;
type: DeviceType.Camera;
}
export interface Phone extends DeviceBase {
receiver: string;
type: DeviceType.Phone;
}
// Union of two extended interfaces
export type Device = Camera | Phone;
// Type for replacing number with Date
export interface DatedDevice extends Omit<Device, 'date'> {
date: Date;
}
// Error in component.
// 'device' is a prop of type 'DatedDevice'
if (device.type === DeviceType.Text) {
console.log(device.lens);
// ^ Property 'lens' does not exist on type 'DatedDevice'
}