Skip to content

Instantly share code, notes, and snippets.

@fukata

fukata/decorator.ts

Last active May 5, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.