HolyJS Autumn 2023 https://holyjs.ru/talks/7a461a9694cc45c3bc0924b07b09d03f/
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
| type IMixinsApplier = <T extends IConstructable, U extends IConstructable[]>(target: T, ...mixins: U) => IMixedAsClass<T, U> | |
| type IMixedAsClass<T extends IConstructable, U extends any[]> = T | |
| & UnionToIntersectionOfConstructables<U[number]> | |
| & IConstructable<InstanceType<T> & UnionToIntersectionOfInstances<U[number]>> | |
| interface IConstructable<T = {}> extends Function { | |
| new (...args: any[]): T | |
| } |
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
| type IMixinApplier = <T extends IConstructable, M extends IConstructable>(target: T, mixin: M) => | |
| T & M & Class<InstanceType<T> & InstanceType<M>> |
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
| type Abstract<T= {}> = Function & {prototype: T} | |
| type Constructor<T= {}> = new (...args: any[]) => T | |
| type Class<T= {}> = Abstract<T> & Constructor<T> |
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 Foo { | |
| foo() { return 'foo' } | |
| } | |
| const foo: Foo = new Foo() // Foo is used as interface | |
| const applyMixins = <T, M>(target: T, mixin: M) => { | |
| // typeof target refers to own class type — a function with constuctor signature | |
| } |
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
| export const applyMixinsFixed = <T extends IConstructable, U extends IConstructable[]>(target: T, ...mixins: U) => { | |
| class Mixed extends target { | |
| constructor(...args: any[]) { | |
| super(...args) | |
| mergeDescriptors(this, ...mixins.map(M => new M(...args))) | |
| } | |
| } | |
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
| // https://www.typescriptlang.org/docs/handbook/mixins.html#mixin-sample | |
| function applyMixins(derivedCtor: any, baseCtors: any[]) { | |
| baseCtors.forEach(baseCtor => { | |
| Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { | |
| Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name)); | |
| }); | |
| }); | |
| } |