shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.88k stars 504 forks source link

【Q707】请简述 typescript 中的 infer #731

Open shfshanyue opened 2 years ago

okbug commented 2 years ago

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

canvascat commented 2 years ago

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