禁止使用TSLint设置数组属性

时间:2017-08-04 07:01:22

标签: 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,但无法为此找到规则。

1 个答案:

答案 0 :(得分:0)

我为此实现了一个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即可实现,对于我的项目而言,这不是一个可接受的解决方案,但这很容易解决。