【发布时间】:2019-11-03 19:31:43
【问题描述】:
两者有什么区别?
在做:
一)
let myAdd: (x: number, y: number) => number =
function(x: number, y: number): number { return x + y; };
就这样做:
B)
let myAdd = function(x: number, y: number): number { return x + y; };
我从打字稿documentation 中获取了这个。为什么 sn -p A 中的代码有用?我只是在其中看到了一些冗余,就像在 sn-p B 中所做的那样,输入了参数并且输入了返回值。
我的印象是,在 sn-p A 中,参数的输入和结果值被完成了两次。
我不明白为什么 A 不同于 B 或者为什么 A 比 B 更有用
【问题讨论】:
-
它没有区别,因为被推断的类型是完全相同的。确保它符合特定的签名而不是像本例那样的内联类型通常很有用。
标签: typescript