react项目中typeScript提示:类型“Readonly<{}>”上不存在属性“count”。ts(23

源代码如下:

class NewPage extends React.Component {
  constructor(props:any) {
    super(props);
    this.state = {
      count: 1
    }
  }
  render() {
    return (
      <div>{this.state.count}</div>  // 此处提示类型“Readonly<{}>”上不存在属性“count”
    )
  }
}
export default NewPage

解决方式:

给第一行代码修改为:

class NewPage extends React.Component<any, any> {
   
   

缺点:失去对类型的检测,typeScript就没有意义了,不太建议使用。

猜你喜欢

转载自blog.csdn.net/u010234868/article/details/124408739