Skip to content

Instantly share code, notes, and snippets.

@baflo
Last active March 17, 2019 08:39
Show Gist options
  • Save baflo/2411782afb3032feb283420b25ddeae4 to your computer and use it in GitHub Desktop.
Save baflo/2411782afb3032feb283420b25ddeae4 to your computer and use it in GitHub Desktop.
/**
* THIS IS IMPORTANT!!!
*
* If it's typed like `new (...args: any[]) => any` these examples won't work!
*/
type Ctor = new (...args: any[]) => {};
import { NamedMixin } from "./named";
import { TaggedMixin } from "./tagged";
type AnotherMixinRequirements = ReturnType<typeof NamedMixin> & ReturnType<typeof TaggedMixin>;
export function AnotherMixin<Super extends AnotherMixinRequirements>(
superClass: Super
) {
return class extends superClass {
public static ANOTHER: string;
public another: string;
protected another_protected: number;
private another_prrivate: number;
constructor(...args: any[]) {
super(...args);
this.name_protected
}
}
}
export function FullMixin<Super extends Ctor>(superClass: Super) {
return AnotherMixin(NamedMixin(TaggedMixin(superClass)));
}
import { TaggedMixin } from "./tagged";
import { NamedMixin } from "./named";
export function TaggedAndNamedMixin<Super extends Ctor>(superClass?: Super) {
return TaggedMixin(NamedMixin(superClass));
}
import { NamedMixin } from "./named";
import { TaggedMixin } from "./tagged";
import { TaggedAndNamedMixin } from "./combined";
import { AnotherMixin, FullMixin } from "./another";
const Named = NamedMixin(class {});
const Tagged = TaggedMixin(class {});
const TaggedAndNamed = TaggedAndNamedMixin(class {});
Named.NAME
Tagged.TAG
TaggedAndNamed.NAME
TaggedAndNamed.TAG
class X extends TaggedAndNamedMixin(class {}) {
constructor() {
super();
this.tag
this.name
this.name_protected
this.tag_protected
X.NAME
X.TAG
}
}
class Y extends AnotherMixin(TaggedMixin(NamedMixin())) {
constructor() {
super();
Y.NAME
Y.TAG
Y.ANOTHER
Y.NAME_PROTECTED
Y.TAG_PROTECTED
Y.ANOTHER_PROTECTED
this.name
this.tag
this.another
this.name_protected
this.tag_protected
this.another_protected
}
}
class Z extends FullMixin(class {}) {
constructor() {
super();
}
}
export type NamedMixin = ReturnType<typeof NamedMixin>;
export function NamedMixin<Super extends Ctor>(superClass?: Super) {
class Named extends superClass {
public static NAME: string;
public name: string;
protected name_protected: number;
private name_prrivate: number;
}
return Named;
}
export type TaggedMixin = ReturnType<typeof TaggedMixin>;
export function TaggedMixin<Super extends Ctor>(superClass?: Super) {
class Tagged extends superClass {
public static TAG: string;
public tag: string;
protected tag_protected: number;
private tag_prrivate: number;
}
return Tagged;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment