TypeScript: example of a function type

TypeScript allows strongly typed functions/lambda arguments.

For instance, you might define a logging routine, where you delegate the actual log writing (console output, file output, etc) to a callback.

To do this, you’d want to accept a function that takes a string, and returns nothing (unit, void, etc, depending on what you compare).

You can do this easily:

let onLog: (value: string)=>void = 
  console.log;

If you want a good Javascript book, I got a lot out of Secrets of the Javascript Ninja.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *