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;

Leave a Reply

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