What's the meaning of "=>" in TypeScript? (Fat Arrow)
Perhaps you are confusing type information with a function declaration. If you compile the following:
var MakePoint: () => {x: number; y: number;};
you will see that it produces:
var MakePoint;
In TypeScript, everything that comes after the :
but before an =
(assignment) is the type information. So your example is saying that the type of MakePoint is a function that takes 0 arguments and returns an object with two properties, x
and y
, both numbers. It is not assigning a function to that variable. In contrast, compiling:
var MakePoint = () => 1;
produces:
var MakePoint = function () { return 1; };
Note that in this case, the =>
fat arrow comes after the assignment operator.
In a type position, =>
defines a function type where the arguments are to the left of the =>
and the return type is on the right. So callback: (result: string) => any
means "callback
is a parameter whose type is a function. That function takes one parameter called result
of type string
, and the return value of the function is of type any
".
For the expression-level construct, see What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
var MakePoint: () => {
x: number; y: number;
};
MakePoint
is a variable. It's type is a function that takes no arguments and produces numbers x and y. Now does the arrow make sense?