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.

Declaring Theme interface for @emotion/react module breaks typings of returned values of styled and withComponent

See original GitHub issue

Current 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.0
  • emotion version: 11.0.0-next.10

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
Djiitcommented, Feb 20, 2020

@sassanh Worked like a charm. Thank you!

Note that I was able to do this, reusing my actual Theme typings :

import "@emotion/react";

import { Theme as EmotionTheme } from "./styles/theme";

declare module "@emotion/react" {
  export interface Theme extends EmotionTheme {}
}

3reactions
sassanhcommented, Feb 15, 2020

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:

import '@emotion/react';

declare module '@emotion/react' {
  export interface Theme {
    color: string;
  }
}

resolves both issues, importing @emotion/react on top of the file fixes the problem of ThemeProvider (and other exports of emotion) not being available.

And to fix theme being of type any you have to declare the theme completely in module declaration instead of declaring it in another file and import and re-exporting it here.

Read more comments on GitHub >

github_iconTop 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 >

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