ts装饰器保存this指向

要使用 function 而不是 箭头函数

typescript 复制代码
interface MethodDecorator<T> {
  (
    target: Object,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<T>
  ): TypedPropertyDescriptor<T> | void;
}

type TMethodDecorator = MethodDecorator<any>;

const matchImport: TMethodDecorator = (target, propertyKey, descriptor) => {
  const callback = descriptor.value;

  descriptor.value = function (...args: any[]) {
    console.log("decorator", this);
    return callback.call(this, ...args);
  };
};
相关推荐
MY_TEUCK17 小时前
【Java 后端】SpringBoot 登录认证与会话跟踪实战(JWT + Filter/Interceptor)
java·开发语言·spring boot
镜宇秋霖丶17 小时前
2026.5.6@霖宇博客制作中遇见的问题
前端·javascript·vue.js
QQ24221997917 小时前
基于python+微信小程序的家教管理系统_mh3j9
开发语言·python·微信小程序
沐知全栈开发18 小时前
JavaScript 条件语句
开发语言
RSTJ_162518 小时前
PYTHON+AI LLM DAY THREETY-SEVEN
开发语言·人工智能·python
吴声子夜歌18 小时前
Vue3——TypeScript基础
javascript·typescript
清水白石00818 小时前
《Python性能深潜:从对象分配开销到“小对象风暴”的破解之道(含实战与最佳实践)》
开发语言·python
Je1lyfish18 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#3 - QueryExecution
linux·c语言·开发语言·数据结构·数据库·c++·算法
小李子呢021118 小时前
前端八股Vue---Vue-router路由管理器
前端·javascript·vue.js
Brilliantwxx19 小时前
【C++】 vector(代码实现+坑点讲解)
开发语言·c++·笔记·算法