【问题标题】:'autoComplete' does not exist on type 'IntrinsicAttributes & InputProps'类型“IntrinsicAttributes & InputProps”上不存在“autoComplete”
【发布时间】:2020-01-13 11:09:12
【问题描述】:

我正在使用带有 React 的 Typescript 并收到此错误:

Property 'autoComplete' does not exist on type 'IntrinsicAttributes & InputProps'.

我正在使用这样的组件:

        <Input
          value={email}
          id="email"
          type="email"
          label="email"
          autoComplete="email"
          placeholder="Joe@hotmail.com"
          handleChange={event => setName(event.target.value)}
        />

道具的打字稿是这样定义的:

type InputProps = {
  label: string;
  id: string;
  type?: "text" | "email" | "date" | "number" | "tel";
  helptext?: string;
  handleChange: (event) => void;
  value?: any;
  validationMessage?: string[] | string;
  validationType?:
    | "help"
    | "info"
    | "invalid"
    | "valid"
    | "validating"
    | "warning";
  otherProps: any;
};

任何多余的 HTML 属性都通过 otherProps 传递。

function Input({
  value = "",
  type = "text",
  handleChange,
  label,
  id,
  helptext,
  validationMessage,
  validationType,
  ...otherProps
}: InputProps) {

然后将 otherProps 放在输入上:

 <input value={value} onChange={handleChange} type={type} id={id} {...otherProps} />

【问题讨论】:

  • Input 组件来自哪里?而且,你可以控制type InputProps 吗?

标签: reactjs typescript


【解决方案1】:

React.InputHTMLAttributes&lt;HTMLInputElement&gt; 类型具有&lt;input&gt; 上可用道具的所有正确类型信息。您可以使用此类型扩展您的 InputProps,然后 autoComplete 属性将提供正确的类型信息。

【讨论】:

  • 谢谢!我得到了它与function Input({ value = "", type = "text", handleChange, label, id, helptext, validationMessage, validationType, ...otherProps }: InputProps &amp; React.InputHTMLAttributes) {
猜你喜欢
  • 2022-01-08
  • 2021-02-06
  • 2019-07-31
  • 1970-01-01
  • 2021-08-23
  • 2022-12-11
  • 2022-12-30
  • 2020-09-03
  • 2021-04-10
相关资源
最近更新 更多