I have a problem when trying to create a simple decorator function on typescript. I don't know whats happening.
This is the decorator:
//Creating a decorator for loging the execution tyme of a method
export function logExecutionTime() {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
return <any>descriptor;
};
}
and when I invoke it on another file like this
@logExecutionTime();
I get the following error:
Unable to resolve signature of property decorator when called as an expression.ts(1240)
If i don't force the type of the decorator's return to be any I get the previous error and also this one:
Decorator function return type is 'PropertyDescriptor' but is expected to be 'void' or 'any'.ts(1271)
Can someone please help? I'm just learning about decorators. Thanks!
I tried to create and invoke a simple decorator;
At the place where the decorator function is called gives this error: Unable to resolve signature of property decorator when called as an expression.
If i don't force the type of the decorator's return to be any I get the previous error and also this one:
Decorator function return type is 'PropertyDescriptor' but is expected to be 'void' or 'any'.ts(1271)
The inner function should not return a value. In a decorator, you change the decorated element by manipulating its description data. Have a look at this example:
export function logExecutionTime() {
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor // this is the description we want to change
) {
const originalFunction = descriptor.value // store the declared fuction
descriptor.value = function(...args){ // set a new function
... // set up your timer
const result = originalFunction.apply(this, args) // run the declared function
... // process timer result
return result
}
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.