6总结Function
不应该用作表示函数类型 。当您只想指定参数而不指定返回类型时 , 可以使用语法(a: string, b: number) => any
。
记住,(...args: any) => any
可用于表示任何函数类型 。
此处,mapper
表示从对象中提取数字的函数 。该 sum 函数的强大之处在于您可以丢弃大多数此类声明:
const youTubeVideos = [ { name: "My favorite cheese", views: 100 }, { name: "My second favorite cheese (you won't believe it)", views: 67, },]; const result = sum(youTubeVideos, (video) => { return video.views;}); // 167
事实上,我们已经舍弃了所有类型声明,但 video
仍旧被推断为 { name: string; views: number }
。这是可能的 , 因为我们的函数定义的特殊性:(item: T) => number
。
【不要在Typescript中使用Function类型】
推荐阅读
- ChatGPT不是唯一:自动编写程序的另七个选择!
- 香蕉怎么存放保鲜长 香蕉怎么存放保鲜长久不坏
- 《伪装者》女主开撕胡歌!被撞断锁骨还不让说,遭网暴10年难以忍受
- 微信这个地方可以查看对方和谁关系不一般,操作简单,一看就能学会
- 人设崩塌, 冯小刚金屋藏娇20年, 绝代风华, 自曝这辈子最对不起的就是徐帆
- 曝汪小菲给大S巨额达成和解,拒绝和玥儿一起过中秋,称以后不相欠
- 娱乐圈到底有多乱?“潜规则”真是无处不在,国际巨星也得如履薄冰
- 不粘锅起泡是怎么回事 不粘锅起泡是什么原因
- 当红女星穿中式衣冠不整,扣子没系裤腿脏,和老外勾肩搭背惹争议
- 港媒称黄日华求婚女神,妻子去世只有三年,说好的终生不娶呢?