Based on this answer, I tried to implement a function from another service inside a decorator.
Here is my attempt:
export function ClearCache() {
const injectCacheService = Inject(CacheService);
return (target: any, _: string, propertyDescriptor: PropertyDescriptor) => {
injectCacheService(target, 'service'); // this is the same as using constructor(private readonly logger: LoggerService) in a class
//get original method
const originalMethod = propertyDescriptor.value;
//redefine descriptor value within own function block
propertyDescriptor.value = async function (...args: any[]) {
try {
console.log(this);
const service: CacheService = this.service;
service.clearCacheById(args[1]);
return await originalMethod.apply(this, args);
} catch (error) {
console.error(error);
}
};
};
}
But I got Property 'service' does not exist on type 'PropertyDescriptor'.ts(2339)
Any idea on what part I am missing?
Update:
tsconfig.json has "strict":true. If it is enabled, then "this" would be strictly use the PropertyDescriptor interface.
Fixed the issue by changing "strict": false in the tsconfig.json.
How to reproduce?
- Use "strict":true on tsconfig.json