1

我需要像这样的箭头函数数组

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

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

有什么办法可以做到这一点?

4

3 回答 3

2

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.

于 2014-11-26T22:49:51.073 回答
1

this seems to work for me

private _tasks :Array<(x: T) => boolean>;
于 2014-11-26T18:29:44.397 回答
1

您可以使用接口使类型更具可读性:

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

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

}

example(
    (str: string) => true,
    (str: string) => false  
);
于 2014-11-26T21:13:07.227 回答