简体   繁体   English

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

[英]How to resolve "Definition for rule '@typescript-eslint/rule-name' was not found"

I'm trying to add TypeScript compilation to an existing Javascript project.我正在尝试将 TypeScript 编译添加到现有的 Javascript 项目中。

AFAIK this is supposed to be possible (even easy), and you can incrementally spread TS through the codebase.据我所知,这应该是可能的(甚至很容易),并且您可以通过代码库逐步传播 TS。 Unfortunately this isn't what I'm seeing - after adding typescript and trying to start the project, I get this error for every single file in the project:不幸的是,这不是我所看到的——在添加 typescript 并尝试启动项目后,项目中的每个文件都会出现此错误:

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

在此处输入图像描述

There is no instance of the string rule-name anywhere in my source code.在我的源代码中的任何地方都没有字符串rule-name的实例。

This is my tsconfig.json :这是我的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"
  ]
}

I have tried adding "checkJs": false to the config and restarting but that doesn't change anything.我尝试将"checkJs": false添加到配置中并重新启动,但这并没有改变任何东西。

I haven't been able to track down anything online about this "rule-name" problem.我无法在网上找到有关此“规则名称”问题的任何信息。 It really looks like this is a placeholder text of some sort, but I can't find the source.看起来这确实是某种占位符文本,但我找不到来源。

Can anybody suggest anything that might help me get this project to build?任何人都可以提出任何可以帮助我构建这个项目的建议吗?

The error that you have is coming from ESLint and means that you have a rule in your .eslintrc which doesn't exist in any of your plugins.您遇到的错误来自 ESLint,这意味着您的.eslintrc有一条规则,该规则在您的任何插件中都不存在。 This could have happened if you followed the usage guide for @typescript-eslint/eslint-plugin but didn't replace the placeholder rule-name with an actual rule name as seen here https://www.npmjs.com/package/@typescript-eslint/eslint-plugin如果您遵循@typescript-eslint/eslint-plugin的使用指南,但没有将占位符rule-name替换为实际规则名称,则可能会发生这种情况,如此处所示https://www.npmjs.com/package/@打字稿-eslint/eslint-plugin

For those who do not want to just disable ESLint but make it work, running npm update could resolve the issue.对于那些不想仅禁用 ESLint 但使其工作的人,运行npm update可以解决问题。 In my case updating just @typescript-eslint/parser and @typescript-eslint/eslint-plugin was not enough but after npm update warnings dissapeared and ESLint was up and running.在我的情况下,仅更新@typescript-eslint/parser@typescript-eslint/eslint-plugin是不够的,但是在npm update警告消失并且 ESLint 启动并运行之后。

Also worth to keep in mind this higlight from package authors:同样值得记住的是包作者的这一亮点:

It is important that you use the same version number for @typescript-eslint/parser and @typescript-eslint/eslint-plugin. @typescript-eslint/parser 和 @typescript-eslint/eslint-plugin 使用相同的版本号很重要。

More detailed packages setup on npmjs .npmjs 上设置更详细的包。

If you're using react-app-rewired and customize-cra , you need to make sure you use disableEsLint from the latter;如果您使用react-app-rewireddisableEsLint customize-cra ,则需要确保使用后者的disableEsLint otherwise linting errors become typescript errors and prevent compilation.否则,linting 错误会变成打字稿错误并阻止编译。

This is because the rule is not declared in your eslint.json file Add the following line to your RULES in esling.json file这是因为该规则未在您的 eslint.json 文件中声明 将以下行添加到 esling.json 文件中的 RULES

"@typescript-eslint/object-curly-spacing" : "off" “@typescript-eslint/object-curly-spacing”:“关闭”

Refer the image below to know more参考下图了解更多

Refer this image to add the rule to eslint.json file参考此图片将规则添加到 eslint.json 文件

For me, it was enough to add plugin:@typescript-eslint/recommended in the.eslintrc file.对我来说,在 .eslintrc 文件中添加 plugin:@typescript-eslint/recommended 就足够了。

"extends": [
  ...<otherExtentions>...
  "plugin:@typescript-eslint/recommended"
]

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

相关问题 在 Storybook js 中找不到规则“@typescript-eslint/no-implicit-any”的定义 - Definition for rule '@typescript-eslint/no-implicit-any' was not found with Storybook js 加载规则“@typescript-eslint/dot-notation”时出错 - Error while loading rule '@typescript-eslint/dot-notation' 如何处理 catch(err) 块上的 @typescript-eslint/no-unsafe-member-access 规则? - How to handle the @typescript-eslint/no-unsafe-member-access rule on catch(err) blocks? (ESLint) 未找到规则“re​​act/jsx-sort-prop-types”的定义 - (ESLint) Definition for rule 'react/jsx-sort-prop-types' was not found eslint 为什么我的插件显示未找到规则定义? - eslint why does my plugin show Definition for rule was not found? 为什么Jquery selectiong这行的所有div具有class = rule-name? - Why is this line of Jquery selectiong ALL div's with the class=rule-name? Angular/typescript 中纯函数的 ESLint 规则 - ESLint rule for pure functions in Angular/typescript 如何在eslint规则中添加异常? - How to add exception to a eslint rule? eslint 缩进和@typescript-eslint/indent 冲突 - eslint indent and @typescript-eslint/indent conflict 如何在 VSCode 中的同一项目中的 .ts 文件和 .js 文件上运行 typescript-eslint - How to run typescript-eslint on .ts files and eslint on .js files in the same project in VSCode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM