打字稿:如何降级或忽略某些类型的错误

Typescript: how to downgrade or ignore certain types of errors

虽然严格的代码分析非常有帮助,但 Typescript 编译失败并出现非关键错误似乎是错误的。

例如 Consecutive blank lines are forbidden,或者在开发中未使用 variables/definitions。特别是第一个,正在整理中

有没有办法根据类型降级或删除错误消息?我知道我们可以....

// @ts-ignore

...针对个别错误,但我正在寻找更广泛的画笔。

我相信 Consecutive blank lines are forbidden 来自 linter。可以在 tslint.json 中使用以下配置 (TSLint core rules) 禁用它:

"no-consecutive-blank-lines": false,
"no-unused-variable": false

每个规则也可以与包含严重性的对象相关联:"default" | "error" | "warning" | "off" (Configuring TSLint)。因此,您可以使用以下内容降级为警告:

"no-consecutive-blank-lines": { "severity": "warning" },
"no-unused-variable": { "severity": "warning" }

没有未使用的 variables/definitions 也可能需要在 tsconfig.json 中禁用 (TypeScript compiler options):

"noUnusedLocals": false

我想指出 tslint 有选项可以使用 --fix 标志自动修复错误,例如连续的空白行。例如,VS Code 有一个选项可以在保存时自动修复 linting 错误,启用此选项后,linter 在开发过程中的障碍要小得多。