Skip to content

Commit

Permalink
feat(types): new Vue() improvements (vuejs#12730)
Browse files Browse the repository at this point in the history
  • Loading branch information
gulewei11022 committed Aug 11, 2022
1 parent 00458cd commit 6ee1422
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 31 deletions.
28 changes: 20 additions & 8 deletions types/options.d.ts
Expand Up @@ -93,7 +93,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Methods,
Computed,
PropNames extends string,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -103,15 +105,19 @@ export type ThisTypedComponentOptionsWithArrayProps<
PropNames[],
Record<PropNames, any>,
SetupBindings
> &
ThisType<
> & {
mixin?: Mixin[]
extends?: Extends
} & ThisType<
CombinedVueInstance<
V,
Data,
Methods,
Computed,
Readonly<Record<PropNames, any>>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand All @@ -124,7 +130,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -134,15 +142,19 @@ export type ThisTypedComponentOptionsWithRecordProps<
RecordPropsDefinition<Props>,
Props,
SetupBindings
> &
ThisType<
> & {
mixin?: Mixin[]
extends?: Extends
} & ThisType<
CombinedVueInstance<
V,
Data,
Methods,
Computed,
Readonly<Props>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand Down
14 changes: 14 additions & 0 deletions types/test/vue-test.ts
Expand Up @@ -246,3 +246,17 @@ const ComponentWithStyleInVNodeData = Vue.extend({
])
}
})

// infer mixin type with new Vue() #12730
new Vue({
mixin: [
{
methods: {
hello() {}
}
}
],
created() {
this.hello()
}
})
4 changes: 2 additions & 2 deletions types/v3-component-public-instance.d.ts
Expand Up @@ -79,11 +79,11 @@ type ExtractMixin<T> = {
Mixin: MixinToOptionTypes<T>
}[T extends ComponentOptionsMixin ? 'Mixin' : never]

type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
export type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
? OptionTypesType<{}, {}, {}, {}, {}, {}>
: UnionToIntersection<ExtractMixin<T>>

type UnwrapMixinsType<
export type UnwrapMixinsType<
T,
Type extends OptionTypesKeys
> = T extends OptionTypesType ? T[Type] : never
Expand Down
120 changes: 99 additions & 21 deletions types/vue.d.ts
Expand Up @@ -14,6 +14,10 @@ import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
import { PluginFunction, PluginObject } from './plugin'
import { DefineComponent } from './v3-define-component'
import { nextTick } from './v3-generated'
import {
UnwrapMixinsType,
IntersectionMixin
} from './v3-component-public-instance'

export interface CreateElement {
(
Expand Down Expand Up @@ -94,18 +98,28 @@ export interface Vue<
$createElement: CreateElement
}

type ComponentMixin = ComponentOptions<any, any, any, any, any>

export type CombinedVueInstance<
Instance extends Vue,
Data,
Methods,
Computed,
Props,
SetupBindings = {}
> = Data &
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin,
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>
> = UnwrapMixinsType<PublicMixin, 'D'> &
Data &
UnwrapMixinsType<PublicMixin, 'M'> &
Methods &
UnwrapMixinsType<PublicMixin, 'C'> &
Computed &
UnwrapMixinsType<PublicMixin, 'P'> &
Props &
Instance &
UnwrapMixinsType<PublicMixin, 'B'> &
(SetupBindings extends void ? {} : SetupBindings)

export type ExtendedVue<
Expand All @@ -114,9 +128,20 @@ export type ExtendedVue<
Methods,
Computed,
Props,
SetupBindings = {}
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
> = VueConstructor<
CombinedVueInstance<Instance, Data, Methods, Computed, Props, SetupBindings> &
CombinedVueInstance<
Instance,
Data,
Methods,
Computed,
Props,
SetupBindings,
Mixin,
Extends
> &
Vue
>

Expand All @@ -142,23 +167,29 @@ export interface VueConstructor<V extends Vue = Vue> {
Methods = object,
Computed = object,
PropNames extends string = never,
SetupBindings = {}
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
options?: ThisTypedComponentOptionsWithArrayProps<
V,
Data,
Methods,
Computed,
PropNames,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): CombinedVueInstance<
V,
Data,
Methods,
Computed,
Record<PropNames, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
>

/**
Expand All @@ -172,23 +203,29 @@ export interface VueConstructor<V extends Vue = Vue> {
Methods = object,
Computed = object,
Props = object,
SetupBindings = {}
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
options?: ThisTypedComponentOptionsWithRecordProps<
V,
Data,
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): CombinedVueInstance<
V,
Data,
Methods,
Computed,
Record<keyof Props, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
>

/**
Expand All @@ -211,38 +248,63 @@ export interface VueConstructor<V extends Vue = Vue> {
Methods,
Computed,
PropNames extends string = never,
SetupBindings = {}
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
options?: ThisTypedComponentOptionsWithArrayProps<
V,
Data,
Methods,
Computed,
PropNames,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): ExtendedVue<
V,
Data,
Methods,
Computed,
Record<PropNames, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
>

/**
* extend with object props
*/
extend<Data, Methods, Computed, Props, SetupBindings = {}>(
extend<
Data,
Methods,
Computed,
Props,
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
options?: ThisTypedComponentOptionsWithRecordProps<
V,
Data,
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
): ExtendedVue<
V,
Data,
Methods,
Computed,
Props,
SetupBindings,
Mixin,
Extends
>

/**
* extend with functional + array props
Expand Down Expand Up @@ -287,7 +349,9 @@ export interface VueConstructor<V extends Vue = Vue> {
Methods,
Computed,
PropNames extends string = never,
SetupBindings = {}
SetupBindings = {},
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
id: string,
definition?: ThisTypedComponentOptionsWithArrayProps<
Expand All @@ -296,25 +360,39 @@ export interface VueConstructor<V extends Vue = Vue> {
Methods,
Computed,
PropNames,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): ExtendedVue<
V,
Data,
Methods,
Computed,
Record<PropNames, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
component<Data, Methods, Computed, Props, SetupBindings>(
component<
Data,
Methods,
Computed,
Props,
SetupBindings,
Mixin extends ComponentMixin = ComponentMixin,
Extends extends ComponentMixin = ComponentMixin
>(
id: string,
definition?: ThisTypedComponentOptionsWithRecordProps<
V,
Data,
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
>
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
component<PropNames extends string>(
Expand Down

0 comments on commit 6ee1422

Please sign in to comment.