question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[styled-components_v4.x.x] function is incompatible with array type

See original GitHub issue

Do 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:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
omninonsensecommented, Feb 13, 2020

Ran into another one while trying to strip out props from being rendered as HTML attributes

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?

type DivProps = {
  // Update the types, or just use `{...}` In `ComponentType`
  animated: any,
  bsStyles: any,
  striped: any
  ...
}

const Div: React.ComponentType<DivProps> = ({ animated, bsStyle, striped, value, ...rest }) =>
  <div {...rest} />

// Careful with the theme here, it's technically `void`, not `{...}`, if there
// is no `ThemeProvider` somewhere up the component hierarchy.
const Bar: StyledComponent<{...}, void, *> = styled(Div)((props) => {
  // …
}

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).

1reaction
omninonsensecommented, Dec 12, 2019

Small comment regarding your typing of NewThing, I would do something like this:

import styled, {css, type StyledComponent} from 'styled-components';
// …
const NewThing: StyledComponent<{...}, Theme, HTMLDivElement> = styled.div(({ theme }) => {/* … */})

or to have Flow infer the instance automatically:

import styled, {css, type StyledComponent} from 'styled-components';
// …
const NewThing: StyledComponent<{...}, Theme, *> = styled.div(({ theme }) => {/* … */})

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Flow type checking to styled-components
I'm trying to get to grips with using styled-components with flow ... 'styled.div' because function [1] is incompatible with array type [2].
Read more >
Releases - styled-components
Highlights: styled-components is now written in TypeScript and ships its own types; stylis v4; tons of bug fixes; node 14+ now required.
Read more >
styled component cannot be used as a jsx component - You.com
You can use fragments to package an array of elements as a single element, ... The types returned by 'render()' are incompatible between...
Read more >
TypeError: "x" is not a function - JavaScript - MDN Web Docs
Maybe the object you are calling the method on does not have this function? For example, JavaScript Objects have no map function, but...
Read more >
styled-components - npm
Start using styled-components in your project by running `npm i ... styled-components allow you to write actual CSS code to style yourΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found