Skip to content

Instantly share code, notes, and snippets.

@frontdevops
Last active January 22, 2016 09:50
Show Gist options
  • Save frontdevops/7677f3015bd0407ac8ef to your computer and use it in GitHub Desktop.
Save frontdevops/7677f3015bd0407ac8ef to your computer and use it in GitHub Desktop.
TypeScript static property decorator example
/**
* Декоратор для логирования статических свойств
* @param target
* @param key
*/
function log_static_prop(target:any, key:string):any {
var val = target[key];
return {
get: ():any => {
console.log(`Get: ${key} => ${val}`);
return val;
},
set: (newVal):void => console.log(`Set: ${key} => ${newVal}`)
}
}
class Person {
public name:string;
public surname:string;
@log_static_prop public static vip:boolean = false;
constructor(name:string, surname:string) {
this.name = name;
this.surname = surname;
}
}
let me = new Person("Alex", "Mayorov");
Person.vip;
Person.vip = true;
Person.vip;
/* Output:
Get: vip => false
Set: vip => true
Get: vip => false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment