1

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 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

1

this seems to work for me

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

2 Comments

In TypeScript 1.4 you'll be able to use that parenthesized syntax, or to create a type alias for that. But till that, your option is the only possible way, yes. Or you may create an interface with only one call signature, and it will be structurally identical to your signature.
I will add that T[] is defined to be the same as Array<T> so there is no loss of precision here; in most cases this is the most readable workaround (I personally hate one-overload type literals!)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.