简体   繁体   English

类型“IntrinsicAttributes & Props”上不存在属性“...”

[英]Property '...' does not exist on type 'IntrinsicAttributes & Props'

I have a <InputField> component in my app with the following type definition for the props:我的应用程序中有一个<InputField>组件,其道具类型定义如下:

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

My component 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 component, as these properties are part of the HTMLInputElement type definition.我希望我现在可以将 prop disabledrequired传递给该组件,因为这些属性是 HTMLInputElement 类型定义的一部分。 However, I get the error:但是,我收到错误:

Property 'disabled' does not exist on type 'IntrinsicAttributes & Props' “IntrinsicAttributes & Props”类型上不存在属性“已禁用”

I tried passing disabled as disabled={true} as well as just disabled with no success.我尝试将 disabled 传递为disabled={true}以及只是disabled但没有成功。 I can, however, pass placeholder as a prop.但是,我可以将placeholder作为道具传递。 So some properties in the HTMLInputElement type definition seem to work, while others don't.因此,HTMLInputElement 类型定义中的某些属性似乎有效,而其他的则无效。

Using React.InputHTMLAttributes<HTMLInputElement> and making sure every additional property, such as customProp does not reach your input .使用React.InputHTMLAttributes<HTMLInputElement>并确保每个附加属性(例如customProp都不会到达您的input In the below example, because customProp is destructed on its own, htmlProps would include only input properties.在下面的示例中,因为customProp是自行销毁的, htmlProps将只包含input属性。

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

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

编辑 angry-fast-upe615

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型 IntrinsicAttributes 和字符串 [] 上不存在属性“道具” - Property 'props' does not exist on type IntrinsicAttributes & string[] “IntrinsicAttributes &amp; MapContainerProps”类型上不存在属性“onClick” - Property 'onClick' does not exist on type 'IntrinsicAttributes & MapContainerProps' React forwardRef - 类型 IntrinsicAttributes 上不存在属性 - React forwardRef - Property does not exist on type IntrinsicAttributes 来自上下文提供者的道具:类型“ IntrinsicAttributes&InterfaceProps”上不存在属性“ xxx” - Props from contextual provider: Property 'xxx' does not exist on type 'IntrinsicAttributes & InterfaceProps' 将引用转发到 react.FC 给出类型错误:属性引用不存在于类型 &#39;IntrinsicAttributes &amp; Props &amp; { children :? 反应节点} - forwarding ref to react.FC gives type error: Property ref does not exist on type 'IntrinsicAttributes & Props & { children :? ReactNode} 如何解决“IntrinsicAttributes &amp; Function”类型上不存在的类型属性“”? - How to solve Type Property '' does not exist on type 'IntrinsicAttributes & Function'? 类型/ IntrinsicAttributes和IntrinsicClassAttributes上不存在React Typescript属性 - React Typescript property does not exist on type / IntrinsicAttributes & IntrinsicClassAttributes React 属性在类型“IntrinsicAttributes”(自定义钩子)上不存在 - React Property does not exist on type 'IntrinsicAttributes' (custom hook) React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes 在“IntrinsicAttributes”类型错误中不存在获取属性“ref” - Getting Property 'ref' does not exist on type 'IntrinsicAttributes' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM