TS2339(TS)属性“状态”在类型上不存在

时间:2019-08-28 11:37:08

标签: reactjs typescript visual-studio-2019

我使用Visual Studio 2019和打字稿让我的React项目正常运行。在没有编码的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
        }       
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

然后执行类声明:

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