【发布时间】:2018-02-09 19:53:58
【问题描述】:
我有以下Widget 类型:
enum WidgetType {
Page = "Page",
Tab = "Tab",
Value = "Value",
Chart = "Chart"
}
type PageWidget = {
type: WidgetType.Page;
children: Widget[];
};
type TabWidget = {
type: WidgetType.Tab;
children: Widget[];
};
type ValueWidget = {
type: WidgetType.Value;
};
type ChartWidget = {
type: WidgetType.Chart;
};
type Widget = PageWidget | TabWidget | ValueWidget | ChartWidget;
基于此,我想创建新类型调用WidgetWithChildren,它必须是具有children 属性的小部件的联合(在这种情况下是PageWidget 和TabWidget 的联合),但我想要让它动态化,所以当有一个新类型的 Widget 时,它会自动出现在 WidgetWithChildren 中。
我想要类似的东西:
type WidgetWithChildren = Pick<Widget, "take widgets children">;
在 TypeScript 中可以吗?我该怎么做?
【问题讨论】: