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);
  };
};
相关推荐
basketball61612 分钟前
C++ 命名空间知识点总结:从入门到合理设计
开发语言·c++
WL_Aurora15 分钟前
Java多线程详解(一)
java·开发语言
RSTJ_162529 分钟前
PYTHON+AI LLM DAY FOURTY-EIGHT
开发语言·人工智能·python·深度学习
南宫萧幕29 分钟前
HEV能量管理建模实战:从零搭建 Simulink 物理环境到 Python(DQN) 强化学习联合仿真调通
开发语言·python·算法·matlab·汽车·控制
lsx20240638 分钟前
C++ 接口(抽象类)
开发语言
handler0140 分钟前
【C++ 算法竞赛基础】数论篇:核心公式、经典例题与高频模板
开发语言·c++·算法·蓝桥杯·数论·最大公约数·最小公倍数
humcomm42 分钟前
2026年 Java 面试新特点
java·开发语言·面试
测试员周周1 小时前
【Appium 系列】第12节-智能路由 — API测试 vs UI 测试的自动选择
开发语言·人工智能·python·功能测试·ui·appium·测试用例
liudanzhengxi1 小时前
AnthropicAPI连接超时:实战避坑指南
开发语言·php
张二娃同学1 小时前
01_C语言学习路线与开发环境搭建
c语言·开发语言·学习