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

@types/[email protected] + jsxImportSource: @emotion/react = Its return type 'ReactNode' is not a valid JSX element. #3049

Closed
karlhorky opened this issue Jun 4, 2023 · 8 comments · Fixed by #3048

Comments

@karlhorky
Copy link

karlhorky commented Jun 4, 2023

Current behavior:

Over the last months, there have been significant changes to the TypeScript @types/react package by @eps1lon in order to accommodate the different return types of components (one of the reasons being to avoid the problems with async React components)

Using "jsxImportSource": "@emotion/react" along with the new @types/[email protected] package causes failures that look like this:

'Title' cannot be used as a JSX component.
  Its return type 'ReactNode' is not a valid JSX element.
    Type 'undefined' is not assignable to type 'Element | null'.

CodeSandbox: https://codesandbox.io/p/sandbox/emotion-issue-template-forked-vsng2b?file=%2Fsrc%2FApp.tsx

Screenshot 2023-06-04 at 16 30 12

To reproduce:

  1. Fork the sandbox reproduction: https://codesandbox.io/s/emotion-issue-template-y306ue
  2. Upgrade all versions of all dependencies and devDependencies
  3. TypeScript error appears

Expected behavior:

"jsxImportSource": "@emotion/react" and the @types/[email protected] package are compatible

Environment information:

  • react version: 18.2.0
  • @emotion/react version: 11.11.0
@karlhorky
Copy link
Author

karlhorky commented Jun 4, 2023

Opened a PR with a potential fix here:

cc @Andarist happy to do any further changes to this as well, let's discuss in the PR if necessary

@naari3
Copy link
Contributor

naari3 commented Jun 5, 2023

As a temporary workaround until #3048 is merged, I think adding this type definition will work fine.

import { JSX } from '@emotion/react/jsx-runtime';

declare module '@emotion/react/jsx-runtime' {
  namespace JSX {
    type ElementType = React.JSX.ElementType;
  }
}

@akd-io
Copy link

akd-io commented Jun 5, 2023

It took me some time to find this issue. I think this is what's causing TypeScript errors in Next.js + Emotion projects created with my tool at create-next-stack.com.

When setting

compilerOptions: {
  ...
  "jsxImportSource": "@emotion/react"
}

in tsconfig.json

I get

./pages/_app.tsx:4:11
Type error: 'Component' cannot be used as a JSX component.
  Its element type 'string | number | boolean | Component<any, any, any> | ReactElement<any, string | JSXElementConstructor<any>> | ReactFragment | PromiseLikeOfReactNode | null | undefined' is not a valid JSX element.
    Type 'undefined' is not assignable to type 'Element | ElementClass | null'.

_app.tsx simply looks like this:

import type { AppProps } from "next/app";

export default function App({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

Dependencies:

@emotion/react 11.11.0     @types/react 18.2.8        eslint-config-next 13.4.4  react 18.2.0
@emotion/styled 11.11.0    @types/react-dom 18.2.4    next 13.4.4                react-dom 18.2.0
@types/node 20.2.5         eslint 8.42.0              next-plausible 3.7.2       typescript 5.1.3

Thank you guys for looking into this!

@rick-chou
Copy link

Failed to compile /examples/material-next-ts mui/material-ui#37500

@naari3 I use this workaround, but I got another error

JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists.

@naari3
Copy link
Contributor

naari3 commented Jun 6, 2023

@rick-chou Normally if you have @types/react, JSX.IntrinsicElements should be present, so perhaps you have a different problem than this issue is dealing with maybe?
Above all, I don't know what kind of environment you are in, so we can't be sure exactly what is going on.

@rick-chou
Copy link

rick-chou commented Jun 6, 2023

@rick-chou Normally if you have @types/react, JSX.IntrinsicElements should be present, so perhaps you have a different problem than this issue is dealing with maybe? Above all, I don't know what kind of environment you are in, so we can't be sure exactly what is going on.

thanks! I updated all the dependencies and used this workaround, and solved the problem.

@karlhorky
Copy link
Author

Thanks for the PR #3048 , review and merge @naari3, @Andarist, @emmatown ! 🙌

Looking forward to trying this out in @emotion/[email protected] after #3051 gets merged!

@akd-io
Copy link

akd-io commented Jun 7, 2023

Works like a charm! 🎉

mnvr added a commit to ente-io/photos-web that referenced this issue Feb 22, 2024
yarn upgrade "@emotion/react" "@emotion/server" "@emotion/styled" --latest

This is to pull in the bugfix for this issue:
emotion-js/emotion#3049
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants