Skip to content

Instantly share code, notes, and snippets.

@fukata

fukata/mixin.ts

Created May 5, 2020
Embed
What would you like to do?
export default null // モジュールモードを強制します
class User {
constructor (
private name: string
) {}
getDebugValue() {
return {
name: this.name
}
}
}
type DebuggableClassConstructor = {
new(...args: any[]): {getDebugValue(): object}
}
function withDebug<C extends DebuggableClassConstructor>(Class: C) {
return class extends Class {
debug() {
console.log(this.getDebugValue())
}
}
}
let user = new (withDebug(User))('fukata')
user.debug()
// $./node_modules/.bin/ts-node mixin.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.