Suppose I have a Typescript Interface as follows
export interface IMyObj {
id: string;
type: 'AA' | 'AZ' | 'XY';
...
}
Now I need an other interface which also has that type field
export interface IMyOtherObj {
...
type: 'AA' | 'AZ' | 'XY';
...
}
As you can see I have duplicated the values of type. So my question is, how can I reuse IMyObj.type in my IMyOtherObj interface? I tried this
export interface IMyOtherObj {
...
type: IMyObj.type; // -> error
...
}
I think I'm close but so far no luck, any suggestions?
Your issue is that TS type system has no . property access but indexed property type, change one thing in your type definition:
type: IMyObj['type']
Define an enumeration for your property type, such as
enum MyEnum {
Aa = "AA",
Az = "AZ",
Xy = "XY"
}
and use it like this;
export interface IMyObj {
id: string;
type: MyEnum;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With