最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Property '...' does not exist on type 'IntrinsicAttributes & Props' - Stack

programmeradmin0浏览0评论

I have a <InputField> ponent in my app with the following type definition for the props:

interface InputFieldProps extends React.HTMLAttributes<HTMLInputElement> {
  customProp: string;
}

My ponent looks like this:

const InputField: React.FC<InputFieldProps> = ({ customProp, ...htmlProps }) => {

  return (
    <input {...htmlProps} />
  );
};

I would expect that I can now pass the prop disabled or required to that ponent, as these properties are part of the HTMLInputElement type definition. However, I get the error:

Property 'disabled' does not exist on type 'IntrinsicAttributes & Props'

I tried passing disabled as disabled={true} as well as just disabled with no success. I can, however, pass placeholder as a prop. So some properties in the HTMLInputElement type definition seem to work, while others don't.

I have a <InputField> ponent in my app with the following type definition for the props:

interface InputFieldProps extends React.HTMLAttributes<HTMLInputElement> {
  customProp: string;
}

My ponent looks like this:

const InputField: React.FC<InputFieldProps> = ({ customProp, ...htmlProps }) => {

  return (
    <input {...htmlProps} />
  );
};

I would expect that I can now pass the prop disabled or required to that ponent, as these properties are part of the HTMLInputElement type definition. However, I get the error:

Property 'disabled' does not exist on type 'IntrinsicAttributes & Props'

I tried passing disabled as disabled={true} as well as just disabled with no success. I can, however, pass placeholder as a prop. So some properties in the HTMLInputElement type definition seem to work, while others don't.

Share Improve this question edited Dec 5, 2022 at 20:57 Youssouf Oumar 46.3k16 gold badges101 silver badges104 bronze badges asked Dec 5, 2022 at 20:19 DariusWDariusW 971 gold badge1 silver badge8 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Using React.InputHTMLAttributes<HTMLInputElement> is right. Just make sure any additional property, such as customProp, does not reach your input. In the below example, because customProp is destructed on its own, inputProps would include only input properties.

interface InputFieldProps extends React.InputHTMLAttributes<HTMLInputElement> {
  customProp: string;
}

const InputField: React.FC<InputFieldProps> = ({
  customProp,
  ...inputProps
}) => {
  return <input {...inputProps} />;
};

发布评论

评论列表(0)

  1. 暂无评论