I have a field in a typescript class which has a decorator A, but i want change the decorator from A to B when it is running in some specific environment. Can this be achieved in typescript?
class SampleClass {
@DecoratorA
sampleField;
constructor() {
if(condition) {
// change DecoratorA to DecoratorB
}
}
}
Is it possible to change the DecoratorA to DecoratorB in the constructor based on some condition?
So as suggested in comments create a decorator factory:
function AorB() {
return condition ? DecoratorA : DecoratorB;
}
And apply it (pay attention to parentheses):
class SampleClass {
@AorB()
sampleField;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With