如何解决“未找到规则‘@typescript-eslint/rule-name’的定义”

Ale*_*lan 4 javascript build typescript

我正在尝试将 TypeScript 编译添加到现有的 Javascript 项目中。

AFAIK 这应该是可能的(甚至很容易),您可以通过代码库逐步传播 TS。不幸的是,这不是我所看到的 - 添加打字稿并尝试启动项目后,项目中的每个文件都出现此错误:

Definition for rule '@typescript-eslint/rule-name' was not found

在此处输入图片说明

rule-name我的源代码中的任何地方都没有该字符串的实例。

这是我的tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "noImplicitAny": false,
    "sourceMap": true,
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "esModuleInterop": true,
    "jsx": "react",
    "skipLibCheck": true
  },
  "include": [
    "src",
    "types"
  ],
  "exclude": [
    "node_modules",
    "build"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我曾尝试添加"checkJs": false到配置并重新启动,但这并没有改变任何东西。

我一直无法在网上找到有关此“规则名称”问题的任何信息。看起来这确实是某种占位符文本,但我找不到来源。

任何人都可以提出任何可以帮助我建立这个项目的建议吗?

小智 9

您遇到的错误来自 ESLint,这意味着您的.eslintrc任何插件中都不存在规则。如果您遵循使用指南@typescript-eslint/eslint-plugin但没有rule-name用实际规则名称替换占位符,则可能会发生这种情况,如此处所示https://www.npmjs.com/package/@typescript-eslint/eslint-plugin


小智 7

对于那些不想仅仅禁用 ESLint 而是想让它工作的人来说,运行npm update可以解决问题。就我而言,仅仅更新@typescript-eslint/parser@typescript-eslint/eslint-plugin不够,但在npm update警告消失并且 ESLint 启动并运行后。

还值得记住包作者的这一要点:

@typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 使用相同的版本号非常重要。

npmjs上更详细的包设置。


Dav*_*vid 7

对我来说,在 .eslintrc 文件中添加 plugin:@typescript-eslint/recommulated 就足够了。

"extends": [
  ...<otherExtentions>...
  "plugin:@typescript-eslint/recommended"
]
Run Code Online (Sandbox Code Playgroud)


Ale*_*lan -2

如果您使用react-app-rewiredand customize-cra,则需要确保使用disableEsLint后者;否则 linting 错误会变成打字稿错误并阻止编译。

  • 恕我直言,这就是 linting 错误的全部要点。摆脱 lint,或更改配置,而不是禁用它。 (3认同)