TypeScript type signatures for functions with variable argument counts

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;
}

Just to add to chuck's answer, you don't need to have an interface defined as such. You can just do the ... directly in the method:

class Header { constructor(public name: string, public value: string) {} }

getHeaders(...additionalHeaders: Header[]): HttpHeaders {
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json')

    if (additionalHeaders && additionalHeaders.length)
        for (var header of additionalHeaders)
            headers.append(header.name, header.value);

    return headers;
}

Then you can call it:

headers: this.getHeaders(new Header('X-Auth-Token', this.getToken()))

Or

headers: this.getHeaders(new Header('X-Auth-Token', this.getToken()), new Header('Something', "Else"))

Tags:

Typescript