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

JSX import fixes do not support new React JSX Transform (without react import) #41762

Closed
cduff opened this issue Dec 1, 2020 · 26 comments · Fixed by #46368
Closed

JSX import fixes do not support new React JSX Transform (without react import) #41762

cduff opened this issue Dec 1, 2020 · 26 comments · Fixed by #46368
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@cduff
Copy link

cduff commented Dec 1, 2020

TS Template added by @mjbvz

TypeScript Version: 4.1.2

Search Terms

  • react
  • jsx

  • VSCode Version: 1.51.1
  • OS Version: Windows 10 Pro

React 17 introduced a new JSX transform which does not require react to be imported in every file. Support for this was added in TypeScript 4.1. However vscode is still suggesting/requiring the react import before other JSX import fixes are suggested.

Steps to Reproduce:

  1. npx create-react-app my-app --template typescript
  2. cd my-app
  3. code --disable-extensions .
  4. Tell vscode to use the Workspace TypeScript version (4.1.2).
  5. Add a src/Test.tsx file with content export default function Test() { return <App />; }. It should show a problem Cannot find name 'App'..
  6. Click to show fixes. It unnecessarily suggests/requires import default 'React' from module "react" before it will suggest the desired import default 'App' from module "./App".

In short, you can't get help with JSX imports without littering your files with unnecessary react imports.

Does this issue occur when all extensions are disabled?: Yes

@cduff cduff changed the title JSX import refactoring does not support new React JSX Transform (without react import) JSX import fixes do not support new React JSX Transform (without react import) Dec 1, 2020
@mjbvz mjbvz transferred this issue from microsoft/vscode Dec 1, 2020
@mjbvz mjbvz removed their assignment Dec 1, 2020
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 3, 2020
@RyanCavanaugh
Copy link
Member

We're still working on exactly figuring out how the new helpful automatic import behavior should interact with TS's framework-agnostic view of JSX

@kuasha420
Copy link

Any progress made on this yet?

@nahtnam
Copy link

nahtnam commented Jan 12, 2021

One workaround I found is: Instead of pressing cmd+. to open the auto suggest panel, remove the last letter of the component and type it back. You should see the suggest panel come up with the component name instead of the import React from 'react' suggestion.

@advaiyalad
Copy link

I tried this workaround too, but when I am trying to fix all missing imports, this is a very annoying task. Instead, I import React from 'react'; and then fix all missing imports, and then it is as simple as removing the react import.

@kuasha420
Copy link

I tried this workaround too, but when I am trying to fix all missing imports, this is a very annoying task. Instead, I import React from 'react'; and then fix all missing imports, and then it is as simple as removing the react import.

I have a precommit hook that runs some modified version of that react-jsx codemod as part of lint staged. VERY UGLY solution but that's the best I could do atm. :/

@Paden
Copy link

Paden commented Jan 28, 2021

We're still working on exactly figuring out how the new helpful automatic import behavior should interact with TS's framework-agnostic view of JSX

Since JSX is no longer apart of a single framework, does this still apply?

@cmlenz
Copy link

cmlenz commented Feb 1, 2021

I think this is the same as #41920

@jmadelaine
Copy link

Any updates on this?

@zackdotcomputer
Copy link

@cmlenz If it is the same then I might have bad news because I pulled in the nightly with #41950 (the fix for #41920) and this issue is still present :-\

@zackdotcomputer
Copy link

@cmlenz I gleefully take it back. Updated and tried again with the latest nightly (4.3.0-dev.20210330) this issue appears to be resolved. Will test it out for a couple days to confirm.

@lucasousi
Copy link

Adding @zackdotcomputer answer, remember to configure the typescript version in your VSCODE

@zenVentzi
Copy link

image

Am I missing anything? Using 4.3.2 and still happening

@lexanth
Copy link

lexanth commented Jun 17, 2021

As noted in this comment it only works if you have "jsx": "react-jsx" or "jsx": "react-jsxdev" in your tsconfig - it doesn't work if you're using "jsx": "preserve".

I'm not sure how you'd get around that - ts would need some other way to work out that you're using the new transform.

@eschwartz
Copy link

So glad to have found this! And thank you @lexanth for the fix.

Just to clarify for anyone else stumbling on this thread, you need to a a jsconfig.json file to your project root:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "checkJs": true,
    "jsx": "react-jsx"
  }
}

You can then use CMD + . to fix a missing import:

2021-06-18 10 30 39
2021-06-18 10 31 06

This has been driving me nuts forever, so glad to have found a solution here!

@Elia-Darwish
Copy link

Elia-Darwish commented Jun 18, 2021

Maybe it’s also worth mentioning for those who are using Naxt.js that this solution will not work for them . Next.js uses "jsx": "preserve" and it can’t be changed

@nghiepdev
Copy link

Any update for Next.js? Using 4.3.4 and still happening 😭

@eliasjnior
Copy link

eliasjnior commented Jun 24, 2021

Any update for Next.js? Using 4.3.4 and still happening 😭

For now, what I did is remove "react/react-in-jsx-scope": "off", from my eslint :/

One thing I noticed is this extension recommend the correct imports with CMD + space (or Ctrl + space on Windows)

@tylersayshi
Copy link

@nghiepit does v4.3.5 resolve the issue?

#44724

@nghiepdev
Copy link

@tylerjlawson I'm working on v4.3.5 and the problem is still.
Am I missing something?

image

@advaiyalad
Copy link

@nghiepit Are you using next.js?

@nghiepdev
Copy link

@PythonCreator27 Yes. I'm using Next.js
This is my tsconfig.json and VSCode 1.58.2

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "jsx": "preserve",
    "baseUrl": ".",
    "strict": true,
    "noEmit": true,
    "allowJs": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "isolatedModules": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", ".next"]
}

@kuasha420
Copy link

"jsx": "preserve", will not work here. But nextjs won't work without preserve. I think Nextjs needs to fix this on their side.

@Admiralfeb
Copy link

Any ideas why the TS engine doesn't pick up react if you set jsx to "preserve" or the reasoning behind the decision?

The handbook on it says it only affects the emit stage and does not affect type-checking.

If I'm working in other configurations and React is at v17, then I shouldn't have to have React imported in each file. If I have my own transpilation system, such as Next, Babel, etc. I'd still like to not have VSCode complaining that React isn't imported into the file.

@Shahaed
Copy link

Shahaed commented Sep 23, 2021

Would love a response from the Typescript team on this

@andrewbranch
Copy link
Member

Looking into it 👍

@andrewbranch andrewbranch added Bug A bug in TypeScript and removed Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Sep 29, 2021
@andrewbranch andrewbranch self-assigned this Sep 29, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.1 milestone Sep 29, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Oct 14, 2021
@pvinis
Copy link

pvinis commented Mar 23, 2022

what about "jsx":"react-native"? should there be a "jsx":"react-native-jsx" too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.