Typescript support for extended Styled Components
See original GitHub issueHi,
I’ve been using the macro to extend components and have recently started also using Typescript and I’m getting errors from the tsc
when using extended components:
So, this works in regular JSX:
const PinkButton = tw(Button)`bg-pink`;
but when it becomes TSX then I get this error:
Argument of type 'FunctionComponent<Props>' is not assignable to parameter of type 'TemplateStringsArray'.
The workaround for this is to import styled
and call it like this:
const PinkButton = styled(Button)`${tw`bg-pink`}`;
Which is great for now but everywhere else I’m using tw.div
etc and would like to be able to consistently use tw
instead of having to import styled
when extending components.
https://github.com/ben-rogerson/twin.macro/pull/24 fixed other issues but there’s still this case.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:11 (8 by maintainers)
Top Results From Across the Web
How to properly extend a styled component using TypeScript
First extend ButtonAProps with an additional prop interface ButtonBProps extends ButtonAProps { b:string; } // ButtonB Component const ...
Read more >API Reference - styled-components
It works on both normal HTML tags as well as components, and supports everything any styled component supports, including adapting based on props,...
Read more >Using styled-components in TypeScript: A tutorial with examples
Here, you can learn how to build and style a TypeScript app using styled-components, with an e-commerce page as an example.
Read more >styled components extend typescript - CodeSandbox
TemplateReact Typescript; Environmentcreate-react-app. Files. public. src. App.tsx. index.tsx. styles.css. package.json. tsconfig.json. Dependencies.
Read more >Use styled-components with TypeScript | by Luxtars Dev
Styled Components has the ability to specify a theme with the help of ThemeProvider . You can later access the theme with ${props=>props.theme.main.something}...
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
Thanks for your thoughts on this Daro. I agree with your Tailwind alignment points and thanks for alerting me to the lack of documentation on the component wrapping. I use component wrapping on the regular and actually don’t go past a single level 😮 I’m planning to create some TypeScript demos in the future and it would be great to add typescript support for the feature without the workaround.
Edit: I’ve now documented the wrapping in feature in the readme.
I don’t really feel personally motivated to implement this. Firstly because it’s not documented(?), and ideologically, I feel like using this a lot would lead down an inheritance-heavy path over that favoring composition. An example in case I’m unclear:
There are ways around the diamond problem I’ve pointed out, but with the alternate path here, it’s something I don’t even have to think about, and I think it aligns more with Tailwind’s core philosophy.
That aside, if someone wants to pick up the mantle, I’ll gladly review the PR, provided the type tests are also updated