Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  5] [ 1]  / answers: 1 / hits: 32542  / 12 Years ago, tue, october 16, 2012, 12:00:00

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.



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.


More From » typescript

 Answers
11

The TS language spec refers to variable number/spread parameters as Rest Parameters. An example interface with a function signature that accepts rest params:



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.



If it wasn't, you would have been able to do something like this:



var fn = function(cb: Function, ...args : string[]) {
...
}

[#82532] Sunday, October 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;