kal*_*hua 21 typechecking typescript eslint
寻求帮助将 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)
Run Code Online (Sandbox Code Playgroud)
.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,
};
Run Code Online (Sandbox Code Playgroud)
包.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"
}
}
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我是否在配置中遗漏了某些东西,或者它在某些方面是否不合适?或者这不是实际上可能的事情吗?
我想通过 ESLint 获取错误报告,因为我在处理项目时在编辑器中显示了 linting 错误。我正在使用 Atom ( https://atom.io/ ),但我也希望它适用于 VSCode 和可能的 VIM;团队成员更喜欢不同的编辑器。
Gor*_*ett 47
不幸的是,ESLint 只报告来自它自己的 linter 的错误,它不报告打字稿编译失败。我很同情你——在我的项目中,我使用 Babel 来更快地转译打字稿,但由于 Babel 实际上并不检查类型(它只是删除它们),我仍然需要将该类型检查作为单独的 lint 步骤。
这篇博客文章https://iamturns.com/typescript-babel/描述了如何check-types在您的内部设置脚本package.json以执行此 linting 功能,并将 typescript 编译器视为辅助 linter。你甚至可以让它lint在你运行 eslint的同一个命令中运行:
{
...
"scripts": {
"check-types": "tsc --noemit",
"eslint": "eslint",
"lint": "npm run eslint && npm run check-types",
}
Run Code Online (Sandbox Code Playgroud)
然后,您将设置您的持续集成服务器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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11641 次 |
| 最近记录: |