不要在Typescript中使用Function类型( 二 )

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类型】


推荐阅读