为什么 VS Code 从“hoist-non-react-statics/node_modules/@types/react”导入

Dig*_*eze 28 intellisense node-modules reactjs visual-studio-code

我最近将我的 React 存储库移至 下的单个 Mono 存储库中/client。我使用yarn install安装了node_modules/client

VS code intellisense 建议我在导入像和 之类的反应钩子时导入 fromhoist-non-react-statics/node_modules/@types/react而不是 from 。当我使用编译时这会导致以下错误reactuseEffectuseRefyarn start

找不到模块:无法解析“hoist-non-react-statics/node_modules/@types/react”

我的 package.json 中没有“hoist-non-react-statics”作为依赖项。我的 package.json 依赖项如下:

"dependencies": {
    "@auth0/auth0-react": "^1.6.0",
    "@hookform/resolvers": "^2.7.1",
    "@reduxjs/toolkit": "^1.6.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/qs": "^6.9.7",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "@types/react-redux": "^7.1.7",
    "@types/react-slider": "^1.3.1",
    "@types/react-table": "^7.7.2",
    "clsx": "^1.1.1",
    "final-form": "^4.20.2",
    "final-form-calculate": "^1.3.2",
    "lodash": "^4.17.21",
    "qs": "^6.10.1",
    "rc-checkbox": "^2.3.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-final-form": "^6.5.3",
    "react-hook-form": "^7.12.2",
    "react-redux": "^7.2.4",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "react-slider": "^1.3.1",
    "react-table": "^7.7.0",
    "react-truncate": "^2.4.0",
    "typescript": "~4.1.5",
    "yup": "^0.32.9"
  },
Run Code Online (Sandbox Code Playgroud)

这似乎是与反应相关的@types的问题。我在 github 存储库上提交了一个问题react-redux

yarn upgrade @types/react修复它。为了更好地衡量,所有软件包都应该升级,yarn upgrade --latest或者只是所有类型升级yarn upgrade --latest --pattern "@types"

https://github.com/reduxjs/react-redux/issues/1806

Hle*_*ess 17

降级/升级 VSCode 和 TypeScript 版本后,我发现最新的 TypeScript 版本会出现此问题(使用 4.4.2 进行测试,但可能存在于整个 4.4.x 分支中)。4.3.5中似乎不存在这个问题。您可以执行以下操作:

  • 将 VSCode 更新到最新版本
  • 确保您已安装 TS 4.3.5 或更早版本(全局或在工作区项目中)
  • 打开任何具有.ts扩展名的文件
  • 使用状态栏中的下拉菜单选择 TypeScript 版本。您可以选择 Workspace 版本或全局版本,而不是 VSCode 的预安装版本。

有关最后一步的说明,请参见下图: 在此输入图像描述


小智 6

我的打字稿项目也有同样的问题。使用 npm:

npm i @types/react@latest
Run Code Online (Sandbox Code Playgroud)

然后重启vscode