I have data that can have one set of identifying fields, a second set, or both. I have that coded in interfaces like this.
export interface IRecordIdBase {
RecordType: enumRecordType;
}
export interface IRecordIdA extends IRecordIdBase {
AlphaCode: string;
LocationCode: string;
}
export interface IRecordIdB extends IRecordIdBase {
BravoCode: string;
LocationCode?: string;
}
The trouble is that the only way I know to make a "this, that, or both" item is with a union type.
export type IRecordId = IRecordIdA | IRecordIdB | (IRecordIdA & IRecordIdB);
However, I can't later extend a type. I use different subsets of data, requiring different interfaces, but all have the same ID structure. For example,
// can't extend IRecordId
export interface IRecordContent extends IRecordId {
RecordData: string;
RecordAuthor: string;
}
export interface IRecordMeta extends IRecordId {
RecordAdded: string;
RecordLastUpdated: string;
}
Is there an interface pattern or other solution that addresses this? I'm using TypeScript v2.6.1.