passing an array of arrow functions

1.9k views Asked by At

I need to have an array of arrow functions like so

//how can we contain the list of tasks?
private _tasks : ((x: T) => boolean)[];

constructor(...tasks : ((x: T) => boolean)[]) {
    this._tasks = tasks;
}

is there any way I can do this?

3

There are 3 answers

2
dbones On

this seems to work for me

private _tasks :Array<(x: T) => boolean>;
0
Fenton On

You can use an interface to make the type more readable:

interface Task<T> {
    (x: T) : boolean;
}

function example<T>(...tasks: Task<T>[]) {

}

example(
    (str: string) => true,
    (str: string) => false  
);
0
basarat On

You have your brackets wrong for inline declarations. Use { not (:

class Foo<T>{
    private _tasks: { ( x: T ): boolean }[];

    constructor( ...tasks: { ( x: T ): boolean }[] ) {
        this._tasks = tasks;
    }
}

And as steve fenton said I'd use an interface just to remove duplication.