【问题标题】:Difference of TypeScript function declaration in interfaces接口中 TypeScript 函数声明的区别
【发布时间】:2015-02-15 01:54:30
【问题描述】:

TypeScript Interfaces 中这两种函数声明有什么区别?

interface IExample {
  myFunction(str: string): void;
}

interface IExample {
  myFunction: (str: string) => void;
}

【问题讨论】:

    标签: javascript function interface typescript


    【解决方案1】:

    这些声明是完全等价的。

    这里唯一相关的区别是第二种形式不能用于函数重载:

    // OK
    interface Example {
        myFunction(s: string): void;
        myFunction(s: number): void;
    }
    
    // Not OK
    interface Example {
        myFunction: (s: string) => void;
        myFunction: (s: number) => void;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 2012-12-30
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 2019-08-10
      • 1970-01-01
      相关资源
      最近更新 更多