Skip to content

Instantly share code, notes, and snippets.

@prokopsimek
Created January 13, 2019 21:16
Show Gist options
  • Save prokopsimek/586dacf54a4b328aae9406d22db0574b to your computer and use it in GitHub Desktop.
Save prokopsimek/586dacf54a4b328aae9406d22db0574b to your computer and use it in GitHub Desktop.
TypeScript decorators introduction test
// src.: http://blog.wolksoftware.com/decorators-reflection-javascript-typescript
// run: ts-node decorators.ts
function log(target: any, key: string, value: any) {
return {
value(...args: any[]) {
let a = args.map(a => JSON.stringify(a)).join()
let result = value.value.apply(this, args)
let r = JSON.stringify(result)
console.log(`Call: ${key}(${a}) => ${r}`)
return result
}
}
}
class C {
@log
foo(n: number) {
return n * 2
}
}
new C().foo(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment