TSLint:变量名必须是camelcase或大写

时间:2017-03-29 17:00:16

标签: typescript tslint

我有一些以前导下划线开头的变量名称,我在更新tslint.json后仍然收到此警告

tslint.json

{
  "extends": "tslint:recommended",
  "rules": {
    "variable-name": [
      true,
      "ban-keywords",
      "check-format",
      "allow-leading-underscore"
    ]
  },
  "exclude": [
    "build/**/*",
    "node_modules/**/*",
    "tmp/**/*"
  ]
}

我哪里错了?

感谢您的反馈

更新

我使用的是TSLint的4.5.1版

3 个答案:

答案 0 :(得分:13)

我更新了tslint.json,配置了文件,并向变量名数组添加了可选参数。

"allow-leading-underscore"允许在下划线开头(仅在指定了“检查格式”的情况下才有效)

"allow-pascal-case"除了lowerCamelCase之外,还允许使用PascalCase。

"allow-snake-case"除lowerCamelCase外,还允许使用snake_case。

"allow-trailing-underscore"允许在最后加下划线。 (仅在指定“检查格式”时有效)

{
  // ...
  "rules": {
    "variable-name": [
      true,
      "allow-leading-underscore"
    ],
  },
  // ...
}

您可以根据需要配置tslint.json

此链接可能有帮助。 https://palantir.github.io/tslint/rules/variable-name/

答案 1 :(得分:12)

您可以通过这种方式解决问题:

{
  "extends": [
    "tslint:recommended"
  ],
  "jsRules": {},
  "rules": {
    "variable-name": [
      true,
      "ban-keywords",
      "check-format",
      "allow-leading-underscore"
    ]
  },
  "rulesDirectory": []
}

答案 2 :(得分:0)

您可以通过编辑tslint.json并将所有这些属性添加到“变量名称”来解决问题:

"variable-name": {
  "options": [
    "ban-keywords",
    "check-format",
    "allow-pascal-case",
    "allow-leading-underscore",
    "allow-snake-case",
    "allow-trailing-underscore",
    "require-const-for-all-caps"
  ]
}