I have some interface, that has an enum field. Also want to make an overload for this interface which will take one more property on the case of some specific enum value.
As an example:
enum CellValueType {
DATE,
STRING,
NUMBER
}
export interface TableCell {
type: CellValueType;
value: string; // ...
}
export interface TableCell {
type: CellValueType.DATE;
format: string; // Should be exist only on the DATE type
value: string; // ...
}
But, the problem is using the interface I got an error:
Subsequent property declarations must have the same type. Property 'type' must be of type 'CellValueType', but here has type 'CellValueType.DATE'.
Error is understandable, no questions about it.
The main question is how to avoid it in a legal way.
I know, that I can just use type instead of interface and get this construction:
export type TableCell = {
type: CellValueType;
value: string;
} | {
type: CellValueType.DATE;
format: string;
value: string;
}
Works pretty well, but the problem is that classes cannot implement union types.
Not working as expected, with DATE still compatible with the first signature.
To work correctly, it needs the type: CellValueType to be changed to type: Exclude<CellValueType, CellValueType.DATE>
Also, I can create a sub-interface only for the DATE enum type but want also to avoid creating an interface for every case.