flow icon indicating copy to clipboard operation
flow copied to clipboard

React components as properties of an object are not checked

Open derek-duncan opened this issue 9 years ago • 1 comments

I ran into an interesting bug recently.

React components that are a property of an object are not checked by flow. However, when destructured from the object, flow finally picks them up. This is the obvious work around, but the bug is still tricky to debug because it silently fails.

Here's the sample code.

import React from 'react';

const localComponents: {
  StyledHeader: Class<React$Component<*, { color: string }, *>>,
} = {
  StyledHeader: class StyledHeader extends React.Component {
    render() {
      return <h1 style={{ color: this.props.color }}>Header</h1>;
    }
  },
};

// $ExpectError, but no error
<localComponents.StyledHeader />;
                                    
const { StyledHeader } = localComponents;
<StyledHeader color="#333" />;

I put together an example as well.

derek-duncan avatar Feb 06 '17 03:02 derek-duncan

If I update this example to Flow 0.53, it now gives the appropriate type error:'

import * as React from 'react';

const localComponents: {
  StyledHeader: Class<React.Component<{ color: string }, *>>,
} = {
  StyledHeader: class StyledHeader extends React.Component<*,*> {
    render() {
      return <h1 style={{ color: this.props.color }}>Header</h1>;
    }
  },
};

// Error about missing prop here
<localComponents.StyledHeader />;
                                    
const { StyledHeader } = localComponents;
<StyledHeader color="#333" />;

asolove avatar Aug 29 '17 18:08 asolove