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] components loose component prop with emotion or styled-component

See original GitHub issue

When using styled components >=4.1.0 or emotion >=10 there will be type errors if material ui components are styled.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

No build or type errors are expected when styling material ui components with styled components or emotion.

Current Behavior 😯

A lot of type errors like

TS2740: Type '{ children: string; }' is missing the following properties from type 'Pick<Pick<(Pick<PaperProps, "style" | "title" | "children" | "className" | "color" | "id" | "lang" | "role" | "tabIndex" | "elevation" | "aria-activedescendant" | "aria-atomic" | ... 245 more ... | "innerRef"> & RefAttributes<...>) | PropsWithChildren<...>, "ref
" | ... 257 more ... | "innerRef"> & Partial<...>, "ref...': ref, style, title, className, and 254 more.

or

TS2322: Type '{ children: Element; component: string; }' is not assignable to type 'IntrinsicAttributes & Pick<Pick<DefaultComponentProps<{ props: { dense?: boolean | undefined; disablePadding?: boolean | undefined; subheader?: ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<...>)> | n
ull) | (new (props: any) => Component<...>)> | undefine...'.
  Property 'component' does not exist on type 'IntrinsicAttributes & Pick<Pick<DefaultComponentProps<{ props: { dense?: boolean | undefined; disablePadding?: boolean | undefined; subheader?: ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<...>)> | null) | (new (props: any) => Component<...>)
> | undefine...'.

Steps to Reproduce 🕹

  1. Checkout https://github.com/Mario-Eis/materialui-styled-test and npm install
  2. Run npm script ‘serve-dev’ --> localhost:3000
  3. Build errors are readable in the console.

I don’t know for sure, but I think there could be two different issues in the demo. The first is a simple styled Paper component. When downgrading to styled components <=4.0.3, this issue will be fixed. The second one is a styled List component. Styled List components will work, as long as there are no prop attributes set. If e.g. component="nav" is added, there will be an error. Downgrading to styled-components 4.0.3 won’t work here. But downgrading to material ui 3.9.2, will.

Both cases are shown in the demo.

Context 🔦

I want to style material ui components using styled-components library.

Your Environment 🌎

Tech Version
Material-UI 4.0.0
React 16.8.17
TypeScript 3.4.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
bengrycommented, Dec 2, 2019

@oliviertassinari why shouldn’t the workaround be part of MUI itself though?

7reactions
eps1loncommented, Jun 3, 2019
const StyledList = styled(List)`
    background-color: blue;
    text-align: center;
` as typeof List;

Seems codemodable. Though you have to be aware that any type information about props handled by styled is lost.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[typescript] components loose component prop with emotion ...
The first is a simple styled Paper component. When downgrading to styled components <=4.0.3, this issue will be fixed. The second one is...
Read more >
Omit specific prop from an emotion styled component?
My end goal is to deprecate the as prop, and rather encourage the usage of .withComponent (for Typescript reasons). reactjs · typescript ......
Read more >
TypeScript - Emotion
When using our JSX factory, TypeScript only allows the css prop on components that accept a className prop. This is because @emotion/react resolves...
Read more >
Tooling - styled-components
By adding a unique identifier to every styled component, this plugin avoids checksum mismatches due to different class generation on the client and...
Read more >
Why We're Breaking Up with CSS-in-JS - DEV Community ‍ ‍
styled -components and Emotion are the most popular CSS-in-JS libraries in ... For each element that uses the css prop, Emotion will render ......
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