我有一个简单的锚标签组件,它扩展了原生<a>
标签。
我已经将我的打字稿接口定义为 extend React.HTMLAttributes<HTMLAnchorElement>
,但是当我尝试使用组件A
并传递类似的道具时rel
,target
我得到了 IntrinsicAttributes 错误。
如何正确扩展锚标签?
组件定义:
interface Props extends React.HTMLAttributes<HTMLAnchorElement> {
href: string;
className?: string;
}
export const A: FC<Props> = ({ href, className, children, ...rest }) => {
const baseClasses = "text-mb-green-200";
const classes = `${baseClasses} ${className ? className : ""}`;
return (
<a {...rest} href={href} className={classes}>
{children}
</a>
);
};
尝试使用:
<A {...rest} href={href} className={classes} rel={rel} target={target}>
{children}
</A>
TS 错误:
Type '{ children: ReactNode; href: string; className: string; target: string; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
Property 'rel' does not exist on type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.ts(2322)