Issue
I am following code with mosh typescript course while doing it I faced the following problem
The code below is working perfectly in mosh video but on my PC and in playground it's not working. I don't know this topic quit well so I don't understand what I'm doing wrong in my code
I hope u guys may helpful comment down below if anyone knows what's wrong going on with my code.
function Log(target: any, methodName: string, descriptor: PropertyDescriptor) {
const original = descriptor.value as Function;
descriptor.value = function (...args: any) {
console.log("before");
original.call(this, ...args);
console.log("after");
};
}
class Person {
@Log
say(message: string) {
console.log("Person says" + message);
}
}
let person = new Person();
person.say("hello");
Solution
Not sure what you mean by not working but if you paste your code into the typescript playground, you likely get the following error...
Unable to resolve signature of method decorator when called as an expression.
The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.
'target' is declared but its value is never read.
'methodName' is declared but its value is never read.
If you enable the experimentalDecorators
flag in the tsconfig.json
it works correctly, see playground. From the typescript docs, it says...
Decorators are a language feature which hasn’t yet been fully ratified into the JavaScript specification. This means that the implementation version in TypeScript may differ from the implementation in JavaScript when it it decided by TC39.
Answered By - Nickofthyme
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.