-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support component options mixin (#2)
* feat: make mixins function argument variadic * feat: support component options object mixins
- Loading branch information
Showing
2 changed files
with
60 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import Vue, { VueConstructor } from 'vue' | ||
import Vue, { VueConstructor, ComponentOptions } from 'vue' | ||
|
||
export type VueClass<T> = VueConstructor<T & Vue> | ||
type VueMixin = VueConstructor | ComponentOptions<never> | ||
|
||
type UnionToIntersection<U> = (U extends any | ||
? (k: U) => void | ||
: never) extends (k: infer I) => void | ||
? I | ||
: never | ||
|
||
export type MixedVueConstructor< | ||
Ctors extends VueConstructor[] | ||
> = Ctors extends VueConstructor<infer Vs>[] | ||
? VueClass<UnionToIntersection<Vs>> | ||
type ExtractInstance<T> = T extends VueConstructor<infer V> | ||
? V | ||
: T extends ComponentOptions<infer V> | ||
? V | ||
: never | ||
|
||
export default function mixins<Ctors extends VueConstructor[]>( | ||
...Ctors: Ctors | ||
): MixedVueConstructor<Ctors> | ||
export default function mixins(...Ctors: VueConstructor[]): VueConstructor { | ||
return Vue.extend({ mixins: Ctors }) | ||
type MixedVueConstructor<Mixins extends VueMixin[]> = Mixins extends (infer T)[] | ||
? VueConstructor<UnionToIntersection<ExtractInstance<T>> & Vue> | ||
: never | ||
|
||
export default function mixins<Mixins extends VueMixin[]>( | ||
...mixins: Mixins | ||
): MixedVueConstructor<Mixins> | ||
export default function mixins( | ||
...mixins: (VueConstructor | ComponentOptions<Vue>)[] | ||
): VueConstructor { | ||
return Vue.extend({ mixins }) | ||
} |
This file contains 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