What's the meaning of “=>” in TypeScript? (Fat Arrow)

后端 未结 8 1922
心在旅途
心在旅途 2020-11-28 22:59

I just start to learn TypeScript, and I saw there is a lot of code using this sytax =>. I did some research by reading the Specification of TypeScript Versio

8条回答
  •  Happy的楠姐
    2020-11-28 23:38

    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.

提交回复
热议问题