Difference between call signature and function type

后端 未结 1 1718
不知归路
不知归路 2020-12-28 17:34

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

interface MyInterface {
   //         


        
相关标签:
1条回答
  • 2020-12-28 17:52

    They are exactly the same.

    why do we need two different yet so similar ways to write the same thing in typescript

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

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

    Which is similar to your:

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

    Just a shorthand for a verbose :

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

    So you can see the simplicity of ()=>.

    Also

    One thing to note is that a function signature allows overloading:

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

    Which is not supported for a property annotation.

    0 讨论(0)
提交回复
热议问题