首页 > 解决方案 > 如何用流描述类属性的类型?

问题描述

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

我有这个错误this.input = null

无法分配给nullthis.input因为[1]input中缺少属性MyComponent

在流文档上找不到答案。

标签: reactjsflowtype

解决方案


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

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);
  } 

}

推荐阅读