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?
source share