【发布时间】:2022-12-27 06:44:06
【问题描述】:
Please see the code block below.
import Button from './Button'; // React component which has type React.FC<ButtonType>
import Select from './Select'; // React component which has type React.FC<SelectType>
import Checkbox from './Checkbox'; // React component which has type React.FC<CheckboxType>
enum ComponentType {
button = 'bla_button',
select = 'bla_select',
checkbox = 'bla_checkbox',
}
type ComponentMap = {
[key in ComponentType]: React.FC<any>;
};
const componentMap: ComponentMap = {
[ComponentType.button]: Button,
[ComponentType.select]: Select,
[ComponentType.checkbox]: Checkbox,
};
I want to have a better type for ComponentMap instead of using React.FC<any> so it can infer when the key is 'bla_button', the value must be Button and so on.
【问题讨论】:
标签: reactjs typescript typescript-typings typescript-generics