emotion: Components created with CreateStyled type incorrectly expects a `theme` prop
Current behavior:
Recently upgraded to emotion 10 from 9, and ran into this issue with theme typing following the official recommended way to type themes using the CreateStyled type: https://emotion.sh/docs/typescript#define-a-theme
Here’s a minimal repro I created from the standard React TypeScript sandbox:
https://codesandbox.io/embed/emotion-bug-typescript-required-theme-prop-jqdo9

Property 'theme' is missing in type '{ children: Element; }' but required in type '{ theme: { primaryColor: string; }; }'.ts(2741)
To reproduce:
- Open the sandbox: https://codesandbox.io/embed/emotion-bug-typescript-required-theme-prop-jqdo9
- Click “Open in Editor” on the top right (for some reason type errors don’t show up in the default view)
- Hover over
<Themed>
Expected behavior:
No type errors. Themed doesn’t actually accept a required theme prop. theme is something that gets injected by the internal context consumer, not exposed as a part of the component API.
Environment information:
reactversion: 16.8.4emotionversion: 10.0.14
I made a temporary workaround here to unblock our upgrade: https://github.com/lewisl9029/emotion/pull/1/files
That addresses the symptom of the issue, but I had a really difficult type following the typings here and so I’m not sure about the ramifications of the change I made or whether it’s a good fix at all.
Let me know if there’s anything else I can do to help!
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 10
- Comments: 33 (18 by maintainers)
Commits related to this issue
- Convert Button to TS This required creating a custom styled instance because of https://github.com/emotion-js/emotion/issues/1434#issuecomment-540109011 — committed to Automattic/wp-calypso by sirbrillig 4 years ago
- Convert Button to TS This required creating a custom styled instance because of https://github.com/emotion-js/emotion/issues/1434#issuecomment-540109011 — committed to Automattic/wp-calypso by sirbrillig 4 years ago
- Convert Button to TS This required creating a custom styled instance because of https://github.com/emotion-js/emotion/issues/1434#issuecomment-540109011 — committed to Automattic/wp-calypso by sirbrillig 4 years ago
- composite-checkout: Convert to TypeScript (#45012) * Convert validation lib to typescript * Convert CheckoutProvider to typescript * Convert useFormStatus and CheckoutContext to TS * Convert... — committed to Automattic/wp-calypso by sirbrillig 4 years ago
OK, I updated to
@next, in case anyone else finds this thread in a similar situation, here is what I did:I removed a few things from
package.json:Then I replaced
emotionwith@emotionin.babelrcand changed theautoLabeloption fromtruetonever.Next, I replaced
@emotion/corewith@emotion/reactin the entire project.For Storybook, I created a custom
withThemeProviderdecorator and then used my custom decorator instead of thestorybook-addon-emotion-theme(I’m sure sooner or later that plugin will be updated):I had a custom
styled.tsxfile in my project in order to get Typescript autocompletion for my theme, that file is now deleted. Instead, I now have atypings/emotion.d.tsfile with the following content:Finally I replaced all my
import styled from '../../styled'imports with good oldimport styled from '@emotion/styled'.I think that was it… went pretty smoothly! ~45 minutes of work.
Same issue also when upgrading Typescript from 3.7 to 3.8, had to go back to 3.7.5 @mbrochh what version of TS are you using?
@mbrochh there are really not that many breaking changes. You can read through all changes by browsing
.mdfiles in the.changesetdirectory here: https://github.com/emotion-js/emotion/pull/1600/files?file-filters[]=.md@grsmvg well it looks like we’re doing the same thing and it’s still expecting a prop on my end. ¯_(ツ)_/¯
@grsmvg My issue is happening more when I’m defining a theme. Even though the theme is typed correctly it’s still expecting a theme prop. I’ve noticed this issue happens on any version of typescript that is greater than 3.1 +.
What I find interesting is that even if I type theme with an optional
themeprop typescript still yells at me.