HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptCritical

TypeScript type signatures for functions with variable argument counts

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptfunctionswithcountsargumentforvariabletypesignatures

Problem

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");
    ...
}

Solution

TypeScript uses the ECMAScript 6 spread proposal,


http://wiki.ecmascript.org/doku.php?id=harmony:spread

but adds type annotations so this would look like,

interface Example {
    func(...args: any[]): void;
}

Code Snippets

interface Example {
    func(...args: any[]): void;
}

Context

Stack Overflow Q#12739149, score: 319

Revisions (0)

No revisions yet.