Javascript super()上的Typescript错误:应为1-2个参数,但得到0
我不知道我需要做什么来解决此TS错误:Javascript super()上的Typescript错误:应为1-2个参数,但得到0,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我不知道我需要做什么来解决此TS错误: import React, { Component } from 'react'; import ReactDOM from 'react-dom'; declare const Ink: any; export default class TableOfContents extends Component <{ id: string }, { rendered: boolean }> { constructor() {
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
declare const Ink: any;
export default class TableOfContents extends Component <{ id: string }, { rendered: boolean }> {
constructor() {
super();
this.state = { rendered: false };
}
import React,{Component}来自'React';
从“react dom”导入react dom;
声明常量墨水:任何;
导出默认类TableOfContents扩展组件{
构造函数(){
超级();
this.state={rendered:false};
}
更新:
我觉得放在这里不舒服,应该是什么?
React组件的构造函数在装入之前被调用。
在实现React.Component
子类的构造函数时,您需要
应该在任何其他语句之前调用super(props)
。否则,
此.props将在构造函数中未定义,这可能导致
虫子
所以,你需要写:
constructor(props) {
super(props);
this.state = { rendered: false };
}
您还可以为道具和状态定义接口,以修复所有TS警告和更好的intellisense:
interface IProps {
id: string
}
interface IState {
rendered: boolean
}
export default class TableOfContents extends Component<IProps, IState> {
constructor(props: IProps) {
super(props)
this.state = {
rendered: false,
}
}
}
接口IProps{
id:字符串
}
界面状态{
渲染:布尔值
}
导出默认类TableOfContents扩展组件{
建造师(道具:IProps){
超级(道具)
此.state={
结果:错误,
}
}
}
如果您的编辑器抱怨并且不接受隐式任何
,您可以在tsconfig文件中设置“noImplicitAny”:false