TypeScript declaration file for function with variable number/type of arguments

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[]) {
    ...
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)