The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

nklein (33) [Avatar] Offline
#1

Thanks for writing a really great book. I've previously written a book for Manning so I'm appreciative of the efforts that go into creating a top-notch book.

My only issue (and its a relatively small one) has to do with B.8.3 Callable Interfaces. Although I'm familiar with Java and Actionscript, a callable interface is a new concept to me. I've read through the example several times, but it still isn't clear to me why and when I'd need to use callable interface. I also looked at the TypeScript Handbook website and didn't find any further information about it.

Norman Klein
Author of Laszlo in Action
Yakov Fain (219) [Avatar] Offline
#2
In JavaScript you can pass a function as an argument to a higher order function, right?
Now imagine that you're supposed to pass a function with a certain signature to a higher order function, but made a mistake and passed a wrong function. This will result in a runtime error.

Callable interfaces allow you to to catch this mistake during the development stage. In the book example you're referring to, we declare the signature of a function that has to be passed to the constructor of the instance of the Person object.

Take a look at the corresponding JavaScript that will be generated at the TypeScript playground: http://bit.ly/2hQRe60
It should help you understanding what's going on.

Now purposely introduce an error - declare a function with the wrong signature (do it on the left side at the playground):

var forTempWorkers = () => console.log("Can't increase salary for temps");

Try to pass it to the constructor to a Person:

workers[0] = new Person(tempWorkers);

TypeScript will immediately highlight the above line as erroneous so you'll catch this error during dev time whereas in JavaScript this error would silently sneak into your code.

Good question, Norman. I should blog about it.

nklein (33) [Avatar] Offline
#3

Thanks for your timely and well explained response. Yes, I agree this would make a good topic for a blog as there is a dearth of explanatory material dealing with callable interfaces and its an important feature within TypeScript.

Norman

Puneet Lamba (12) [Avatar] Offline
#4
Yakov, thanks for the writing a splendid book and for your answer to Norman's question on callable interfaces, which weren't entirely clear to me until now.

Whereas interfaces are generally implemented in the class declaration:

class Employee implements IPayable {...}

callable interfaces are specified as function arguments to other functions, e.g. the Person constructor.

Functions that implement the callable interface IPayable (e.g. forEmployees) can then be passed in as arguments to the Person constructor when creating a new instance:

const p = new Person(forEmployees);