首页 > 解决方案 > 停止 typescript-eslint/explicit-module-boundary-types 应用于不使用 Typescript 的 vue 组件

问题描述

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

警告

我对非 typescript组件内的方法有此警告

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

问题

我怎么能不对.vue不使用的文件应用此规则<script lang="ts">

标签: typescriptvue.jstypescript-eslint

解决方案


因此,此规则的目的是确保导出的模块对外部使用它的人有一个清晰的接口。从规则页面.ts来看,用for files代替files似乎是有道理的.vue,但是你可以根据你的项目需求来判断。

对于我的项目,我使用了建议的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"]
      }
    }
  ]
}

除此之外,我认为你不能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 的每个 tsconfig 的包含选项 - 您必须确保所有文件都与包含 glob 匹配,否则我们的工具将无法找到它。如果您的文件不应该是现有 tsconfig 的一部分(例如,它是存储库本地的脚本/工具),那么请考虑在您的项目根目录在其包含中列出了此文件。例如,您可以查看我们在此 repo 中使用的配置:tsconfig.eslint.json .eslintrc.js


推荐阅读