Skip to content

Instantly share code, notes, and snippets.

@sothmann
Created August 4, 2015 12:14
Show Gist options
  • Save sothmann/915b13fdce147e6e1a1e to your computer and use it in GitHub Desktop.
Save sothmann/915b13fdce147e6e1a1e to your computer and use it in GitHub Desktop.
TypeScript Decorator - Log method calls
function log(target, key, descriptor: PropertyDescriptor) {
var originalMethod = descriptor.value;
descriptor.value = function(...args: any[]) {
let functionName = key;
console.log(functionName + "(" + args.join(", ") + ")");
let result = originalMethod.apply(this, args);
console.log("=> " + result);
return result;
};
return descriptor;
}
class Person {
@log
greet(name: string): string {
return `hello ${name}`;
}
}
new Person().greet("world");
@dkmin
Copy link

dkmin commented Feb 10, 2023

result is

greet(world)
=> hello world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment