我不知道我需要做什么来解决这个 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() {
super();
this.state = { rendered: false };
}
![enter image description here](https://i.sstatic.net/XyHL8.jpg)
更新 :
放在这里我感觉不太好,应该是什么?
![enter image description here](https://i.sstatic.net/Agrz2.jpg)
最佳答案
The constructor for a React component is called before it is mounted. When implementing the constructor for a
React.Component
subclass, you should callsuper(props)
before any other statement. Otherwise,this.props
will be undefined in the constructor, which can lead to bugs.
https://reactjs.org/docs/react-component.html#constructor
所以,你需要写:
constructor(props) {
super(props);
this.state = { rendered: false };
}
你还可以为你的 props 和 state 定义接口(interface)来修复所有 TS 警告和更好的智能感知: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,
}
}
}
如果你的编辑提示并且不接受隐含的 any
,可以设置"noImplicitAny": false
在 compilerOptions在 tsconfig 文件中。
关于javascript - super() : expected 1-2 arguments, 上的 typescript 错误但得到 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312030/