【问题标题】:Getting "No overload matches this call" error while converting code to TypeScript将代码转换为 TypeScript 时出现“没有重载匹配此调用”错误
【发布时间】:2021-09-25 10:15:24
【问题描述】:

我是 TypeScript 的新手,我已将我的所有代码(React Hooks)都转换为 TypeScript,除了我收到以下错误的一件事:

没有重载匹配此调用。 重载 1 of 2, '(props: { component: ElementType; } & { children?: ReactNode; color?: "primary" | "secondary" | undefined; disabled?: boolean | undefined; error?: boolean | undefined; 。 .. 6 more ...; variant?: "outlined" | ... 2 more ... | undefined; } & CommonProps & Pick): Element',给出了以下错误。 类型“{ children: Element;”中缺少属性“组件”;类名:字符串; htmlFor: 字符串; }' 但在类型 '{ component: ElementType; }'。 重载 2 of 2,'(props: DefaultComponentProps>): Element',给出了以下错误。 类型'{孩子:元素;类名:字符串; htmlFor: 字符串; }' 不可分配给类型 'IntrinsicAttributes & { children?: ReactNode;颜色?

问题来自这里:

import {
  Box,
  Button,
  TextField,
  FormControl,
  FormGroup,
  Select,
} from "@material-ui/core/";
import { Trans } from "react-i18next";

interface ConnectedLinkProps {
  onSubmit: string;
  classes: {
    formControl: string;
  };
}


<FormControl className={classes.formControl} htmlFor="source">
  <TextField
    id="source"
    disabled={true}
    value={linkSettings.source}
    label={<Trans i18nKey="form.linkForm.sourceNode">Source Node</Trans>}
  />
</FormControl>;

“FormControl line”的第一行是红色的。

【问题讨论】:

  • @T.J.Crowder 我添加了更多需要的代码和导入,你能帮忙吗?

标签: typescript


【解决方案1】:

如果您从 FormControl 组件中删除道具,错误应该会消失。

<FormControl>
  <TextField
    id="source"
    disabled={true}
    value="Something"
    label={<Trans i18nKey="form.linkForm.sourceNode">Source Node</Trans>}
  />
</FormControl>

FormControl 组件不支持 classNamehtmlFor 属性。

Link to a forked example on CodeSandbox.

共享代码的工作示例如下所示:

import React from "react";
import {
  Box,
  Button,
  TextField,
  FormControl,
  FormGroup,
  Select,
  InputLabel
} from "@material-ui/core/";
import { Trans } from "react-i18next";

interface ConnectedLinkProps {
  onSubmit: string;
  classes: {
    formControl: string;
  };
}

const App: React.FC<ConnectedLinkProps> = ({ onSubmit, classes }) => {
  return (
    <div className="App">
      <FormControl>
        <InputLabel htmlFor="source">Something</InputLabel>
        <TextField
          id="source"
          disabled={true}
          value="Something"
          label={<Trans i18nKey="form.linkForm.sourceNode">Source Node</Trans>}
          className={classes?.formControl}
        />
      </FormControl>
    </div>
  );
};

export default App;

【讨论】:

  • 但是我需要的那些道具和它正在使用'js'但是当转换为'tsx'时我得到了那个错误
  • 在这种情况下,它应该类似于this。它将在 JS 中工作,因为 JS 不会从 MUI 库中检查适当的类型。
  • 你能用我的代码给出工作示例,所以我可以接受吗?
  • 我应该将 'htmlFor="id"' 添加到 'TextField' 吗?
  • htmlFor 仅在 InputLabel 组件上可用。
猜你喜欢
  • 2022-12-07
  • 2021-09-13
  • 2020-06-29
  • 2020-02-15
  • 2023-01-04
  • 2020-11-28
  • 2020-08-24
  • 2021-11-17
相关资源
最近更新 更多