Skip to content

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

Closed

Description

@cduff

TS Template added by Matt Bierner (@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

Activity

  1. 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)[/+] on Dec 1, 2020
  2. removed their assignment
    on Dec 1, 2020
  3. RyanCavanaugh commented on Dec 3, 2020

    @RyanCavanaugh
    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

  4. kuasha420 commented on Dec 31, 2020

    @kuasha420

    Any progress made on this yet?

  5. nahtnam commented on Jan 12, 2021

    @nahtnam

    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.

  6. advaiyalad commented on Jan 13, 2021

    @advaiyalad

    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.

  7. kuasha420 commented on Jan 13, 2021

    @kuasha420

    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. :/

  8. Paden commented on Jan 28, 2021

    @Paden

    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?

  9. cmlenz commented on Feb 1, 2021

    @cmlenz

    I think this is the same as #41920

  10. jmadelaine commented on Mar 17, 2021

    @jmadelaine

    Any updates on this?

  11. zackdotcomputer commented on Mar 22, 2021

    @zackdotcomputer

    Christopher Lenz (@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 :-\

  12. 10 remaining items

  13. advaiyalad commented on Jul 20, 2021

    @advaiyalad

    @nghiepit Are you using next.js?

  14. nghiepdev commented on Jul 20, 2021

    @nghiepdev

    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"]
    }
  15. kuasha420 commented on Jul 20, 2021

    @kuasha420

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

  16. Admiralfeb commented on Sep 19, 2021

    @Admiralfeb

    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.

  17. Shahaed commented on Sep 23, 2021

    @Shahaed

    Would love a response from the Typescript team on this

  18. andrewbranch commented on Sep 27, 2021

    @andrewbranch
    Member

    Looking into it 👍

  19. pvinis commented on Mar 23, 2022

    @pvinis

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

  20. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions