flow
flow copied to clipboard
React components as properties of an object are not checked
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" />;
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" />;