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

[React 19] Wrong @types/react* default version and build fails if the correct version is installed #65599

Open
mwskwong opened this issue May 10, 2024 · 5 comments
Labels
bug Issue was opened via the bug report template. create-next-app Related to our CLI tool for quickly starting a new Next.js application. Linting Related to `next lint` or ESLint with Next.js. TypeScript Related to types with Next.js.

Comments

@mwskwong
Copy link

mwskwong commented May 10, 2024

Link to the code that reproduces this issue

https://github.com/mwskwong/wrong-react-19-types

To Reproduce

  1. Run next build and observe the terminal output.

Current vs. Expected behavior

Current

When creating a new Next.js project, types/react@^18 and types/react-dom@^18 are used. These types are for React 18, not 19.

If we follow https://react.dev/blog/2024/04/25/react-19-upgrade-guide#installing to install the React 19 compatible @types/react and @types/react-dom (already done in the above repo), Next.js will believe @types/react is not installed when running next build:

It looks like you're trying to use TypeScript but do not have the required package(s) installed.
Installing dependencies

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files in your pages directory).


Installing devDependencies (npm):
- @types/react

In CI env, e,g. when deploying on Vercel, the build will straight up fail without the above patching

Expected

Build should be successful.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home
  Available memory (MB): 32493
  Available CPU cores: 20
Binaries:
  Node: 20.12.2
  npm: N/A
  Yarn: N/A
  pnpm: 8.14.1
Relevant Packages:
  next: 14.3.0-canary.54 // Latest available version is detected (14.3.0-canary.54).
  eslint-config-next: 14.3.0-canary.54
  react: 19.0.0-beta-4508873393-20240430
  react-dom: 19.0.0-beta-4508873393-20240430
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

create-next-app, Linting, TypeScript

Which stage(s) are affected? (Select all that apply)

next build (local), Vercel (Deployed)

Additional context

No response

@mwskwong mwskwong added the bug Issue was opened via the bug report template. label May 10, 2024
@github-actions github-actions bot added create-next-app Related to our CLI tool for quickly starting a new Next.js application. Linting Related to `next lint` or ESLint with Next.js. TypeScript Related to types with Next.js. labels May 10, 2024
@mwskwong
Copy link
Author

mwskwong commented May 10, 2024

I suspect it is caused by the name of @types/react being changed to types-react in package.json and this affects how resolveFrom works.

export const resolveFrom = (

@patelvivekdev
Copy link

I am using react 19 with next canary 14.3.0-canary.58

  "@types/react": "18.2.74",
  "@types/react-dom": "18.2.23",

You can check the Portfolio and my Repo

@mwskwong
Copy link
Author

For now, a workaround is to patch next to remove its checking on whether @types/react exists during next build. I have to admit this workaround is quite hacky, but it will do the job before a proper fix is landed/React moves types-react back to @types/react.

diff --git a/node_modules/next/dist/lib/verify-typescript-setup.js b/node_modules/next/dist/lib/verify-typescript-setup.js
index 23998d8..da91cf4 100644
--- a/node_modules/next/dist/lib/verify-typescript-setup.js
+++ b/node_modules/next/dist/lib/verify-typescript-setup.js
@@ -72,11 +72,6 @@ const requiredPackages = [
         pkg: 'typescript',
         exportsRestrict: true
     },
-    {
-        file: '@types/react/index.d.ts',
-        pkg: '@types/react',
-        exportsRestrict: true
-    },
     {
         file: '@types/node/index.d.ts',
         pkg: '@types/node',

@mwskwong
Copy link
Author

I am using react 19 with next canary 14.3.0-canary.58

  "@types/react": "18.2.74",
  "@types/react-dom": "18.2.23",

You can check the Portfolio and my Repo

It will work for the most part, but again @types/react*@^18 is meant to be used with React 18, not 19. There are some differences in the API between the two versions.

e.g.

// initial value for useRef() is now mandatory for React 19, 
// but @types/react@^18 won't complain on that if it's missing
const ref = useRef(null);
// refs are now always mutable in React 19 due to the previous change in API, 
// but @types/react@^18 will complain about this line, since refs with initial values are read-only in React 18
ref.current = newValue;

@joaopedrodcf
Copy link

I guess this is still something that was tackled in #65058 and needs to be to make us able to use react 19 beta in nextjs canary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. create-next-app Related to our CLI tool for quickly starting a new Next.js application. Linting Related to `next lint` or ESLint with Next.js. TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants