反应全局声明变量得到流类型错误

时间:2019-01-11 05:16:54

标签: javascript reactjs typescript flowtype

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

在这里,我使用Flow来对React类组件进行类型检查。 当我使用 npm运行流运行该文件时,我收到了类似的错误 无法为this.checkedItems分配数组文字,因为checkedItems [1]中缺少属性MyApp。 有人可以告诉我该如何解决流量错误

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

在代码中,您已经为道具定义了类型,但checkItems不在道具中。 您需要为类变量checkedItems

定义类型
interface IMyAppProps {
    prop1: boolean
    prop2: string
}
interface IMyAppState {
    state1: number
    state2: string
}

class MyApp extends Component<IMyAppProps,IMyAppState> {
    checkedItems: any //defined your class variable here with any type
    constructor(props) {
        super(props)
        this.checkedItems = []
    }
} 
相关问题