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 React Typescript component auto-import and autocomplete not working. #45004

Closed
filiptrplan opened this issue Jul 13, 2021 · 6 comments
Closed
Assignees
Labels
Domain: Auto-import Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@filiptrplan
Copy link

filiptrplan commented Jul 13, 2021

Bug Report

🔎 Search Terms

vscode tsx autocomplete, vscode tsx auto import, vscode typescript react autocomplete

🕗 Version & Regression Information

I'm on Typescript version 4.3.5 and I have tried the nightly build too.
I'm on the latest VS Code version 1.58.0. It is a new install with all extensions disabled.
Doesn't work on either 1.56 or 1.57 too.

This seems to be a duplicate of #44657 but that issue is marked as closed even if it still persists.

💻 Code

Analytics.tsx

import React from 'react'

interface AnalyticsProps {

}

export const Analytics: React.FC<AnalyticsProps> = ({}) => {
    return (
        <div>
            
        </div>
    );
}

Sidebar.tsx

import React from 'react'
import './Sidebar.scss';

interface SidebarProps {

}

export const Sidebar: React.FC<SidebarProps> = ({ children }) => {
        return (
            <div className="Sidebar">
                {children}
            </div>
        );
}

Folder structure:
image
package.json

{
  "name": "analytics-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "chart.js": "^3.4.1",
    "primeflex": "^2.0.0",
    "primeicons": "^4.1.0",
    "primereact": "^6.5.0-rc.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "react-transition-group": "^4.4.2",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/react-router-dom": "^5.1.8",
    "node-sass": "5.0.0"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5", 
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ], 
    "allowJs": true, 
    "skipLibCheck": true, 
    "esModuleInterop": true, 
    "allowSyntheticDefaultImports": true, 
    "strict": true, 
    "forceConsistentCasingInFileNames": true, 
    "module": "esnext", 
    "moduleResolution": "node", 
    "isolatedModules": true, 
    "resolveJsonModule": true, 
    "noEmit": true, 
    "jsx": "react", 
    "sourceMap": true, 
    "declaration": true, 
    "noUnusedLocals": true, 
    "noUnusedParameters": true, 
    "incremental": true, 
    "noFallthroughCasesInSwitch": true 
  },
  "include": [
    "src/**/*",
    "src/**/*.d.tsx",
    "src/**/*.tsx" 
  ],
  "exclude": [
    "node_modules",
    "build"
  ] 
}

🙁 Actual behavior

I try to start typing the "Sidebar" component and the autocomplete doesn't suggest it anywhere. When I finish typing the component, the autoimport function shows up in the quick fix category.
Animation

🙂 Expected behavior

It should have suggested the "Sidebar" component and upon clicking enter, it should have imported it automatically.

@RyanCavanaugh RyanCavanaugh added Domain: Auto-import Needs Investigation This issue needs a team member to investigate its status. labels Jul 13, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.5.0 milestone Jul 13, 2021
@andrewbranch
Copy link
Member

When you’re inside the <div></div>, you’re typing plain old JSX text. We don’t offer any kind of completions or auto imports there because it’s basically the same thing as writing inside a string literal. Think about how annoying it would be if you were actually just trying to type the word “Sidebar” as part of some paragraph text and you kept auto-importing things as you typed. You have to type a < if you want completions for tag/component names.

Kapture.2021-07-13.at.11.19.12.mp4

@andrewbranch andrewbranch added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 13, 2021
@filiptrplan
Copy link
Author

filiptrplan commented Jul 13, 2021 via email

@filiptrplan
Copy link
Author

You have to type a < if you want completions for tag/component names.

Maybe this is also working as intended, but I'll ask anyway. The method where you type < and then the name of the component only works for modules in my src folder. If I include my node_modules folder, the TS language features say that the folder is too big.

Is the only option to get autocomplete from npm modules to include each folder separately, like so?:

  "include": [
    "src/**/*",
    "src/**/*.d.tsx",
    "src/**/*.tsx",
    "node_modules/primereact/*"
  ]

@andrewbranch
Copy link
Member

You shouldn’t have to do anything special to get auto imports from node_modules, but some libraries unknowingly use patterns that make it impossible for auto-imports to discover what’s available. Can you tell me what component you’re trying to import from what library?

@filiptrplan
Copy link
Author

filiptrplan commented Jul 14, 2021 via email

@andrewbranch
Copy link
Member

primereact does not have a main or types field in its package.json, so there’s no entrypoint where we can explore the package. The author should minimally add an index.d.ts file that imports or triple-slash references each component so that auto imports can find them from the package root.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Auto-import Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants