{"id":11950,"date":"2025-02-04T11:16:10","date_gmt":"2025-02-04T11:16:10","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11950"},"modified":"2025-02-04T11:16:10","modified_gmt":"2025-02-04T11:16:10","slug":"typescript-issue-with-styled-components-css-prop-and-storybook","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/typescript-issue-with-styled-components-css-prop-and-storybook","title":{"rendered":"TypeScript Issue with styled-component&#8217;s &#8220;css&#8221; Prop and Storybook"},"content":{"rendered":"<p>The issue arises because both @emotion\/core (used internally by Storybook) and styled-components define their own css prop, leading to a type conflict in TypeScript. When we import from Storybook, TypeScript tries to merge the types, causing the incompatibility.<\/p>\n<p>Instead of using the css prop from styled-components directly, we can refactor our code to avoid the conflict altogether by using the styled API from styled-components. This approach is cleaner and sidesteps the need for additional type declarations.<\/p>\n<p>Here\u2019s a refactored version of our code that works seamlessly:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">import React from \"react\";\r\nimport styled from \"styled-components\";\r\nimport { storiesOf } from \"@storybook\/react\";\r\n\r\nexport default function App() {\r\n const H1 = styled.h1`\r\n   background: red;\r\n `;\r\n return &lt;H1&gt;Hello world!&lt;\/H1&gt;;\r\n}\r\n<\/pre>\n<p><img decoding=\"async\" class=\"size-full wp-image-11952 alignleft\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/04111222\/output.png\" alt=\"output\" width=\"821\" height=\"90\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/04111222\/output.png 821w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/04111222\/output-300x33.png 300w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/02\/04111222\/output-768x84.png 768w\" sizes=\"(max-width: 821px) 100vw, 821px\" \/><\/p>\n<ol>\n<li><strong>Avoiding the css Prop:<\/strong> By using the styled API instead of the css prop, we can eliminate the need to explicitly declare css as a prop for DOMAttributes. This avoids conflicts with @emotion\/core.<\/li>\n<li><strong>Scoped Styles:<\/strong> The styled API encapsulates styles within the component, making it easier to maintain and debug.<\/li>\n<\/ol>\n<p>If we still need to use the css prop for certain use cases, we can explore options like:<\/p>\n<ul>\n<li>Avoiding @emotion by ensuring Storybook doesn&#8217;t include conflicting packages in your project. This might involve configuring Storybook to avoid @emotion dependencies.<\/li>\n<li>Using babel-plugin-styled-components to ensure proper css prop usage if we&#8217;re using css from styled-components.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The issue arises because both @emotion\/core (used internally by Storybook) and styled-components define their own css prop, leading to a type conflict in TypeScript. When we import from Storybook, TypeScript tries to merge the types, causing the incompatibility. Instead of using the css prop from styled-components directly, we can refactor our code to avoid the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11951,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11950"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11950"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11950\/revisions"}],"predecessor-version":[{"id":11953,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11950\/revisions\/11953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11951"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}