1

Why would you create this construct in TypeScript?

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

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.