Skip to content

Instantly share code, notes, and snippets.

@antongolub
Last active January 30, 2020 13:14
Show Gist options
  • Save antongolub/800f163422d69a9c8492140a9248745d to your computer and use it in GitHub Desktop.
Save antongolub/800f163422d69a9c8492140a9248745d to your computer and use it in GitHub Desktop.
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
}
export type IExtendsCondition<T, E, R1, R2> = T extends E
? R1
: R2
export type InstanceTypeOrType<T> = IExtendsCondition<T, IConstructable, InstanceType<IConstructable & T>, T>
export type ConstuctableOrEmpty<T> = IExtendsCondition<T, IConstructable, T, {}>
export type UnionToIntersectionOfConstructables<U> = (U extends any
? (k: ConstuctableOrEmpty<U>) => void
: never) extends (k: infer I) => void
? I
: never
export type UnionToIntersectionOfInstances<U> = (U extends any
? (k: InstanceTypeOrType<U>) => void
: never) extends (k: infer I) => void
? I
: never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment