I'm attempting to enhance a setter function within a class in the following manner:
export class SearchResultSortBy{
private sortByChoice;
constructor() { ...}
/* getters & setters */
get sortBy() {
return this.sortByChoice;
};
@myDeco({o:'something',o2:'another something'})
set sortBy(sortBy) {
this.sortByChoice = sortBy;
};
}
utilizing the given decorator:
export function myDeco(element:any){
return (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
descriptor.set = function(...args: any[]) {
console.log("The method args are: " + JSON.stringify(args)); // pre
var result = originalMethod.apply(this, args); // execute and store the result
someOtherStaticClass.someFunc(element); // post
return result; // return the result of the original method
};
return descriptor;
};
}
however, the decorator is not being invoked/does not have any effect.
when I place the @myDeco above a getter function instead, the decorator is triggered and functions as intended, which is quite unusual... (decorates the setter func.)
is it feasible to decorate a setter function in typescript?