From 6ee14228a7b9ffa81cc0a573bf3bf9955dd04d44 Mon Sep 17 00:00:00 2001 From: gulewei11022 Date: Thu, 11 Aug 2022 16:47:26 +0800 Subject: [PATCH] feat(types): new Vue() improvements (#12730) --- types/options.d.ts | 28 ++++-- types/test/vue-test.ts | 14 +++ types/v3-component-public-instance.d.ts | 4 +- types/vue.d.ts | 120 +++++++++++++++++++----- 4 files changed, 135 insertions(+), 31 deletions(-) diff --git a/types/options.d.ts b/types/options.d.ts index e11020f4948..ca619bf5323 100644 --- a/types/options.d.ts +++ b/types/options.d.ts @@ -93,7 +93,9 @@ export type ThisTypedComponentOptionsWithArrayProps< Methods, Computed, PropNames extends string, - SetupBindings + SetupBindings, + Mixin, + Extends > = object & ComponentOptions< V, @@ -103,15 +105,19 @@ export type ThisTypedComponentOptionsWithArrayProps< PropNames[], Record, SetupBindings - > & - ThisType< + > & { + mixin?: Mixin[] + extends?: Extends + } & ThisType< CombinedVueInstance< V, Data, Methods, Computed, Readonly>, - SetupBindings + SetupBindings, + Mixin, + Extends > > @@ -124,7 +130,9 @@ export type ThisTypedComponentOptionsWithRecordProps< Methods, Computed, Props, - SetupBindings + SetupBindings, + Mixin, + Extends > = object & ComponentOptions< V, @@ -134,15 +142,19 @@ export type ThisTypedComponentOptionsWithRecordProps< RecordPropsDefinition, Props, SetupBindings - > & - ThisType< + > & { + mixin?: Mixin[] + extends?: Extends + } & ThisType< CombinedVueInstance< V, Data, Methods, Computed, Readonly, - SetupBindings + SetupBindings, + Mixin, + Extends > > diff --git a/types/test/vue-test.ts b/types/test/vue-test.ts index 8489f3ae8ed..253d667b461 100644 --- a/types/test/vue-test.ts +++ b/types/test/vue-test.ts @@ -246,3 +246,17 @@ const ComponentWithStyleInVNodeData = Vue.extend({ ]) } }) + +// infer mixin type with new Vue() #12730 +new Vue({ + mixin: [ + { + methods: { + hello() {} + } + } + ], + created() { + this.hello() + } +}) diff --git a/types/v3-component-public-instance.d.ts b/types/v3-component-public-instance.d.ts index 3586f4031e8..1c55908ac73 100644 --- a/types/v3-component-public-instance.d.ts +++ b/types/v3-component-public-instance.d.ts @@ -79,11 +79,11 @@ type ExtractMixin = { Mixin: MixinToOptionTypes }[T extends ComponentOptionsMixin ? 'Mixin' : never] -type IntersectionMixin = IsDefaultMixinComponent extends true +export type IntersectionMixin = IsDefaultMixinComponent extends true ? OptionTypesType<{}, {}, {}, {}, {}, {}> : UnionToIntersection> -type UnwrapMixinsType< +export type UnwrapMixinsType< T, Type extends OptionTypesKeys > = T extends OptionTypesType ? T[Type] : never diff --git a/types/vue.d.ts b/types/vue.d.ts index 8c91bc88a2f..3c2ccb35798 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -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 { ( @@ -94,18 +98,28 @@ export interface Vue< $createElement: CreateElement } +type ComponentMixin = ComponentOptions + export type CombinedVueInstance< Instance extends Vue, Data, Methods, Computed, Props, - SetupBindings = {} -> = Data & + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin, + PublicMixin = IntersectionMixin & IntersectionMixin +> = UnwrapMixinsType & + Data & + UnwrapMixinsType & Methods & + UnwrapMixinsType & Computed & + UnwrapMixinsType & Props & Instance & + UnwrapMixinsType & (SetupBindings extends void ? {} : SetupBindings) export type ExtendedVue< @@ -114,9 +128,20 @@ export type ExtendedVue< Methods, Computed, Props, - SetupBindings = {} + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin > = VueConstructor< - CombinedVueInstance & + CombinedVueInstance< + Instance, + Data, + Methods, + Computed, + Props, + SetupBindings, + Mixin, + Extends + > & Vue > @@ -142,7 +167,9 @@ export interface VueConstructor { Methods = object, Computed = object, PropNames extends string = never, - SetupBindings = {} + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin >( options?: ThisTypedComponentOptionsWithArrayProps< V, @@ -150,7 +177,9 @@ export interface VueConstructor { Methods, Computed, PropNames, - SetupBindings + SetupBindings, + Mixin, + Extends > ): CombinedVueInstance< V, @@ -158,7 +187,9 @@ export interface VueConstructor { Methods, Computed, Record, - SetupBindings + SetupBindings, + Mixin, + Extends > /** @@ -172,7 +203,9 @@ export interface VueConstructor { Methods = object, Computed = object, Props = object, - SetupBindings = {} + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin >( options?: ThisTypedComponentOptionsWithRecordProps< V, @@ -180,7 +213,9 @@ export interface VueConstructor { Methods, Computed, Props, - SetupBindings + SetupBindings, + Mixin, + Extends > ): CombinedVueInstance< V, @@ -188,7 +223,9 @@ export interface VueConstructor { Methods, Computed, Record, - SetupBindings + SetupBindings, + Mixin, + Extends > /** @@ -211,7 +248,9 @@ export interface VueConstructor { Methods, Computed, PropNames extends string = never, - SetupBindings = {} + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin >( options?: ThisTypedComponentOptionsWithArrayProps< V, @@ -219,7 +258,9 @@ export interface VueConstructor { Methods, Computed, PropNames, - SetupBindings + SetupBindings, + Mixin, + Extends > ): ExtendedVue< V, @@ -227,22 +268,43 @@ export interface VueConstructor { Methods, Computed, Record, - SetupBindings + SetupBindings, + Mixin, + Extends > /** * extend with object props */ - extend( + 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 + ): ExtendedVue< + V, + Data, + Methods, + Computed, + Props, + SetupBindings, + Mixin, + Extends + > /** * extend with functional + array props @@ -287,7 +349,9 @@ export interface VueConstructor { Methods, Computed, PropNames extends string = never, - SetupBindings = {} + SetupBindings = {}, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin >( id: string, definition?: ThisTypedComponentOptionsWithArrayProps< @@ -296,7 +360,9 @@ export interface VueConstructor { Methods, Computed, PropNames, - SetupBindings + SetupBindings, + Mixin, + Extends > ): ExtendedVue< V, @@ -304,9 +370,19 @@ export interface VueConstructor { Methods, Computed, Record, - SetupBindings + SetupBindings, + Mixin, + Extends > - component( + component< + Data, + Methods, + Computed, + Props, + SetupBindings, + Mixin extends ComponentMixin = ComponentMixin, + Extends extends ComponentMixin = ComponentMixin + >( id: string, definition?: ThisTypedComponentOptionsWithRecordProps< V, @@ -314,7 +390,9 @@ export interface VueConstructor { Methods, Computed, Props, - SetupBindings + SetupBindings, + Mixin, + Extends > ): ExtendedVue component(