不允许使用 TSLint 设置数组属性

Disallowing setting array properties with TSLint

我想知道是否有可以禁止以下代码的 TSLint 规则:

var arr = [1, 2];
//the line below should be disallowed
arr.prop1 = "3";
//because arr now has array elements and object properties

我想禁止这样做,因为它会阻止 arrJSON.stringify 完全序列化,因为 JSON.stringify 会将 arr 序列化为 [1, 2]

我查看了 TSLint rules,但没能找到相关规则。

我为此实施了 TSLint 规则,可在 GitHub 和 NPM 上使用:

可以这样使用:

  • 使用 npm install no-setting-array-properties-rule --save-dev 安装它。
  • 将它添加到您的 tslint.json,像这样:

    {
       "rulesDirectory": [
           "../node_modules/no-setting-array-properties-rule/dist/src"
       ],
       "rules": {
           "no-setting-array-properties": true
       }
    }
    

编辑:

它也可以通过设置 noImplicitAny: true 来实现,对于我的项目来说,这不是一个可以接受的解决方案,但这是一个简单的解决方案。