RJM
RJM

Reputation: 1178

Typescript: Type of a function that takes a function argument - getting "; expected" error

I've got a class that has a property which is a function type, which takes a function as an argument. The typescript compiler is producing a "; expected" error on the second =>. Any thoughts why? Code is below.

class Foo{
    public fn: ((string) => void) => void;
}

var foo = new Foo();
foo.fn = function(logger: (string) => void): void{
    logger("bar");
};

var writeToConsole = function(str: string): void {
    console.log(str);
}

foo.fn(writeToConsole);

Upvotes: 3

Views: 198

Answers (1)

Marko Gresak
Marko Gresak

Reputation: 8207

Because the inner function needs a name and only then you can specify it's type:

public fn: (inner: (string) => void) => void;

Obviously the inner is arbitrary, change it to your liking. See it working at Typescript playground (note: using shortened url because the original link includes parentheses which mess up markdown and I don't fancy escaping all of them)

Upvotes: 4

Related Questions