【问题标题】: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;
}