接口中TypeScript函数声明的区别 [英] Difference of TypeScript function declaration in interfaces

查看:198
本文介绍了接口中TypeScript函数声明的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the difference between these two declarations of functions in TypeScript Interfaces?

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

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


推荐答案

这些声明完全等效。

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

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;
}

这篇关于接口中TypeScript函数声明的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆