首页 > 解决方案 > 反应全局声明变量得到流类型错误

问题描述

type Props = {
  checkedItems:any
};
class MyApp extends Component<Props> {
constructor(props) {
    super(props);
    this.checkedItems = [];
  }
 }
 
 

在这里,我使用 Flow 对 react 类组件进行类型检查。当我使用npm run flow运行此文件时,我收到错误,例如无法将数组文字分配给,this.checkedItems因为[1]checkedItems中缺少属性。MyApp有人可以告诉我我应该怎么做才能解决流量错误

标签: javascriptreactjstypescriptflowtype

解决方案


跑步者是正确的,checkedItems类上没有定义属性。通过Props作为类型传递给Component类,您只需定义props类型,您需要checkedItems自己在类上定义。

class MyApp extends Component<{}> {
checkedItems: any;

constructor(props) {
    super(props);
    this.checkedItems = [];
  }
 }

推荐阅读