gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:53 24 4
gpt4 key购买 nike

我在一个使用 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/

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