Typescript 中的可变参数,但没有数组/列表

Variable arguments in Typescript, but no array / list

有没有办法在 Typescript 中使用不包含在数组中的可变参数?我知道的唯一语法是:

protected log(...args: any[]): void

这不是真正的问题,但结果是耗时和不必要的点击。如果你传递 args 例如到 console.log 输出是可折叠的 "object",如 |> [Array[2]]。展开,有不需要的,嵌套的文本和原型信息,s.th。喜欢

v [Array[2]]
    0:Array[2]
       0:"Blah"
    |> 1:input
       length:2
 |> __proto__:Array[0]
    length:1
 |> __proto__:Array[0]

您必须单击每个条目才能完整阅读日志或搜索条目。与实际数组 [1, 2, 3, 4, 5] 或对象的输出相比,这并不有趣:

|> Object {a1: "123", b1: 456, c1: 789, a2: "123", b2: 456…}

他们提供了一种总结,扩展版也是按字母排序的:

a1:"123"
a2:"123"
a3:"123"
b1:456
b2:456
b3:456
c1:789
c2:789
c3:789

当然,可以 iterate 参数成员或使用 reduce 函数。但是除了不必要的代码和逻辑之外,为了更舒适的日志记录输出接缝会以更高的成本开销。

不,没有其他语法可以接受可变数量的参数。这听起来像是您的 browser/console 只是输出了太多不必要的信息。我的——chrome——只输出括号中的值。

如果你想要快速的东西,那么你可以这样做:

console.log(JSON.stringify(args));

或者创建您自己的函数来根据您的喜好输出数组。

对于数组,您可以执行以下操作:

function log() {
    console.log.apply(console, arguments);
}

然后:

log(1); // 1
log(1, 2, 3); // 1 2 3
log([1, 2, 3]); // > [1, 2, 3]