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;

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

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 *