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.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:30
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nrattercommented, 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!

1reaction
balazsorban44commented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto Import of React Components in Visual Studio Code [closed]
I'm using JavaScript (not TypeScript) and using the latest Visual Studio Code. I'm not able to get auto import of components working. I've...
Read more >
Vs code auto import Not Working !!! 100% Solved ... - YouTube
Vs code auto import Not Working !!! 100% Solved. ( React, Next JS,HTML,CSS).
Read more >
TSConfig Reference - Docs on every TSConfig option
This does not affect errors on the basis of code which appears to be ... Source map files are emitted as .js.map (or...
Read more >
VS Code Auto Import is bugging. Or I am stupid. - Reddit
Recently, every time I want to import a hook it brings this option. https://imgur.com/a/tRnxml8 BEFORE: import { useState } from "react"; ...
Read more >
April 2022 (version 1.67) - Visual Studio Code
explorer.fileNesting.patterns - Controls how files are nested. The default configuration provides nesting intelligence for TypeScript and JavaScript projects, ...
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