dbones
dbones

Reputation: 4504

passing an array of arrow functions

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?

Upvotes: 1

Views: 2098

Answers (3)

basarat
basarat

Reputation: 275847

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.

Upvotes: 2

Fenton
Fenton

Reputation: 250922

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  
);

Upvotes: 1

dbones
dbones

Reputation: 4504

this seems to work for me

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

Upvotes: 1

Related Questions