Search code examples
javascriptreactjsflowtypecreate-react-app

React @flow avoid duplicate type for this


I'm using flow along with create-react-app. And sometimes it easy to use this.someFlag to setup some flags in constructor(props), but @flow yell on me. Temporary solution is using any type for this. Like so: (this: any).timerID = null; I don't really like it, because I have to use it everywhere and the code becomes whole mess. It's really not comfortable. Can I define this: any for current component somehow so I won't repeat it everytime ?


Solution

  • You can resolve this quite easily:

    class MyComponent extends Component {
      myThing: number;
    
      constructor() {
        super();
        this.myThing = 1;
      }
    
      render() {
        return <div>my component</div>
      }
    }
    

    you just need to define a type on the class.