Created
January 13, 2019 21:16
-
-
Save prokopsimek/586dacf54a4b328aae9406d22db0574b to your computer and use it in GitHub Desktop.
TypeScript decorators introduction test
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
// 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