Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Array of Callbacks

How would you declare an array of callbacks in TypeScript?

A single callback looks like this:

var callback:(param:string)=>void = function(param:string) {};

So an array of callbacks should look like this:

var callback:(param:string)=>void[] = [];

However, that creates ambiguity, since I could mean an array of callbacks, or a single callback which returns an array of voids.

In the TypeScript playground, it thinks it is an array of voids. So, my next though was to wrap it in parentheses:

var callback:((param:string)=>void)[] = [];

But that doesn't work either.

Any other ideas?

like image 407
samanime Avatar asked Mar 27 '13 21:03

samanime


1 Answers

You'll need to use the full type literal syntax form, like so:

var callback:{(param:string): void;}[] = [];

This is sort of ugly; if you like you can make a name for it first:

interface fn {
    (param: string): void;
}
var callback2: fn[] = [];
like image 136
Ryan Cavanaugh Avatar answered Oct 22 '22 13:10

Ryan Cavanaugh