提问者:小点点

没有重载与此调用匹配。在typescript React中


我在React中编写了以下组件,我收到一个错误。感谢帮助。

主要组成部分:

export interface ICode {
code: (code: string) => void;

}导出默认类UserCode扩展React. Component{

state = {
    formFile: File,
    code: ""
}
//single
fileSelected(e: React.ChangeEvent<HTMLInputElement>) {
    var file = e.target.files;
    console.log(file);
    if (e.target.files)
        this.setState(
            { formFile: e.target.files[0] }
        )
}
aaa() {
    let value = (document.getElementsByClassName("user-code")[0] as HTMLInputElement).innerText;
    alert(value);
    console.log(value);
    return value;

}
updateCodeInFather() {
    this.props.code(this.state.code);
}
render() {
    return (
        <div>
            {/* <h6>Write your code here</h6> */}
            <Editor className="user-code"
                value={this.state.code}
                onChange={code => { this.setState({ code }); this.updateCodeInFather(); }}
                defaultLanguage="cpp"
                width={590}
                height={325}
                defaultValue={"void setup()\n{\n// put your setup code here, to run once:\n}\nvoid loop()\n{\n// put your main code here, to run repeatedly:\n}"}
            />
            <div>
                <input placeholder="browse" name="browse" id="browse" type="file" onChange={(e) => this.fileSelected(e)} accept=".txt, .ino" ></input>

            </div>

        </div>
    );
}
import Editor from './TagLanguage/Editor'
ReactDOM.render(
  <React.StrictMode>
    {
      <Editor></Editor>
    }
  </React.StrictMode>,
  document.getElementById('root')
);

出现错误:没有重载与此调用匹配。重载1 of 2,'(props: Readonly):UserCode',出现以下错误。属性'code'在类型'{}'中丢失,但在类型'Readonly'中需要。重载2 of 2,'(props:ICode,context?:any):UserCode',出现以下错误。属性'code'在类型'{}'中丢失,但在类型'Readonly'中需要。


共1个答案

匿名用户

需要更清楚地说明您希望如何使用ICode。如果您希望props和state都具有此接口,请考虑以这种方式声明Component:

export default class UserCode extends React.Component<ICode,ICode>

如果你只希望道具有这个界面:

 export default class UserCode extends React.Component<ICode,{}>