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
Answers
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.
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 usingany
in order to access the new members.TypeScript Playground
Typescript applies experimantal(legacy) decorators like:
So, to apply your decorator programmatically, all you need is to
(you may need to copy the __decorate implementation)