我想创建一个Reaction功能组件,它将预定义的组件之一作为props ,哪个逻辑取决于传入的组件.
因此,以下是可能的名称:
enum ComponentName {
ComponentA: "component.a",
ComponentB: "component.b",
ComponentC: "component.c",
// etc
}
现在最难的是,我有一组组件类型,它们的名称都在枚举中,如下所示:
type ComponentA = {
attributes: {
// ...
}
}
type ComponentB = {
attributes: {
// ...
}
}
现在,我想要做的是以某种方式定义类型组件,以便:
type Component =
| {
id: string;
name: ComponentName.ComponentA;
component: ComponentA;
}
| {
id: string;
name: ComponentName.ComponentB;
component: ComponentB;
}
| // ...
有没有一种方法可以通过映射ComponentName
多个枚举并将枚举属性匹配到组件类型名称来简化这种联合类型?