[styled-components_v4.x.x] function is incompatible with array type
See original GitHub issueDo you want to request a feature or report a bug? Bug
What is the current behavior?
Cannot call styled.div because function [1] is incompatible with array type [2].
error while using styled-components with prop function
If the current behavior is a bug, please provide the steps to reproduce.
Using the styled-components ThemeProvider
, there is always a theme
prop available in all styled components. Something, similar to the following example renders properly, but Flow check fails.
const NewThing: React.AbstractComponent<{}> = styled.div(({ theme }) => {
return css`
background-color: ${theme.color.primary}
`;
});
After running a flow-check, the follow error render
Error βββββββββββββββββββββββββββββββββββββββββββββββββββββββ src/views/components/Foobar.jsx:22:58
Cannot call styled.div because function [1] is incompatible with array type [2].
src/views/components/Foobar.jsx
21β
[1] 22β const NewThing: React.AbstractComponent<{}> = styled.div(({ theme }) => {
23β return css`
24β background-color: ${theme.color.global.primary}
25β `;
26β });
27β
flow-typed/npm/styled-components_v4.x.x.js
[2] 185β [[call]]: <StyleProps, Theme>(string[], ...Interpolation<PropsWithTheme<StyleProps, Theme>>[]) => StyledComponent<StyleProps, Theme, V>;
What is the expected behavior?
This should pass flow check because it is valid styled-components behavior.
If this is a feature request, what is motivation or use case for changing the behavior?
Local Environment Information
- flow-bin
v0.109.0
- styled-components
v4.3.2
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
Is something complaining about the extra prop, or maybe a more general question would be: Why is it a problem? Iβm just curious. But regarding the Flow error:
I think itβs because for some reason Flow isnβt picking up that the component you passed in is actually a component? π€
Does something like this work?
I also think you might be able to achieve the same thing via a type-cast, but that would sacrifice readability, and you donβt get that much terseness out of it.
P.S: Havenβt tried this, but I guess it should work. Iβm also a bit confused though because I assumed Flow would pick up on the fact that your function should be compatible with
React.ComponentType<Props>
.Let me know if this works or doesnβt work, though. We can try something else in that case (and/or maybe fix the libdef if it can be improved).
Small comment regarding your typing of
NewThing
, I would do something like this:or to have Flow infer the instance automatically:
This way you get type-checking inside the template string literal (and the function version), and also type-checking of input props when using the styled component.