简体   繁体   English

接口中TypeScript函数声明的区别

[英]Difference of TypeScript function declaration in interfaces

What is the difference between these two declarations of functions in TypeScript Interfaces? TypeScript接口中这两个函数声明有什么区别?

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

and

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

These declarations are completely equivalent. 这些声明完全等同。

The only relevant difference here is that the second form can't be used for function overloads: 这里唯一相关的区别是第二种形式不能用于函数重载:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM