如何用flow描述类的属性类型?

时间:2018-05-14 13:29:53

标签: reactjs flowtype

constructor(props) {
  super(props);
  this.state = {
    open: false,
  };
  this.input = null;
  this.handleChange = this.handleChange.bind(this);
}  

我在this.input = null

中遇到此错误
  

无法将null分配给this.input,因为属性input是。{   遗漏在MyComponent [1]

找不到流量文档的答案。

1 个答案:

答案 0 :(得分:0)

您可以在类定义的顶部定义实例属性的类型 -

class MyComponent extends React.Component {
  input: string,
  handleChange: (event: Object) => mixed,

  constructor(props) {
    super(props);
    this.state = {
      open: false,
    };
    this.input = null;
    this.handleChange = this.handleChange.bind(this);
  } 

}