FlowType - Infer Generic Type on React Component

Viewed 1002

I have this following component

type PropTypes<T> = {
  items: T[],
  header: (item: T) => React.Element<*>,
  body: (item: T) => React.Element<*>,
}
type StateTypes<T> = {
  expandItem: ?T,
};

export default class Accordian<T> extends React.Component {
  props: PropTypes<T>;
  state: StateTypes<T>;
  setExpandItem: (expandItem: ?T) => void;

  constructor(props: PropTypes<T>) {
    super(props);
    ...
  }
}

I'd really like it to infer from items to know that the parameter of the header has the same type. Instead when I initialize the component with

<Accordian
    items={list}
    header={listItem => ...}
/>

I received:

-^ React element `Accordian`
20: export default class Accordian<T> extends React.Component {
                                   ^ T. This type is incompatible with. 
See: src/App/components/Accordian/index.js:20
19:   banks: GenericBank[],
             ^^^^^^^^^^^ object type

I've found a similar question for TypeScript, but I would like to know is it possible to infer the generic type on a React element's props in FlowType?

1 Answers
Related