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
const fn = < | |
T extends typeof Base, | |
Instance extends InstanceType<T> | |
>(Cls: T, prop: keyof Instance, | |
) => new Cls()[prop]; |
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 Base { } | |
function fn<T extends typeof Base>( | |
Cls: T, | |
prop: keyof InstanceType<T>, | |
) { | |
return (new Cls())[prop]; // error | |
} |
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 UnionToIntersection<U> = | |
(U extends any ? (k: U) => void : never) extends ( | |
k: infer I | |
) => void | |
? I | |
: never; | |
type ClassType = new (...args: any[]) => any; | |
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
// credits goes to https://stackoverflow.com/a/50375286 | |
type UnionToIntersection<U> = | |
(U extends any ? (k: U) => void : never) extends ( | |
k: infer I | |
) => void | |
? I | |
: never; | |
function Mixin<T extends ClassType, R extends T[]>(...classRefs: [...R]): | |
new (...args: any[]) => UnionToIntersection<InstanceType<[...R][number]>> { |
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 ClassType = new (...args: any[]) => any; | |
function Mixin<T extends ClassType, R extends T[]>(...classRefs: [...R]): InstanceType<R[number]> { | |
return null as any | |
} | |
class Foo { | |
foo() { } | |
} |
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
// added extra R generic | |
function Mixin<T extends ClassType, R extends T[]>(...classRefs: [...R]){ | |
return merge(class { }, ...classRefs); | |
} |
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
function Mixin<T extends ClassType>(...classRefs: T[]) { | |
return merge(class { }, ...classRefs); | |
} |
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 ClassType = new (...args: any[]) => any; | |
function merge(derived: ClassType, ...classRefs: ClassType[]) { | |
classRefs.forEach(classRef => { | |
Object.getOwnPropertyNames(classRef.prototype).forEach(name => { | |
const descriptor = Object.getOwnPropertyDescriptor(classRef.prototype, name) | |
// just add condition statement | |
if (name !== 'constructor' && descriptor) { | |
Object.defineProperty( | |
derived.prototype, |
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 ClassType = new (...args: any[]) => any; | |
// add ClassType | |
function merge(derived: ClassType, ...classRefs: ClassType[]) { | |
// same logic from previous version | |
return derived | |
} |
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 ClassType = new (...args: any[]) => any; |