I have a typescript decorator factory which console logs total time taken to execute a function, actual function execution results and parameters passed to the decorator as well.
e.g.
export function performaceLog(...args: any[]) {return (target: Object, key: string, descriptor: TypedPropertyDescriptor<any>) => { var msg = ''; if (args.length != 0) { msg = args.map(arg => arg).join(''); } if (descriptor === undefined) { descriptor = Object.getOwnPropertyDescriptor(target, key); } if (typeof descriptor.value !== 'function') { throw new SyntaxError('Only functions can be used with log decorators'); } var originalMethod = descriptor.value.bind(target); descriptor.value = function() { var funcArgs: any = []; for (var i = 0; i < arguments.length; i++) { funcArgs[i - 0] = arguments[i]; } var startTime = performance.now(); console.log(`Begin function ${key} with params (${funcArgs}) : ${msg}`); var result = originalMethod.apply(this, funcArgs); var endTime = performance.now(); console.log(`End function ${key}. Execution time: ${endTime - startTime} milliseconds. Return result : ${result}`); return result; }; return descriptor;};}
I am using the above decorator with a function which is present in a class:(considering my class has other methods as well like ctor, get, set and utils).
class LoggerService { @performaceLog('validate', 'message') handleMessage(message) { console.log(message); return true; };}
Function call looks like this:
handleMessage('decoratorValidation');
This gives me perfect output as:
Begin function handleMessage with params (decoratorValidation) : validate message decoratorValidationEnd function handleMessage. Execution time: 0.3000000142492354 milliseconds. Return result : true
But when I change the function handleMessage to support arrow format (ES6), it throws me an error:
@performaceLog('validate', 'message')handleMessage = message => { console.log(message); return true;};
Error Message:
Unable to resolve signature of property decorator when called as an expression.
I have all the parameters set correctly in my tsconfig.json. I have my entire project supporting "ES6" target and I want the decorator to support arrow functions.