TS2339 (TS) 属性 'state' 类型不存在

TS2339 (TS) Property 'state' does not exist on type

我的 React 项目使用 Visual Studio 2019 和打字稿运行良好。 1 个月没有编码后,我打开 Visual Studio 解决方案,系统提醒我将打字稿 3.4 更新到 3.5。现在,我突然在组件的 this.state 定义上出现构建错误。

link

export default class Employee extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            user: null,
            loading: true
        }       
    }
}

如果您使用的是打字稿,则需要将其更改为声明状态接口:

interface IState = {
  user: any;
  loading: boolean;
}

然后 class 声明:

export default class Employee extends React.Component<any, IState> { }