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.

Typescript support for extended Styled Components

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
ben-rogersoncommented, Apr 22, 2020

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.

2reactions
itsMapleLeafcommented, Apr 17, 2020

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:

// these are only a few classes, but imagine they're way longer, since buttons generally are
const Button = tw.button`...`
const PinkButton = tw(Button)`bg-pink`
const UnderlineButton = tw(Button)`underline`
const PinkUnderlineButton = tw(/* ??? */)` ??? `

// I'd prefer this
const pinkButtonStyle = `bg-pink`
const underlineButtonStyle = `underline`

// then apply them to any element/component
// don't have to make new styled components, or fool around with 'as' prop
<button css={pinkButtonStyle} />
<a css={underlineButtonStyle} />
<Link to="/" css={[pinkButtonStyle, underlineButtonStyle]} />

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

Read more comments on GitHub >

github_iconTop 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 >

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