具有可变参数计数的函数的 TypeScript 类型签名 [英] TypeScript type signatures for functions with variable argument counts
本文介绍了具有可变参数计数的函数的 TypeScript 类型签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在定义具有接受可变数量参数的函数成员的接口时遇到问题.以下面的对象字面量为例:
I'm having trouble defining interfaces with function members that accept variable amounts of arguments. Take the following object literal as an example:
var obj = {
func: () => {
for(var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
};
我希望能够定义一个接口,例如:
I'd like to be able to define an interface such as:
interface IExample {
func: ( ??? ) => void;
}
这样下面的代码就可以编译没有错误了:
So that the following code can compile without error:
var test = (o: IExample) {
o.func("a");
o.func("a", "b");
o.func("a", "b", "c");
...
}
推荐答案
TypeScript 使用 ECMAScript 6 传播提案,
TypeScript uses the ECMAScript 6 spread proposal,
http://wiki.ecmascript.org/doku.php?id=和谐:传播
但添加了类型注释,因此看起来像,
but adds type annotations so this would look like,
interface Example {
func(...args: any[]): void;
}
这篇关于具有可变参数计数的函数的 TypeScript 类型签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文