I'm struggling to figure out if it's possible in TypeScript to declare a statically typed array of functions.
For example, I can do this:
const foo: (data: string) => void = function (data) {};
But if I want foo to be an array of functions that take a string and return nothing, how do I do that?
const foo: (data: string) => void [] = [];
Doesn't work because TypeScript thinks it's a function that takes a string and returns an array of void, and it doesn't seem to like me trying to wrap the function in brackets.
Any ideas?
Updated answer from 2025:
const foo: ((data: string) => void)[] = [];
function doFoo() {
for (var i = 0; i < this.foo.length; i++) {
this.foo[i]("test");
}
}
foo.push((bar) => { alert(bar) });
foo.push((bar) => { alert(bar.length.toString()) });
doFoo();