我的接口定义如下:
interface One {
one: {
oneone: string;
fieldA: string;
fieldB: string;
fieldC: string;
}
}
interface Two {
two: {
twotwo: string;
fieldA: string;
fieldB: string;
fieldC: string;
}
}
如您所见,fieldA
,fieldB
和fieldC
是可共享的。无论如何,我们可以分享它们吗?像
interface SharedFields {
fieldA: string;
fieldB: string;
fieldC: string;
}
interface One {
one: (extends SharedFields) {
oneone: string;
}
}
答案 0 :(得分:3)
您可以为此使用intersection type:
interface SharedFields {
fieldA: string;
fieldB: string;
fieldC: string;
}
interface One {
one: SharedFields & {
oneone: string;
}
}