I am new to typescript. I use a third party package (mostly irrelevant which package, this is more of a general typescript question) as such:
export const GenerateTitle: FunctionComponent = (): JSX.Element => {
return (
<div>
<span>TeSTING <Badge>Soon</Badge></span>
</div>
)
};
export const Patient: FunctionComponent = (): JSX.Element => {
...
<SubMenu icon={<GenerateTitle />} title={<GenerateTitle />} >
<MenuItem>About</MenuItem>
</SubMenu>
}
The code for the third party package shows the icon props and the title props having the same type
export type Props = React.LiHTMLAttributes<HTMLLIElement> & {
icon?: React.ReactNode;
title?: React.ReactNode;
...
onOpenChange?: (open: boolean) => void;
};
const SubMenu: React.ForwardRefRenderFunction<unknown, Props> = (
{
icon,
title,
...
onOpenChange,
...rest
},
ref,
) => {
return (
<li
{...rest}
>
<div>
{icon ? (
<span className="pro-icon-wrapper">
<span className="pro-icon">{icon}</span>
</span>
) : null}
{title ? <span className="pro-item-content">{title}</span> : null}
</div>
</div>
</li>
}
However, the icon prop renders the component just fine while I get an error on the title prop stating:
Type 'Element' is not assignable to type '(string & (boolean | ReactChild | ReactFragment | ReactPortal | null)) | undefined'.
Type 'Element' is not assignable to type 'string & ReactPortal'.
Type 'Element' is not assignable to type 'string'. TS2322
This is confusing me because it seems the typing and code to generate both the icon prop and the title prop are identical.