On this page
ThisParameterType工具类型作用
ThisParameterType<T>如何提取函数类型中this参数的类型?举例在装饰器中保留原方法this上下文的类型处理
考察点分析
核心能力维度
- TypeScript高级类型理解:考察对工具类型底层实现的掌握程度
- 函数类型操作能力:检验对函数参数、返回值、上下文类型的处理能力
- 装饰器类型安全:评估类型系统在元编程场景中的实际应用水平
技术评估点
ThisParameterType
工具类型的工作原理- 函数签名中
this
参数的隐式声明特性 - 装饰器上下文保留的类型推导技巧
OmitThisParameter
与ThisParameterType
的配合使用- 泛型约束在类型传播中的应用
技术解析
关键知识点
- ThisParameterType > 函数上下文类型提取
- 装饰器类型签名 > 泛型类型传递
- OmitThisParameter > 剔除
this
参数的工具类型
原理剖析
TypeScript通过函数签名首个参数的this: Type
语法声明上下文类型。ThisParameterType<T>
通过条件类型推断提取该类型:
type ThisParameterType<T> = T extends (this: infer U, ...args: any[]) => any ? U : unknown
装饰器场景中,需通过泛型保留原方法的类型特征:
- 使用
ThisParameterType
获取原函数this
类型 - 通过
OmitThisParameter
剔除原函数中的this
类型声明 - 将原参数、返回值类型与新上下文类型组合
常见误区
- 误认为
this
参数是普通函数参数 - 装饰器中未正确保留参数/返回类型
- 混淆
ThisType
与ThisParameterType
的区别
问题解答
ThisParameterType<T>
用于提取函数类型中显式声明的this
参数类型。在装饰器场景中,可通过以下方式保留原方法类型:
declare function decorate<
T extends (this: any, ...args: any[]) => any
>(fn: T): (
this: ThisParameterType<T>, // 提取原始this类型
...args: Parameters<T> // 保留参数类型
) => ReturnType<T> { // 保留返回值类型
return function(this, ...args) {
return fn.apply(this, args)
}
}
class Example {
value = 42;
@decorate
method(this: Example) {
return this.value;
}
}
该方案通过泛型T
捕获原始函数类型,组合ThisParameterType
确保装饰后的方法保持原始this
上下文,
Parameters
和ReturnType
维护参数和返回值的类型安全。
深度追问
如何实现带副作用的装饰器类型?
提示:通过ThisType
绑定上下文 + 交叉类型
多个装饰器叠加时的类型变化?
提示:递归应用装饰器类型推导
OmitThisParameter实现原理?
提示:通过this: never
覆盖原始类型
Last updated 06 Mar 2025, 13:07 +0800 .