簡體   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