停止在不使用 Typescript 的 vue 组件上应用 typescript-eslint/explicit-module-boundary-types

Léo*_*oco 11 typescript vue.js typescript-eslint

我正在使用vue,我刚刚更新了@typescript-eslint/eslint-plugin": "^3.10.1". 我的项目包含几个组件。其中一些正在使用,javascript而另一些正在使用typescript

警告

我对 typescript组件内部的方法有这个警告

警告:缺少函数的返回类型(@typescript-eslint/explicit-module-boundary-types)

我怎么能不在.vue没有使用的文件上应用这个规则<script lang="ts">

Reu*_*iro 17

因此,此规则的目的是确保导出的模块对外部使用它的任何人都有一个清晰的界面。从规则页面来看,使用for .tsfiles而不是.vuefiles似乎是有道理的,但是你可以根据你的项目需求来判断。

对于我的项目,我使用了建议的overrides配置

{
  "rules": {
    // disable the rule for all files
    "@typescript-eslint/explicit-module-boundary-types": "off"
  },
  "overrides": [
    {
      // enable the rule specifically for TypeScript files
      "files": ["*.ts", "*.tsx"],
      "rules": {
        "@typescript-eslint/explicit-module-boundary-types": ["error"]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

除此之外,我认为你不能filterson the caught .vue files by eslint, only apply the rule on the ones matched by: 'having <script lang="ts">'

也许您最好对这些特定错误使用内联注释

编辑

我看到的另一个解决方案可能是手动在文件中列出您的.eslint.config文件。

扩展您的打字稿与感知类型的规则起绒我得到的错误告诉我,“该文件必须包含在所提供的项目中的至少一个”的想法。

如果您确实想使用类型感知 linting 对文件进行 lint:检查您提供给 parserOptions.project 的每个 tsconfigs 的 include 选项 - 您必须确保所有文件都匹配一个 include glob,否则我们的工具将无法找到它。如果您的文件不应该是现有 tsconfigs 之一的一部分(例如,它是存储库本地的脚本/工具),则考虑在您的文件中创建一个新的 tsconfig(我们建议将其命名为 tsconfig.eslint.json)项目根目录在其包含中列出此文件。举个例子,你可以查看我们在这个 repo 中使用的配置:tsconfig.eslint.json .eslintrc.js