gpt4 book ai didi

tslint - 禁止使用 TSLint 设置数组属性

转载 作者:行者123 更新时间:2023-12-03 17:37:34 27 4
gpt4 key购买 nike

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

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

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

我看过 TSLint rules但无法为此找到规则。

最佳答案

我为此实现了 TSLint 规则,可在 GitHub 和 NPM 上找到:

  • https://github.com/leroydev/no-setting-array-properties-rule
  • https://www.npmjs.com/package/no-setting-array-properties-rule

  • 它可以像这样使用:
  • 使用 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即可实现同样,对于我的项目,这不是一个可接受的解决方案,但这是一个简单的解决方案。

    关于tslint - 禁止使用 TSLint 设置数组属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45500142/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com