React global declare variables getting Flow type error

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

Here I am using the Flow for the type checking into the react class components. when I am running this file using npm run flow then I am getting the error like Cannot assign array literal to this.checkedItems because property checkedItems is missing inMyApp [1]. could someone tell me what should I do for this to solve flow error

2 answers

  • answered 2019-01-11 05:27 Krzysztof WoliƄski

    Well the runner is correct, there's no checkedItems property defined on the class. By passing Props as a type to the Component class, you just define the props type, you need to define checkedItems on the class yourself.

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

  • answered 2019-01-11 06:41 Ashish

    Here in your code, you have defined type for your prop, but checkedItems is not in props. You need to define type for class variable 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 = []
        }
    }