简体   繁体   English

传递箭头函数数组

[英]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? 有什么办法可以做到吗?

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. 正如史蒂夫·芬顿(Steve fenton)所说,我将使用一个接口来删除重复项。

这似乎对我有用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM