Skip to content

Instantly share code, notes, and snippets.

function logProperty(target: any, key: string) {
const newKey = `_${key}`;
Object.defineProperty(target, key, {
get() {
console.log(`Get: ${key} => ${this[newKey]}`);
return this[newKey];
},
set(newVal) {
console.log(`Set: ${key} => ${newVal}`);
this[newKey] = newVal;
},
enumerable: true,
configurable: true
});
}
class Task {
@logProperty
public id: number;
@logProperty
public title: string;
constructor(id : number, title : string) {
this.id = id;
this.title = title;
}
}
const task = new Task(1, 'Write more articles');
// Set: id => 1
// Set: title => Write more articles
console.log(task.id, task.title);
// Get: id => 1
// Get: title => Write more articles
// 1 'Write more articles'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment