3

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

1 Answer 1

4

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)

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

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.