TypeScript:可变参数函数的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12699058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:11:17 来源:igfitidea点击:
TypeScript: types for variadic functions
提问by tensai_cirno
Possible Duplicate:
open-ended function arguments with TypeScript
可能的重复:
使用 TypeScript 的开放式函数参数
Is there any acceptable type signature for variadic functions? Example:
可变参数函数是否有任何可接受的类型签名?例子:
function sum () {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
};
console.log(sum(1, 2, 3, 4, 5));
gives me compilation error:
给我编译错误:
foo.ts(9,12): Supplied parameters do not match any signature of call target
回答by mohamed hegazy
In TypeScript you can use "..." to achive the above pattern:
在 TypeScript 中,您可以使用“ ...”来实现上述模式:
function sum (...numbers: number[]) {
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
};
This should take care of your error.
这应该可以解决您的错误。

