Skip to content

Instantly share code, notes, and snippets.

@mikaelvesavuori
Created July 14, 2023 12:54
Show Gist options
  • Save mikaelvesavuori/077dd94465f0d47c6e9b9293d485f121 to your computer and use it in GitHub Desktop.
Save mikaelvesavuori/077dd94465f0d47c6e9b9293d485f121 to your computer and use it in GitHub Desktop.
TypeScript 5.0 decorators.
class DecoratorDemo {
@log
public do() {
console.log('BLIP');
}
}
const d = new DecoratorDemo();
d.do();
function log(target: any, context: any) {
const methodName = context.name || 'Unknown';
function replacementMethod(this: any, ...args: any[]) {
console.log(`Before "${methodName}"`);
const result = target.call(this, ...args);
console.log(`After "${methodName}"`);
return result;
}
return replacementMethod;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment