How to apply a decorator to existing class

197 Views Asked by At

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

2

There are 2 best solutions below

1
Dimava On BEST ANSWER

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)

1
Behemoth On

I'm not sure if what you're trying to achieve is considered a good pattern, but you could add another property using Mixins. Class decorators cannot add additional properties in a type safe way.

class A {
  mess: string;
}

function test<T extends {new (...args: any[]): {}}>(constructor: T) {
  return class extends constructor {
    customProperty = "customProperty";
  };
}

const a = new (test(A))();

console.log(a.customProperty);

TypeScript Playground


Edit: If you really wanted to, you could declare a new class that is derived from A and turn off the type constraints using any in order to access the new members.

class A {
  mess: string;
}

function test<T extends {new (...args: any[]): {}}>(constructor: T) {
  return class extends constructor {
    customProperty = "customProperty";
  };
}

@test
class ExtendedA extends A {}

const a: any = new ExtendedA();
console.log(a.customProperty);

TypeScript Playground