接口中TypeScript函数声明的区别

时间:2014-12-16 17:02:22

标签: javascript function interface typescript

TypeScript接口中这两个函数声明有什么区别?

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

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

1 个答案:

答案 0 :(得分:1)

这些声明完全等效。

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

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

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