styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.18k stars 2.48k forks source link

TS error when referencing styled component within `createGlobalStyle` #4203

Open delijah opened 7 months ago

delijah commented 7 months ago

System:

Reproduction

https://codesandbox.io/s/gifted-newton-q7fd9d

Steps to reproduce

When referencing a styled component within createGlobalStyle, i get a ts error

Expected Behavior

No type error

Actual Behavior

Type error

quantizor commented 7 months ago

The error in your linked sandbox is just because you added a non-optional prop and then didn't compose the prop.

delijah commented 7 months ago

Thx for your reply. What do you mean by "composing" the prop? Could you provide an example?

The thing is: when i use that component in the context of react, if i render that component, that prop must exist, but if i just want to use that styled component as a selector, why should i get notified about missing props? This is what i do not understand here.

minuz commented 4 months ago

@delijah he means this:

import { render } from "react-dom";
import styled, { createGlobalStyle } from "styled-components";
import React from "react";

const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

const Title = styled.h1<{ $color: string }>` // <= this is the mandatory custom prop
  font-size: 1.5em;
  text-align: center;
  color: ${({ $color }) => $color};
`;

const GlobalStyle = createGlobalStyle<{$color: string}>` // <= it need to be propagated here
  ${Title} {
    background: white;
  }
`;

// Render these styled components like normal react components.
// They will pass on all props and work
// like normal react components – except they're styled!
const App = () => (
  <Wrapper>
    <GlobalStyle $color='blue' /> // <= and consumed here
    <Title $color="palevioletred">
      Hello World, this is my first styled component!
    </Title>
  </Wrapper>
);

render(<App />, document.getElementById("root"));