Angular 1 个项目的 TSLint 配置

TSLint configuration for Angular 1 project

我的团队正在使用 Angular 1.5.* + typescript 开发项目。

有人可以为我这样的项目提供最佳 TSLint 配置建议吗?

我现在想添加 TSLint 配置(https://github.com/Microsoft/TypeScript/blob/master/tslint.json) from official TS repo and with ESlint rules set. https://github.com/Microsoft/TypeScript/blob/master/tslint.json

够不够?你怎么看?

提前感谢您的回答。

Will it be enough? What do you think?

就我个人而言,直到出现问题时我才打开它。幸运的是,我有幸让我的队友尊重和爱护我,所以他们知道我的意思。

您可以选择那个,也可以选择:

{
  "extends": "tslint:recommended"
}

要使用 palantir 默认值:https://github.com/palantir/tslint/#configuration

我也有类似情况。我使用 tslint-microsoft-contrib 作为基准。

  "extends": "tslint-microsoft-contrib",

它绝对能以您期望的所有方式提供帮助。但是,对于 AngularJs TypeScript 代码,需要重新配置或关闭一些规则。我对函数名称进行了以下配置更改,member-ordering, no-import-side-effect, and no-unsafe-any:

"function-name": [
  true,
  {
    // allow public methods to start with $ for $onChanges
    // and other Angular lifecycle hooks
    "method-regex": "^[a-z$][\w\d]+$", 

    // otherwise unchanged
    "private-method-regex": "^[a-z][\w\d]+$",
    "protected-method-regex": "^[a-z][\w\d]+$",
    "static-method-regex": "^[A-Z_\d]+$",
    "function-regex": "^[a-z][\w\d]+$"
  }
],

// Variant ordering that places public statics before constructor
// So we can place AngularJs $inject just before the constructor
"member-ordering": [
  true,
  {
    "order": [
      "public-instance-field",
      "protected-instance-field",
      "private-instance-field",
      "public-static-field",
      "protected-static-field",
      "private-static-field",
      "constructor",
      "public-static-method",
      "protected-static-method",
      "private-static-method",
      "public-instance-method",
      "protected-instance-method",
      "private-instance-method"
    ]
  }
],

// angular.module(...).component(...) is a side-effect
"no-import-side-effect": false,

// ng.IController, among others in @types/angular, uses "any"
// and is flagged by this rule
"no-unsafe-any": false,

根据您定义 AngularJs 组件的方式,您可能不需要将 no-import-side-effect 设置为 false。我还没有看到关于在 TypeScript 中定义 AngularJs 组件的最佳方式的任何共识,这是一种耻辱,因为这被列为从 AngularJs 迁移到 Angular 的第 1 步2+.

遗憾的是,这错过了为 AngularJs 定制 tslint 规则以匹配最佳实践的可能性。我正在考虑对生成的代码使用 运行 eslint-plugin-angular 的想法,或者将这些规则转换为 TypeScript。但更好的解决方案可能是迁移到 Angular 2+ 并使用 codelyzer.