具有可变数量/类型参数的函数的 TypeScript 声明文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12907038/
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
TypeScript declaration file for function with variable number/type of arguments
提问by Josh Handel
headjs does some very crazy JavaScript type things to its API. For instance it takes an arbitrary number of strings (not a string array) for a function. Sometimes it ends that same function call, you can optionally end it with a function, for example.
headjs 对其 API 做了一些非常疯狂的 JavaScript 类型的事情。例如,一个函数需要任意数量的字符串(不是字符串数组)。有时它会结束同一个函数调用,例如,您可以选择用一个函数来结束它。
head.js("scripturl1", "scripturl2",...,callback);
You can also (just as easily) do the following
您也可以(同样轻松)执行以下操作
head.js({scriptlabel:"scripturl1"},{scriptlabel2:"scripturl2"},...., callback);
My question is how the HECK do we describe that in a declaration file? I am all ears here as my current pass seems completely wrong.
我的问题是我们如何在声明文件中描述它?我全神贯注,因为我目前的通行证似乎完全错误。
回答by nxn
The TS language spec refers to variable number/spread parameters as "Rest Parameters". An example interface with a function signature that accepts rest params:
TS 语言规范将可变数量/传播参数称为“其余参数”。具有接受其余参数的函数签名的示例接口:
interface IExample {
fn : (...args : any[]) => any;
}
var x : IExample = {
fn: function(...args : any[]) {
for (var i = 0, arg; arg = args[i]; i++) {
console.log(arg);
}
}
}
x.fn(1);
x.fn(1, 2);
x.fn("cat", "dog", "mouse");
Unfortunately, there are some limitations. The "Rest Parameter" has to be the last one in a function's signature -- so you won't be able to capture the type of the callback parameter since it is after the repeating parameter.
不幸的是,有一些限制。“Rest Parameter” 必须是函数签名中的最后一个——因此您将无法捕获回调参数的类型,因为它在重复参数之后。
If it wasn't, you would have been able to do something like this:
如果不是,您将能够执行以下操作:
var fn = function(cb: Function, ...args : string[]) {
...
}
回答by slebetman
The declaration is simply:
声明很简单:
function foo () { //...
The function arguments part of a function declaration in Javascript is just advisory. When calling a function Javascript allows you to call with less than the declared number of arguments (arguments not passed in defaults to undefined
) or with more than the declared number of arguments.
Javascript 中函数声明的函数参数部分只是建议性的。调用函数时,Javascript 允许您使用少于声明数量的参数(未传入的参数默认为undefined
)或多于声明的参数数量进行调用。
When a function is called, an arguments objectis created. The arguments object is a bit like an array (though it is not a proper Array object) that holds each passed in argument.
调用函数时,会创建一个参数对象。arguments 对象有点像一个数组(尽管它不是一个合适的 Array 对象),它保存每个传入的参数。
So, for example, to handle infinite arguments you can simply do:
因此,例如,要处理无限参数,您可以简单地执行以下操作:
function foo () {
for (var n=0; n<arguments.length; n++) {
alert(arguments[n]);
}
}
So now you can call foo
like:
所以现在你可以这样调用foo
:
foo(1,2,3);
foo(1,2,3,4,5,6,7,8);
Beyond that, it is simply a matter of using typeof, itstanceofetc. to detect what type of argument is passed in and process them accordingly.
除此之外,只需使用typeof、itstanceof等来检测传入的参数类型并相应地处理它们。