寻求帮助,以将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获得错误报告,因为在我从事该项目时,编辑器中会显示掉毛错误。我正在使用Atom(https://atom.io/),但我也希望它能用于VSCode以及可能的VIM。团队成员喜欢其他编辑。
我下载了您的存储库,并且在VS Code中一切正常。您是否忘记了陪同。我运行了eslint --ext .ts .
和tsc -p tsconfig.json
,eslint和tsconfig都在运行。希望您能解决这个问题。
[不幸的是,ESLint仅报告来自其自己的linter的错误,不报告打字稿编译失败。我很同情您-在我的项目中,我使用Babel加快了打字稿的翻译速度,但是由于Babel实际上并不检查类型(只是删除了它们),因此我仍然需要那种类型检查作为单独的皮棉步骤。
此博客文章https://iamturns.com/typescript-babel/描述了如何在check-types
中设置package.json
脚本以执行此linting功能,并将打字稿编译器视为辅助linter。您甚至可以在运行eslint的同一lint
命令中运行它:
{
...
"scripts": {
"check-types": "tsc --noemit",
"eslint": "eslint",
"lint": "npm run eslint && npm run check-types",
}
然后您将连续集成服务器设置为运行npm run lint
作为其构建步骤之一。
对于您的编辑器,似乎有一个用于打字稿的Atom插件:https://atom.io/packages/atom-typescript那是使打字稿错误显示在编辑器中的理想方法。 VSCode具有内置的此功能。我主要使用VSCode,效果很好!
我为VSCode建议的最后一个设置是将eslint的“自动修复”功能与VSCode的eslint插件一起使用,并配置为在保存文件时运行eslint。您可以在每个项目的.vscode/settings.json
中执行此操作:
{
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}