Created
July 14, 2023 12:54
-
-
Save mikaelvesavuori/077dd94465f0d47c6e9b9293d485f121 to your computer and use it in GitHub Desktop.
TypeScript 5.0 decorators.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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