极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。
山月训练营之面试直通车 服务上线了,从准备简历、八股文准备、项目经历准备、面试、面经、面经解答、主观问题答复、谈薪再到入职的一条龙服务。

# 请简述 typescript 中的 infer

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 731 (opens new window)

Author

回答者: okbug (opens new window)

和 returnType 有点关联,做返回值推断的

infer 表示在 extends 条件语句中待推断的类型变量。

例子:

/**
 * Obtain the parameters of a function type in a tuple
 */
type Parameters<T extends (...args: any) => any> = T extends (
  ...args: infer P
) => any
  ? P
  : never;

image

Last Updated: 6/26/2022, 10:48:10 AM