Skip to content

Instantly share code, notes, and snippets.

@sergebat
Last active September 5, 2018 09:47
Show Gist options
  • Save sergebat/2ee8faf2b561a8b31683acac3d094211 to your computer and use it in GitHub Desktop.
Save sergebat/2ee8faf2b561a8b31683acac3d094211 to your computer and use it in GitHub Desktop.
Method call log with experimental Typescript decorators
const DEBUG = false;
const logDebug = function(_target: any, key: string, descriptor: PropertyDescriptor): any {
const originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
const functionName = key;
console.log(functionName + "(" + args.join(", ") + ")");
const result = originalMethod.apply(this, args);
console.log("=> " + result);
return result;
};
return descriptor;
};
const logNoop = function() {};
const log = DEBUG ? logDebug : logNoop;
class Test {
@log
test(a: number, b: number) {
}
}
new Test().test(1, 2);
new Test().test(3, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment