A Typed array of functions

or foo: ((data: string) => void)[]


If you wish declare an array of callable function in TypeScript, you can declare a type:

type Bar = (
  (data: string) => void
);

And then use it:

const foo: Bar[] = [];

const fooFn = (data: string) => console.log(data);
foo.push(fooFn);
foo.forEach((fooFn: Bar) => fooFn("Something");

You can find this in the language spec section 3.6.4:

foo: { (data: string): void; } []

Other (newer, more readable) ways to type an array of functions using fat arrows:

let foo: Array<(data: string) => void>;
let bar: ((data: string) => void)[];

Tags:

Typescript