typescript - TSLint:禁止使用 Function.length 属性

标签 typescript tslint

我在一个使用 knockout.js(使用 TypeScript)的项目中,由于 knockout observables 只是函数,所以人们经常会错误地访问可观察函数的 length 属性,而不是访问其自定义对象模型的 length 属性。

是否有一些 tslint 规则可以禁止使用某种类型的特定属性?我看过“禁止”规则,但这似乎只适用于禁止使用函数和方法,而不适用于属性。

最佳答案

有一个 tslint 规则禁止使用特定函数或全局方法。

以下格式的禁用函数或方法列表:

  • 禁止功能:
    • 只是函数的名称:"functionName"
    • 具有一个元素的数组中的函数名称:["functionName"]
    • 以下格式的对象:{"name": "functionName", "message": "optional explanation message"}
  • 禁止方法:
    • 包含对象名称、方法名称和可选消息的数组:["functionName", "methodName", "optional message"]
    • 以下格式的对象:{"name": ["objectName", "methodName"], "message": "optional message"}
      • you can also ban deeply nested methods: {"name": ["foo", "bar", "baz"]} bans foo.bar.baz()
      • the first element can contain a wildcard (*) that matches everything. {"name": ["*", "forEach"]} bans [].forEach(...), $(...).forEach(...), arr.forEach(...), etc.

配置示例

"ban": [
  true,
  "eval",
  {"name": "$", "message": "please don't"},
  ["describe", "only"],
  {"name": ["it", "only"], "message": "don't focus tests"},
  {
    "name": ["chai", "assert", "equal"],
    "message": "Use 'strictEqual' instead."
  },
  {"name": ["*", "forEach"], "message": "Use a regular for loop instead."}
]

架构

{
  "type": "list",
  "listType": {
    "anyOf": [
      {
        "type": "string"
      },
      {
        "type": "array",
        "items": {
          "type": "string"
        },
        "minLength": 1,
        "maxLength": 3
      },
      {
        "type": "object",
        "properties": {
          "name": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "minLength": 1
              }
            ]
          },
          "message": {
            "type": "string"
          }
        },
        "required": [
          "name"
        ]
      }
    ]
  }
}

关于typescript - TSLint:禁止使用 Function.length 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639295/

相关文章:

visual-studio - 如何在 Visual Studio 中调用 "Organize imports"TypeScript 功能?

typescript - 在 Typescript 中构建 const 键的类型

javascript - 如何在 JavaScript/TypeScript 中检测具有相同索引的嵌套循环

typescript 错误 : foo is declared but its value is never read. TS6133

node.js - 如何调试 TSLint 错误?

typescript tsc --声明

typescript - 什么是环境声明

javascript - 动态添加到 typescript 中的每一行

来自@types 中声明的命名空间的 typescript 扩展接口(interface)

javascript - `undefined!` 在 typescript 中是什么意思?