Last active
April 16, 2019 07:56
-
-
Save starbeast/6ae005dfa67ea4237e2defaf0f8618a6 to your computer and use it in GitHub Desktop.
TypeScript mixins example
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
class Point { | |
constructor(public x: number, public y: number) {} | |
} | |
class Person { | |
constructor(public name: string) {} | |
} | |
type Constructor<T> = new(...args: any[]) => T; | |
function Tagged<T extends Constructor<{}>>(Base: T) { | |
return class extends Base { | |
_tag: string; | |
constructor(...args: any[]) { | |
super(...args); | |
this._tag = ""; | |
} | |
} | |
} | |
const TaggedPoint = Tagged(Point); | |
let point = new TaggedPoint(10, 20); | |
point._tag = "hello"; | |
class Customer extends Tagged(Person) { | |
accountBalance: number; | |
} | |
let customer = new Customer("Joe"); | |
customer._tag = "test"; | |
customer.accountBalance = 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment