Skip to content

Instantly share code, notes, and snippets.

@starbeast
Last active April 16, 2019 07:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starbeast/6ae005dfa67ea4237e2defaf0f8618a6 to your computer and use it in GitHub Desktop.
Save starbeast/6ae005dfa67ea4237e2defaf0f8618a6 to your computer and use it in GitHub Desktop.
TypeScript mixins example
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