Skip to content

Instantly share code, notes, and snippets.

@fukata
Last active May 5, 2020 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukata/b94cfcc2ed88b0ebb6c8a4e4e80594cd to your computer and use it in GitHub Desktop.
Save fukata/b94cfcc2ed88b0ebb6c8a4e4e80594cd to your computer and use it in GitHub Desktop.
export default null // モジュールモードを強制します
type DebuggableClassConstructor = {
new(...args: any[]): {getDebugValue(): object}
}
function debuggable<
C extends DebuggableClassConstructor
>(Class: C) {
return class extends Class {
debug() {
console.log(this.getDebugValue())
}
}
}
class User {
constructor(
private name: string
) {}
getDebugValue() {
return {
name: this.name
}
}
}
/*
let user = new User('fukata')
user.debug()
// decorator.ts:31:6 - error TS2339: Property 'debug' does not exist on type 'User'.
*/
let user = new (debuggable(User))('fukata')
user.debug()
// $./node_modules/.bin/ts-node decorator.ts
// { name: 'fukata' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment