繁体   English   中英

如何解决:“找不到规则'@typescript-eslint/consistent-type-assertions'的定义”

[英]How to resolve: "Definition for rule '@typescript-eslint/consistent-type-assertions' was not found"

我的 React 应用程序没有因为错误而无法编译:未找到规则“@typescript-eslint/consistent-type-assertions”的定义

我有点不知所措。 我无法在项目中找到任何当前规则定义。 该项目最初正在编译。 在我重组应用程序后开始出现错误。

该应用程序无法编译并出现错误:未找到规则“@typescript-eslint/consistent-type-assertions”的定义

包装.json -

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/react-dom": "16.9.0",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-router-dom": "^5.0.1",
    "react-scripts": "3.1.1",
    "styled-components": "^4.3.2",
    "typescript": "^3.5.3"
  },
  "scripts": {
    "start": "PORT=3005 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --transformIgnorePatterns",
    "e2e": "./node_modules/.bin/cypress open"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@testing-library/dom": "^6.1.0",
    "@testing-library/jest-dom": "^4.0.1",
    "@testing-library/react": "^9.1.3",
    "@types/jest": "^24.0.18",
    "@types/lodash": "^4.14.137",
    "@types/node": "^12.7.2",
    "@types/react": "^16.9.2",
    "@types/react-router-dom": "^4.3.4",
    "@types/styled-components": "^4.1.18",
    "babel-jest": "^24.9.0",
    "cypress": "^3.4.1"
  }
}

只需更新react-scripts 我通过卸载并再次安装解决了它。 在命令行中:

卸载:

npm uninstall react-scripts --save

并再次安装:

npm i react-scripts

当我遇到这个问题时,我使用create-react-app my-app --typescript创建了一个全新的应用程序,然后从我刚刚完成的另一个项目中复制了依赖项。 在搞砸了一些事情之后,我注意到react-scripts的版本是3.1.1 ,当我终于再次运行它时,工作的package.json让它在3.1.2看起来像是罪魁祸首。 一个工作的package.json看起来像:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@types/jest": "24.0.18",
    "@types/node": "12.7.5",
    "@types/react": "16.9.2",
    "@types/react-dom": "16.9.0",
    "node-sass": "^4.12.0",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-scripts": "3.1.2",
    "typescript": "3.6.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

试图让错误消失是一个挑战,Visual Studio 中存在一个邪恶的错误,它会让您的VSCompiler.exe和其他服务仅使用服务挂起时所拥有的文件版本挂起。 无论您做什么,在您重新启动这些设备之前什么都不会起作用,请参阅this answer on some Tips 如何解决它,我意识到它适用于 Angular 但同样的原则适用。

我找到了解决方案。 1) 删除 node_modules 2) 将 Package.Json 行的 React-scripts 更新为:""react-scripts": "3.1.2"" 3) 运行 ZBB30E854261B56DC584ABF444

这应该工作

yarn add -D @typescript-eslint/eslint-plugin@latest或者更准确yarn add -D @typescript-eslint/eslint-plugin@^2

我遇到了这个问题,因为插件位于@typescript-eslint/eslint-plugin@^1

我可以通过再次运行yarn install来解决问题。 如果这不起作用,您可以尝试完全删除 node_modules 文件夹并再次运行yarn install

我刚刚在我克隆的 create-react-app git 存储库中发现了这个确切的错误。 运行npm install然后npm start给了我同样的错误信息。

然后我意识到创建者一直在使用纱线而不是 npm。 所以我删除了 repo,重新克隆了它,然后运行yarn installyarn start 错误清除。

因此,这可能是 npm 与纱线问题。 特别是如果你之前安装过 create-react-app 将默认使用 yarn 如果继续使用 npm,可能会导致问题? 欣赏这是一种不同的情况,因为这是您自己的应用程序,但它可能是相关的。

诚然,我直接遇到了另一个错误,但至少我取得了进展......

我之前尝试了所有步骤,但是,真正解决我的问题的是这些步骤:

  • 1.- 删除 node_modules
  • 2.- 删除 package-lock.json
  • 3.- 更新 package.json 行的 React 脚本,如下所示:“react-scripts”:“3.1.2”
  • 4.- npm 安装(或 sudo npm 安装,如果您需要进一步授权)。

我希望它可以帮助别人。

对我来说,我应该使用 Yarn,但我盲目地使用 npm。

  1. 在 Package.json - 将反应脚本的版本更新为“反应脚本”:“3.1.2”
  2. npm 安装--保存
  3. npm启动

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM