I want to create a React functional component which takes one of the predefined components as a prop and which logic depends on what component was passed in.
So these are the possible names:
enum ComponentName {
ComponentA: "component.a",
ComponentB: "component.b",
ComponentC: "component.c",
// etc
}
Now the hard part, I have a bunch of Component types, which names are all in the enum, like:
type ComponentA = {
attributes: {
// ...
}
}
type ComponentB = {
attributes: {
// ...
}
}
Now what I want to do is somehow to define type Component, so that:
type Component =
| {
id: string;
name: ComponentName.ComponentA;
component: ComponentA;
}
| {
id: string;
name: ComponentName.ComponentB;
component: ComponentB;
}
| // ...
Is there a way to simplify this union type by mapping over ComponentName enum and matching enum prop to Component type name?
I think if you have some sort of mappings between component names and their attributes
ComponentMappings, then you can create a union from that mapping using a helper function likeValuesgenerates a union type from the values of a mapped type or object.TypesSript Playground
Note that
Prettifytype in the playground does nothing and just makes it more readable when you hover on the type.