Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript typing issue with themes when using "Bundler" module resolution #3092

Closed
kyvong opened this issue Aug 14, 2023 · 0 comments · Fixed by #3101
Closed

Typescript typing issue with themes when using "Bundler" module resolution #3092

kyvong opened this issue Aug 14, 2023 · 0 comments · Fixed by #3101

Comments

@kyvong
Copy link
Contributor

kyvong commented Aug 14, 2023

Current behavior:
When using Typescript 5 with "moduleResolution": "Bundler", the typing
for the css prop is not working properly, particularly with the theme type.
I have followed the documentation to override the Theme type including
using the following directive:

/// <reference types="@emotion/react/types/css-prop" />

Typescript complains with an error like:

ERROR in src/App.tsx:20:20
TS7006: Parameter 'theme' implicitly has an 'any' type.
    18 |   return (
    19 |     <div className={appClassName}>
  > 20 |       <Title css={(theme) => ({ color: theme.headerColor })}>
       |                    ^^^^^
    21 |         Emotion Issue Template
    22 |       </Title>
    23 |

To reproduce:

  1. Clone this repo. The repo is based off the CodeSandbox
    issue template.
  2. Run npm install && npm run start

Expected behavior:
There should be no type error. The type for the theme parameter should be the overridden one provided in the project.

The issue is resolved by making the following change in css-prop.d.ts

--- a/packages/react/types/css-prop.d.ts
+++ b/packages/react/types/css-prop.d.ts
@@ -1,6 +1,6 @@
 import {} from 'react'
 import { Interpolation } from '@emotion/serialize'
-import { Theme } from '.'
+import { Theme } from '@emotion/react'

It also looks like a similar change should be made to jsx-namespace.d.ts

--- a/packages/react/types/jsx-namespace.d.ts
+++ b/packages/react/types/jsx-namespace.d.ts
@@ -1,6 +1,6 @@
 import 'react'
 import { Interpolation } from '@emotion/serialize'
-import { Theme } from './index'
+import { Theme } from '@emotion/react'

Environment information:

  • Typescript version: 5
  • @emotion/react version: 11.11.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant