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

VS Code auto-import of React Components broken in VS Code (Next.JS + React 17 JSX + TypeScript incompatibility) #27212

Closed
mikelehen opened this issue Jul 15, 2021 · 10 comments
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.

Comments

@mikelehen
Copy link

What version of Next.js are you using?

10.2.3 (alse tested: 11.0.1)

What version of Node.js are you using?

12.22.1

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

n/a

Describe the Bug

When using Next.JS w/ TypeScript enabled, trying to auto-import react Components in VS Code incorrectly suggests adding import React from 'react' instead of the correct component import.

The fix from a TypeScript / VS Code perspective seems to be setting "jsx": "react-jsx" in your tsconfig.json (see here). But if you do this in your next.js app, then yarn dev will "helpfully" undo it:

$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
We detected TypeScript in your project and reconfigured your tsconfig.json file for you. Strict-mode is set to false by default.

The following mandatory changes were made to your tsconfig.json:

	- jsx was set to preserve (next.js implements its own optimized jsx transform)

Expected Behavior

Using VS Code's auto-import functionality should work out of the box in a Next.JS + TypeScript app.

To Reproduce

  1. yarn create next-app --typescript (accept default my-app name)
  2. code my-app (to open VS Code).
  3. Open my-app/pages/index.tsx file.
  4. Add <MyApp /> anywhere within the Home component.
  5. When the red squiggly appears under MyApp, hover over it and click "Quick Fix..." (or just press ⌘. shortcut on macOS)

Result: It suggests: import default 'React' from module "react"
Expected: It should suggest: import default 'MyApp' from module "./_app"

Note: If you accept the first suggestion to add the React import, then triggering the quick fix a second time will suggest the correct import.

@mikelehen mikelehen added the bug Issue was opened via the bug report template. label Jul 15, 2021
@callmephil
Copy link

Does this also affect absolute imports and or aliases?
https://nextjs.org/docs/advanced-features/module-path-aliases

// tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/components/*": ["components/*"]
    }
  }
}

@n1xx1
Copy link

n1xx1 commented Sep 13, 2021

I think the correct approach here is to change to preserve only if you are using "tsx": "react".

https://github.com/vercel/next.js/blob/canary/packages/next/lib/typescript/writeConfigurationDefaults.ts

It's not optimal having to change your tsconfig.json everytime you restart the dev server.

@Admiralfeb
Copy link

From what I can glean:
TSConfig jsx - preserve will keep the same files and make only the jump from TS to JS, passing them to the next process (Nextjs) where they are processed to the final output.

This means that "preserve" should be fine, but TS/VSCode isn't handling it correctly.

@lslunis
Copy link

lslunis commented Oct 12, 2021

Looks like the underlying issue is in TypeScript: microsoft/TypeScript#41762

@Admiralfeb
Copy link

Typescript's design meeting over this: microsoft/TypeScript#46141

@jasonwilliams
Copy link

Looks like this has been fixed at TypeScript level microsoft/TypeScript#46368

@nratter
Copy link

nratter commented Nov 16, 2021

Hopefully released in Typescript 4.5?

@nratter
Copy link

nratter commented Nov 18, 2021

Fixed in Typescript 4.5.2! After upgrading, make sure you're using the correct version in VSCode. View -> Command Palette -> type in "Typescript" and select "Typescript: Select Typescript version" and then select 4.5.2. Hope this helps!

@balazsorban44
Copy link
Member

balazsorban44 commented Dec 7, 2021

@mikelehen could you check the suggested changes?

I am trying to reproduce, and it seems to work now, using TypeScript 4.5.2:

image

@balazsorban44 balazsorban44 added the TypeScript Related to types with Next.js. label Dec 7, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

8 participants