Jesse Liberty
Jesse Liberty

Reputation: 1414

Why would you create this arrow construct in TypeScript?

Why would you create this construct in TypeScript?

var sayFirstNumber: (firstNumber: number) => void;

sayFirstNumber = function (first: number) {
console.log(first);
}

Upvotes: 1

Views: 38

Answers (2)

Daniel Tabuenca
Daniel Tabuenca

Reputation: 13681

The only reason to do that is if you want to later re-assign sayFirstNumber to other implementations. Otherwise you wouldn't need to create a variable like that, you just do:

function(first: number):void { 
  console.log(first);
}

In your example the arrow construct is just defining the type of sayFirstNumber. The => indicates that that var sayFirstNumber will expect to be set to some type of function. The left hand side of the => represents the parameters that the function should contain. The right hand side of the => represent the return type that the function should return.

Upvotes: 0

toskv
toskv

Reputation: 31612

That arrow is the TypeScript syntax to define the return type for a function in an interface.

Upvotes: 2

Related Questions