TypeScript array of functions

I was wondering how to declare an array of typed functions in TypeScript.

For example, let's say I have a field that can contain a function that has no arguments and returns void:

private func: () => void; 

Now, let's say I need a field that can contain an array of such functions:

 private funcs: () => void []; 

This is obviously the wrong way to do what I thought, as the compiler considers it to be a function that returns an array of voids.

Trying to isolate an inline prototype declaration with parentheses, as in:

 private funcs2: ( () => void ) []; 

causes a compiler error.

Does anyone have any idea how this can be done?

+6
source share
1 answer

You will need to use the full literal syntax instead of the abbreviation => :

 private funcs: { (): void; }[]; 

You can also create an interface if this looks too weird:

 // (elsewhere at top-level) interface foo { (): void; } class etc { private funcs: foo[]; } 
+8
source

All Articles