【问题标题】:ESLint not reporting TypeScript compiler type checking errorsESLint 不报告 TypeScript 编译器类型检查错误
【发布时间】:2020-03-03 20:21:31
【问题描述】:

寻求帮助,将 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

{
  "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 获取错误报告,因为在我处理项目时,我的编辑器中显示了 linting 错误。我正在使用 Atom (https://atom.io/),但我也希望它适用于 VSCode,也可能适用于 VIM;团队成员喜欢不同的编辑器。

【问题讨论】:

标签: typescript eslint typechecking


【解决方案1】:

不幸的是,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
这将是让您的打字稿错误出现在您的编辑器中的理想方式。 VSCode 内置了这个功能。我主要使用 VSCode,效果很好!

我为 VSCode 推荐的最后一个设置是将 eslint 的“自动修复”功能与 VSCode 的 eslint 插件一起使用,并将其配置为在保存文件时运行 eslint。您可以在 .vscode/settings.json 中针对每个项目执行此操作:

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

【讨论】:

  • 您可能更喜欢使用分号而不是 && - npm run eslint; npm run check-types 使用 && 意味着如果 lint 命令输出错误,则 check-types 命令将不会运行。
  • 确实如此!但是,如果您使用分号,则会抑制 eslint 命令的退出代码。因此,如果 eslint 失败但 check-types 成功,那么您的持续集成脚本可能不会发现它失败了。所以这取决于你用它做什么!
  • 当然,我可能应该重申:“如果您想将 linting 错误视为警告,请使用 &”。
【解决方案2】:

您可以使用以下设置报告 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

【讨论】:

    最近更新 更多