Skip to content

Instantly share code, notes, and snippets.

@awinogradov
Created April 5, 2018 12:55
Show Gist options
  • Save awinogradov/89d05590b9fd208e285f50eaa39bee82 to your computer and use it in GitHub Desktop.
Save awinogradov/89d05590b9fd208e285f50eaa39bee82 to your computer and use it in GitHub Desktop.
Shit
export function compose<T0, T1 extends T0, T2 extends T0>(a: T0, b:T1, c: T2): T1 & T2;
export function compose<T0, T1>(a: T0, b:T1): T0 & T1;
function combineClasses(to, from) {
to.super_ = from;
Object.setPrototypeOf(to.prototype, from.prototype);
return Object.setPrototypeOf(to, from);
}
export function compose(...args) {
const ResultClass = args.splice(0, 1)[0];
return args.reduce((acc, Mixin) => combineClasses(Mixin, acc), ResultClass) as any;
}
import { compose } from './index.js';
interface IMyBlock {
a: string;
}
class MyBlock {
public block = 'MyBlock';
public a: string;
constructor(props: IMyBlock) {
this.a = props.a;
}
public attrs(props, state): any {
return {};
}
}
class MyBlockMod11 extends MyBlock {
// constructor(props: { b: string}) {
// super(props);
// console.log('MyBlockMod11', this.a);
// this.a += 1;
// }
public tag(props, state) {
return 'b';
}
}
class MyBlockMod12 extends MyBlockMod11 {
public tag(props, state) {
return 'c' + super.tag(props, state);
}
public attrs() {
return { id: 'the-id' };
}
}
class MyBlockMod21 extends MyBlock {
public b: number;
// constructor(props: IMyBlock) {
// super(props);
// console.log('MyBlockMod21', this.a);
// this.b = this.a + 1;
// }
public abc() {}
public attrs(props, state) {
return { name: 'the-name', ...super.attrs(props, state) };
}
}
const Test = compose(MyBlock, MyBlockMod12, MyBlockMod21);
const res = new Test({ a: 1 }); // должно ругаться на невалидные опции конструктору
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment