假设我有一个界面:
df.pivot_table(index=['DECLARANT','PARTNER','PRODUCT'],
columns=['PERIOD'],
values=['KG', 'VALUE'])
我想创建自己的界面,如下所示:
// I get this from an external library so I have no control over it
interface Balloon {
diameter: number;
color: "red" | "blue" | "green";
}
我的问题是是否可以从 Balloon 中“获取”颜色部分并将其注入 Shirt,所以我得到了这样的结果:
interface Shirt {
size: "m" | "l" | "xl";
color: "red" | "blue" | "green";
}
答案 0 :(得分:0)
您可以使用基本接口:
interface WithColor {
color: "red" | "blue" | "green";
}
interface Shirt extends WithColor {
size: "m" | "l" | "xl";
}
或者您可以将 Color
设为枚举:
enum Color {
RED = "red",
BLUE = "blue",
GREEN = "green",
}
interface Shirt {
size: "m" | "l" | "xl";
color: Color;
}
...甚至这两者的组合。
虽然我不建议使用其他类型的接口,因为这会带来可维护性风险,如果您碰巧在源接口上更改它,这也是一种可能的方法:
interface Shirt {
size: "m" | "l" | "xl";
color: "red" | "blue" | "green";
}
interface Balloon {
size: "m" | "l" | "xl";
color: Shirt["color"];
}
答案 1 :(得分:0)
interface Balloon {
diameter: number;
color: "red" | "blue" | "green";
}
type Shirt = {
size: "m" | "l" | "xl";
} & Pick<Balloon, 'color'>
type Check = Shirt['color'] // "red" | "blue" | "green"
Here 您可以找到更多实用程序类型