【发布时间】:2022-12-11 19:32:16
【问题描述】:
我的应用程序中有一个 <InputField> 组件,其道具类型定义如下:
interface InputFieldProps extends React.HTMLAttributes<HTMLInputElement> {
customProp: string;
}
我的组件如下所示:
const InputField: React.FC<InputFieldProps> = ({ customProp, ...htmlProps }) => {
return (
<input {...htmlProps} />
);
};
我希望我现在可以将 prop disabled 或 required 传递给该组件,因为这些属性是 HTMLInputElement 类型定义的一部分。但是,我收到错误:
“IntrinsicAttributes & Props”类型上不存在属性“已禁用”
我尝试将 disabled 作为
disabled={true}以及disabled传递,但没有成功。但是,我可以将placeholder作为道具传递。因此 HTMLInputElement 类型定义中的某些属性似乎有效,而其他的则无效。
【问题讨论】:
-
乍一看,它看起来不错。也许试试
extends React.ComponentPropsWithoutRef<'input'>。
标签: javascript html reactjs typescript