Components created with CreateStyled<Theme> type incorrectly expects a `theme` prop
See original GitHub issueCurrent 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:
react
version: 16.8.4emotion
version: 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!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:33 (18 by maintainers)
Top GitHub Comments
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
emotion
with@emotion
in.babelrc
and changed theautoLabel
option fromtrue
tonever
.Next, I replaced
@emotion/core
with@emotion/react
in the entire project.For Storybook, I created a custom
withThemeProvider
decorator 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.tsx
file in my project in order to get Typescript autocompletion for my theme, that file is now deleted. Instead, I now have atypings/emotion.d.ts
file 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?