Incorrect typings for withComponent()
See original GitHub issueVersion
2.2.3
Reproduction
I’m trying to pass Stateless Functional Component to withComponent()
method. Everything works fine, but typescript complains about its type. It seems that StyledComponentClass
interface is missing something like withComponent(element: StatelessComponent<P>): StyledComponentClass<P, T, O>;
(not very familiar with typescript though).
Steps to reproduce
- Pass Stateless Functional Component to withComponent() method.
Expected Behavior
Typescript not complaining about incompatible types.
Actual Behavior
Typescript complaining about incompatible types.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to correctly use TS typings with styled-components ...
I have troubles wrapping my head around using withComponent() method of a StyledComponent in a Typescript environment ...
Read more >API Reference - styled-components
Returns a new StyledComponent with the new tag / component being applied when it's used. Note. As of styled-components v4 the withComponent API...
Read more >Composing React Components with TypeScript - Pluralsight
The React typings package will allow you to import types from the react module that TypeScript will understand. Start by importing React in ......
Read more >Styled components, the styling library for your React apps you ...
There is really nothing wrong with the above, it is a viable approach even though some of ... Using the withComponent() method allows...
Read more >React with TypeScript Cheatsheet - Bits and Pieces
Typing useContext hook. The useContext hook type is usually inferred from the initial value you passed into the createContext() function as ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve run into this issue, or at least a similar one, with the latest version of styled-components.
If I have a styled span for example as so:
… and I use withComponent to use an anchor tag as so:
I get type errors when trying to use props like
href
.Compiling gives the following error:
It seems to be because in the typings
O = P
will set that to the same type (in this example, the type of intrinsic span). Then,withComponent
returns aStyledComponentClass
where the third generic passed isO
meaning ifO = P
in the parent then thewithComponent
result will have an intersected type with whatever that type was. In this case, span doesn’t have a prophref
so we receive the error above.This is tough because we use
styled-components
in our internal shared ui library with typescript. So far we’ve had to just cast the export to a type that works but that excludes us from ever having a proper type forO
where it doesn’t equalP
and having it pass down throughwithComponent
.@mbrowne @Igorbek I was hit with this bug today. I made a PR based on the work and discussion in #1283 and #1201 , I hope it is ok since they are not updated for a while.