I apologize if it is a silly question. I am new with typescript and learning typescript decorators. I found a code: MethodDecorator to log the arguments and the result.
log decorator
function log (target: Object, key: string, descriptor: TypedPropertyDescriptor<any>) { let originalMethod = descriptor.value; descriptor.value = function (...args:any[]) { //before console.log(`${key} method called with args: ${JSON.stringify(args)}`); let result = originalMethod.apply(this, args); //after console.log(`${key} method return value: ${JSON.stringify(result)}`); } return descriptor;}
I am using @log with setId and getId methods in Book class
book.ts
class Book{ constructor(public id: number, public title: string, public publisher: string){} @log setId(id: number){ this.id = id; } @log getId(): number{ return this.id; }}
All code works fine but getId returns undefined when i run this code.
let book = new Book(1, "Learn TypeScript", "O\'Reilly Media");let favBookId = book.getId();console.log("Book before setId: ");console.log(book);console.log("Favourite book id: "+favBookId);book.setId(5);console.log("Book after setId: ");console.log(book);console.log("Favourite book id: "+favBookId);
my tsconfig.json
{"compilerOptions": {"target": "es5","module": "commonjs","removeComments": false,"experimentalDecorators": true }}
To compile and run:
tsc -p ./node book.js
Output:
getId method called with args: []getId method return value: 1Book before setId:Book {id: 1, title: 'Learn TypeScript', publisher: 'O\'Reilly Media' }Favourite book id: undefinedsetId method called with args: [5]setId method return value: undefinedBook after setId:Book {id: 5, title: 'Learn TypeScript', publisher: 'O\'Reilly Media' }Favourite book id: undefined
I am not able to understand why setId is working as I want and getId is not??
tsc -v: Version 1.8.10