Declaring Theme interface for @emotion/react module breaks typings of returned values of styled and withComponent
See original GitHub issueCurrent behavior:
When I add
declare module '@emotion/react' {
export {Theme} from 'somewhere';
}
typings for theme prop is correctly set, but whenever type of styled(Element)
becomes StyledComponent<any, {}>
. If I remove the above declaration, the type of styled(Element)
becomes
StyledComponent<Props & etc>
which is correct.
Also import {ThemeProvider} from '@emotion/react';
doesn’t work unless I add export {ThemeProvider} from '@emotion/react';
to the declaration.
To reproduce:
Add this declaration to your code:
declare module '@emotion/react' {
export {Theme} from 'somewhere';
}
Expected behavior:
Providing theme typing don’t break other stuff.
Environment information:
react
16.12.0emotion
version: 11.0.0-next.10
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
How to use emotion/styled with React and Typescript
Problem: props.theme is an empty object according to TS - palette does not exist on the Theme type (however the object I am...
Read more >TypeScript - Emotion
You can define a theme type by extending our type declarations via your own declarations file. emotion.d.ts. import '@emotion/react' declare module '@emotion ......
Read more >@emotion/styled | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >How to use styled components with Material UI in a React app
The 5 steps to mastery. 1. Use Material UI's JSS classes once for overriding Material UI original theme. Even though our objective is...
Read more >Renovate Bot Package Diff
+ declare module '@emotion/react' {. 28. + export interface Theme { ... This is what we use to provide TypeScript typings for object...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sassanh Worked like a charm. Thank you!
Note that I was able to do this, reusing my actual Theme typings :
By tracing the issue in styled-components to see how it’s resolved there, I found a workaround specially by reading https://github.com/styled-components/styled-components-website/issues/447
After some trial and error I found that declaring an
emotion.d.ts
(or whatever) file which contains:resolves both issues, importing
@emotion/react
on top of the file fixes the problem ofThemeProvider
(and other exports of emotion) not being available.And to fix
theme
being of typeany
you have to declare the theme completely in module declaration instead of declaring it in another file and import and re-exporting it here.