OnChange TypeScript decorator

Example fro article https://habrahabr.ru/post/310870/

by Pavel Bogatyrev

TypeScript

class MyClass {
	
	@safe public foo(str: string, data: any): boolean {
    return str.length > 0;
  }
}

function safe(target: Object, propertyKey: string) {
  var descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
  var originalMethod = descriptor.value;
  descriptor.value = function NoErrorWrapper () {
    try {
      originalMethod.apply(this, arguments);
    } catch(ex) {
      var message = ex.name + " occured in call of " + 
          target.constructor.name + "." + propertyKey + ": " + ex.message + "\n";
      message += "Arguments:\n";
      [].forEach.call(arguments, (x, i) => 
         message += "   [" + i + "]: " + JSON.stringify(x) + " \n");
      message += "Callstack:" + ex.stack;
      console.error(message);
      document.body.innerHTML += "<br><pre style='color: red'>" + message + "</pre>";
    }
  };
  Object.defineProperty(target, propertyKey, descriptor);
  return descriptor;
}

console.info("Starting...");
document.body.innerHTML += "Starting...<br>";
var my = new MyClass();
my.foo(null, { answer: 42 });
console.info("Continue execution");
document.body.innerHTML += "<br>Continue execution";