ESLint没有报告TypeScript编译器类型检查错误。

56
寻求帮助,将TypeScript编译器报告的类型错误输出到ESLint中。typescript-eslint (https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/TYPED_LINTING.md) 库让我认为这应该是可能的。
文件结构。
src/
  ... source files
  tsconfig.json
test/
  ... testing files
.eslintrc.js
package.json
tsconfig.json (symlink to src/tsconfig.json)

.eslintrc.js

module.exports = {
  'env': {
    'jest': true,
    'node': true,
  },
  'extends': [
    'airbnb-typescript/base',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:jest/recommended',
  ],
  'parser': '@typescript-eslint/parser',
  'parserOptions': {
    'project': ['./tsconfig.json'],
    'tsconfigRootDir': __dirname,
  },
  'plugins': [
    '@typescript-eslint',
    'jest',
  ],
  'root': true,
};

package.json

->

package.json

{
  "name": "...",
  "version": "...",
  "description": "...",
  "scripts": {},
  "devDependencies": {
    "@types/jest": "^25.1.3",
    "@typescript-eslint/eslint-plugin": "^2.21.0",
    "@typescript-eslint/parser": "^2.21.0",
    "eslint": "^6.8.0",
    "eslint-config-airbnb-typescript": "^7.0.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-jest": "^23.8.1",
    "jest": "^25.1.0",
    "nock": "^12.0.2",
    "ts-jest": "^25.2.1"
  },
  "dependencies": {
    "@types/node": "^13.7.7",
    "aws-sdk": "^2.630.0",
    "jsonschema": "^1.2.5",
    "typescript": "^3.8.3"
  }
}

ESLint的输出为空 - 没有错误 - 但在运行TypeScript编译器来构建项目时,会报告许多错误,例如:

src/file.ts:xx:xx - error TS2339: Property 'body' does not exist on type 'object'.
src/file.ts:xx:xx - error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
src/filets:xx:xx - error TS7006: Parameter 'err' implicitly has an 'any' type.

在配置中是否缺少某些内容或者存在问题?还是说这并不是一件可行的事情?

我希望通过ESLint来报告错误,因为我会在编辑器上看到这些lint错误。我使用的是Atom (https://atom.io/),但我也希望它能够在VSCode和可能的VIM上运行;团队成员有不同的编辑偏好。


我创建了一个Git存储库来说明我正在经历的不一致性,希望有人可以帮助我正确配置它。https://github.com/kalisjoshua/eslint-typescript-type-warnings-example - kalisjoshua
2个回答

97

很遗憾,ESLint只能报告自己的linter发现的错误,无法报告TypeScript编译失败的情况。我对你深表同情 - 在我的项目中,我使用Babel来加速TypeScript编译,但由于Babel实际上不检查类型(它只是删除它们),所以我仍然需要将类型检查作为一个单独的lint步骤。

这篇博客文章https://iamturns.com/typescript-babel/介绍了如何在package.json中设置check-types脚本来执行此linting功能,并将TypeScript编译器视为次要linter。甚至可以在运行eslint的同一lint命令中运行它:

{
  ...
  "scripts": {
    "check-types": "tsc --noemit",
    "eslint": "eslint",
    "lint": "npm run eslint && npm run check-types",
  }

然后,您将设置持续集成服务器,将npm run lint作为其构建步骤之一运行。

对于您的编辑器,似乎有一个TypeScript的Atom插件:https://atom.io/packages/atom-typescript,那将是在编辑器中显示TypeScript错误的理想方式。 VSCode已经内置了这个功能。我主要使用VSCode,并且它非常好用!

我建议为VSCode设置最后一个选项是结合VSCode的ESLint插件使用eslint的“自动修复”功能,并配置每次保存文件时都运行eslint。您可以在每个项目的.vscode/settings.json文件中进行设置:

{
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  }
}

3
您可以使用分号代替 && - npm run eslint; npm run check-types。使用 && 意味着如果 lint 命令输出错误,check-types 命令将不会运行。 - Maciej Krawczyk
2
非常正确!但是如果您使用分号,就会抑制 eslint 命令的退出代码。因此,如果 eslint 失败但 check-types 成功,则您的持续集成脚本很可能无法捕获它失败的情况。所以这取决于您使用它的目的! - Gordon Burgett
2
当然,我最好重新表述一下:“如果你想将linting错误视为警告,请使用&”。 - Maciej Krawczyk

1
您可以使用以下内容来设置报告TypeScript类型检查错误:
  "scripts": {
     "lint": "eslint src/**/*.{ts,tsx}",
     "lint:fix": "eslint src/**/*.{ts,tsx} --fix",
     "prettify": "prettier --write src/**/*.{ts,tsx}",
     "type-check": "tsc --noEmit",
   },
  "lint-staged": {
     "*.{ts,tsx}": [
       "eslint",
       "prettier --write",
       "git add"
     ]
   },

最后,要运行该脚本以检查 Linting 和类型检查错误,您可以使用以下命令:

yarn type-check && lint-staged

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接