I've a List component
type Option = {
id: string;
};
type Props<O> = {
options?: O[];
option?: JSXElementConstructor<O>;
};
const List = <O extends Option>(props: Props<O>) => ...
and some option components like this one
type Props = {
label: string;
icon?: ElementType;
onClick?: () => void;
};
const BasicOption = (props: Props) => ...
in order to get the right type I have to do this
const Example = () => {
const options = [
{ id: 'a', label: 'Label A', icon: PlaceholderIcon },
{ id: 'b', label: 'Label B', icon: PlaceholderIcon },
{ id: 'c', label: 'Label C', icon: PlaceholderIcon },
{ id: 'd', label: 'Label D', disabled: true },
{ id: 'e', label: 'Label E' },
];
return (
<List<typeof options[number]>
options={options}
option={BasicOption}
/>
);
};
is there a way to get the right typing directly from the array in order to avoid the <typeof options[number]> part?
Working example: https://codesandbox.io/s/vigorous-hopper-i41uj?file=/src/App.tsx