以下为ChatGPT自动生成的解答,正确与否请自行判断。

BFE.devAI解答 - TypeScript 类型谜题
14. 实现ThisParameterType<T>

type MyThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown;

解析:

对于一个函数类型 T,如果它有一个显式的 this 参数,则这个参数的类型就是此函数类型的 ThisParameterType。如果没有显式的 this 参数,则 ThisParameterType 就是 unknown

在实现上,我们可以利用条件类型的推断机制获取函数类型中的 this 参数类型。具体地,如果 T 类型的函数是一个有自定义 this 参数的函数类型,则将其 this 参数类型提取为 U;否则将返回 unknown。这个过程可以使用条件类型来实现。