【问题标题】:React + TypeScript error: No overload matches this callReact + TypeScript 错误:没有重载匹配此调用
【发布时间】:2020-02-15 09:20:54
【问题描述】:

通过如下数组进行映射时,我收到一个严重错误:

// Home.tsx

  render() {
    const { inputs, outputs, expectedOutputs } = this.state;
    return (
        <ContentContainer>
          {inputs.map((input, i) => {
            return (
              <TestRow
                key={i}
                rowNumber={i}
                xml={inputs[i].xml}
                desc={inputs[i].desc}
                output={outputs[i]}
                expectedOutput={expectedOutputs[i]}
                handleTextAreaUpdate={this.handleTextAreaUpdate}
              />
            );
          })}
        </ContentContainer>
    );
// TestRow.tsx

interface TestRowProps {
  xml: string;
  desc: string;
  output: string;
  expectedOutput: string;
  rowNumber: number;
}

class TestRow extends Component<TestRowProps, {}> {
  textArea: any;

错误是:

没有重载匹配这个调用。 Overload 1 of 2, '(props: Readonly): TestRow',给出了以下错误。

    Type '{ key: number; rowNumber: number; xml: string; desc: string; output: never; expectedOutput: string; handleTextAreaUpdate: (e: { target: { value: string; }; }, column: number, rowNumber: number) => void; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<TestRow> & Readonly<TestRowProps> & Readonly<{ children?: ReactNode; }>'.
      Property 'handleTextAreaUpdate' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<TestRow> & Readonly<TestRowProps> & Readonly<{ children?: ReactNode; }>'.
  Overload 2 of 2, '(props: TestRowProps, context?: any): TestRow', gave the following error.
    Type '{ key: number; rowNumber: number; xml: string; desc: string; output: never; expectedOutput: string; handleTextAreaUpdate: (e: { target: { value: string; }; }, column: number, rowNumber: number) => void; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<TestRow> & Readonly<TestRowProps> & Readonly<{ children?: ReactNode; }>'.
      Property 'handleTextAreaUpdate' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<TestRow> & Readonly<DiagramProps> & Readonly<{ children?: ReactNode; }>'

。 TS2769

【问题讨论】:

  • Property 'handleTextAreaUpdate' does not exist on type 表示您的组件 TestRow 不期望一个名为 handleTextareaUpdate 的道具
  • 请提供TestRow的实现。好像根本没有任何道具。
  • 更新为包含界面
  • @yadejo 我需要将该道具添加到界面吗?
  • 似乎handleTextAreaUpdate 不存在于任何附加的渲染函数上。

标签: reactjs typescript


【解决方案1】:

在错误信息中,您可以找到问题所在。 Property 'handleTextAreaUpdate' does not exist on type 表示你应该在TestRowProps 接口中为handleTextAreaUpdate 定义一个属性。

【讨论】:

  • handleTextAreaUpdate: Function 至少摆脱了错误
  • Material UI 中的 Button 组件有同样的问题。它不接受 component 属性,因为看起来它没有被声明。这解决了问题(将组件属性添加到Buton.d.ts):component?: Function;
猜你喜欢
  • 2022-12-07
  • 2020-06-29
  • 2020-11-28
  • 2021-11-03
  • 2023-02-01
  • 2021-02-10
  • 1970-01-01
  • 2021-12-31
  • 2020-03-28
相关资源
最近更新 更多