I was trying to implement a Typography react component.
As you can see below, I got variant as an input prop and used it as index of VariantsMap object to get corresponding html tag name.
Then I used styled-components 'as' polymorphic prop to render it as selected html tag.
but I keep get this error :
No overload matches this call. Overload 1 of 2, '(props: Omit<Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "key" | keyof HTMLAttributes<...>> & { ...; } & { ...; }, never> & Partial<...>, "theme"> & { ...; } & { ...; }): ReactElement<...>', gave the following error. Type 'string' is not assignable to type 'undefined'.
I found in @types/styled-component that 'as' props can be 'never | undefined', and my variantsMap returns string type.
But I really want to use this 'as' prop with my variant-specific html tag selection feature.
Is there any way to solve this problem?
const variantMap = {
h1: 'h1',
h2: 'h2',
h3: 'h3',
h4: 'h4',
h5: 'h5',
h6: 'h6',
subheading1: 'h6',
subheading2: 'h6',
body1: 'p',
body2: 'p',
};
export const Typography = ({ variant : string }) => {
const selectedComponent = variantMap[variant];
return (<TypographyRoot
as={selectedComponent}
variant={variant}
{...props}
>
{children}
</TypographyRoot>);
}