Difference between call signature and function type

I am trying to understand why we have a different syntax for call signatures and function types. Consider the following code:

interface MyInterface { // This is call signature // It is used inside object type, function expression, function declaration, etc... (x:number, y:number):number; } var myOne : MyInterface = (x,y) => x + y; // vv this is function type var myTwo : (x:number, y:number)=>number = (x,y) => x + y; // function type is used in function type literal 

In this code, myOne and myTwo variables are equally effective. They (as far as I can see) of the same type, are simply defined differently.

Now that we use the interface to define them, we use a call signature that looks like this:

 (x:number, y:number):number 

When we do not use an interface, we use a function type literal:

 (x:number, y:number)=>number 

Both express the same thing, parameter names and types, and return type types. I would like to know why we need two different but identical ways to write the same thing in typescript?

+6
source share
1 answer

They are exactly the same.

why do we need two different but identical ways to write the same thing in typescript

The signature (x:number, y:number)=>number is useful as a property annotation:

 interface MyInterface { (x:number, y:string):string; someProperty: (x:number, y:number)=>number; } 

Which looks like yours:

 var myTwo : (x:number, y:number)=>number 

Just a short description of the detailed :

 var myTwo : {(x:number, y:number):number} 

So you can see the simplicity ()=> .

Also

It should be noted that the signature of the function allows you to overload:

 var myTwo : { (x:number, y:number):number; (x:number):string; } 

Which is not supported for property annotation.

+11
source

All Articles