typescript arrow function

Arrow function

Typescript arrow function is pretty much anonymous / lambda funciton in other languages


A traiditional way to declare a function  is as below. It can call the function directory or assign it to a function variable.

The format of a function is (parameter: type) => return type

function Func(input: number) : number{

        return input * 2

};

Func(1)


let func: (input:number) => number;

func = Func

func(1)


To make above a one liner to combine the declare and assignment

 let func: (input:number) => number = function (input: number){return input * 2};


Using Arrow function skips the use of 'function' keywork. 

 let func : Function = (input:number) => {return input * 2;};


The type 'Function' is not necessary as it can be inferred

 let func = (input:number) => {return input * 2;};


When there is only one line in the funciton body, no need for the braces or the return keyword.

let func = (input:number) =>  input * 2;


console.log(func(1))


This shows how simplicity can be achieved with Arrow function.