Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, April 20, 2016

What's the meaning of

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


I just start to learn TypeScript. And I saw there are a lot of code using this sytax =>. I did some research by reading the Specification of TypeScript Version 1.6 and some google. I still cannot understand the meaning of =>.
For me, it feels like a pointer in C++. But I can't confirm it. If anyone can explain the following examples, that will be great. Thank you!

Here are the examples that I found it when I was reading the specification of Typescript. :

Object Types

var MakePoint: () => {        x: number; y: number;    };  

Question: What is this code doing? Creating a object called MakePoint with x and y filed in number type? Is this a constructor or a function for MakePoint?

Function Types

function vote(candidate: string, callback: (result: string) => any) {     // ...    }  

Question: What does the meaning of => any? Do you have to return the string type of result?

Can anybody explain me the difference or the purpose of these with proper example. Thank you!

Answer by Ryan Cavanaugh for What's the meaning of "=>" in TypeScript? (Fat Arrow)


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?

Answer by mam8cc for What's the meaning of "=>" in TypeScript? (Fat Arrow)


It is called a "fat arrow". It was added in EcmaScript6 and replaces the function keyword among other things.

More can be read here.

Answer by Eric Lippert for What's the meaning of "=>" in TypeScript? (Fat Arrow)


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?

Answer by mk. for 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.

Answer by Amit for What's the meaning of "=>" in TypeScript? (Fat Arrow)


Directly from the link in OP:

In this example, the second parameter to 'vote' has the function type

(result: string) => any which means the second parameter is a function returning type 'any' that has a single parameter of type 'string' named 'result'.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.