Search code examples
javascripttypescriptreflectionreflect-metadata

How to apply a decorator to existing class


I have class A in external package

class A {
  mess: string;
}

And my property decorator

function test(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  target.propertyKey = 'test';
};

I want to apply my decorator test to class A, but I can't get reflection of class A to apply

test(reflectionOfClassA, attributeKey)

How do I get reflection of class A? I can't update code of class A, because it is imported from other package


Solution

  • Typescript applies experimantal(legacy) decorators like:

    // ts code:
    import { Column } from "typeorm"
    
    export class User {
      @Column()
      age!: number
    }
    
    // compiled JS code:
    "use strict";
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.User = void 0;
    const typeorm_1 = require("typeorm");
    class User {
    }
    exports.User = User;
    __decorate([
        (0, typeorm_1.Column)()
    ], User.prototype, "age", void 0);
    

    So, to apply your decorator programmatically, all you need is to

    __decorate([Column()], User.prototype, "age", undefined)
    

    (you may need to copy the __decorate implementation)