mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 20:28:54 +00:00
feat(types): support mixins inference for new Vue() (#12737)
close #12730
This commit is contained in:
parent
b4bf4c52ad
commit
89a6b5e865
33
types/options.d.ts
vendored
33
types/options.d.ts
vendored
@ -3,6 +3,7 @@ import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
|
|||||||
import { SetupContext } from './v3-setup-context'
|
import { SetupContext } from './v3-setup-context'
|
||||||
import { DebuggerEvent } from './v3-generated'
|
import { DebuggerEvent } from './v3-generated'
|
||||||
import { DefineComponent } from './v3-define-component'
|
import { DefineComponent } from './v3-define-component'
|
||||||
|
import { ComponentOptionsMixin } from './v3-component-options'
|
||||||
|
|
||||||
type Constructor = {
|
type Constructor = {
|
||||||
new (...args: any[]): any
|
new (...args: any[]): any
|
||||||
@ -93,7 +94,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames extends string,
|
PropNames extends string,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
> = object &
|
> = object &
|
||||||
ComponentOptions<
|
ComponentOptions<
|
||||||
V,
|
V,
|
||||||
@ -102,7 +105,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
|
|||||||
Computed,
|
Computed,
|
||||||
PropNames[],
|
PropNames[],
|
||||||
Record<PropNames, any>,
|
Record<PropNames, any>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
> &
|
> &
|
||||||
ThisType<
|
ThisType<
|
||||||
CombinedVueInstance<
|
CombinedVueInstance<
|
||||||
@ -111,7 +116,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Readonly<Record<PropNames, any>>,
|
Readonly<Record<PropNames, any>>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
|
|
||||||
@ -124,7 +131,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
> = object &
|
> = object &
|
||||||
ComponentOptions<
|
ComponentOptions<
|
||||||
V,
|
V,
|
||||||
@ -133,7 +142,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
|
|||||||
Computed,
|
Computed,
|
||||||
RecordPropsDefinition<Props>,
|
RecordPropsDefinition<Props>,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
> &
|
> &
|
||||||
ThisType<
|
ThisType<
|
||||||
CombinedVueInstance<
|
CombinedVueInstance<
|
||||||
@ -142,7 +153,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Readonly<Props>,
|
Readonly<Props>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
|
|
||||||
@ -158,7 +171,9 @@ export interface ComponentOptions<
|
|||||||
Computed = DefaultComputed,
|
Computed = DefaultComputed,
|
||||||
PropsDef = PropsDefinition<DefaultProps>,
|
PropsDef = PropsDefinition<DefaultProps>,
|
||||||
Props = DefaultProps,
|
Props = DefaultProps,
|
||||||
RawBindings = {}
|
RawBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
> {
|
> {
|
||||||
data?: Data
|
data?: Data
|
||||||
props?: PropsDef
|
props?: PropsDef
|
||||||
@ -217,12 +232,12 @@ export interface ComponentOptions<
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent?: Vue
|
parent?: Vue
|
||||||
mixins?: (ComponentOptions<Vue> | typeof Vue)[]
|
mixins?: (Mixin | ComponentOptions<Vue> | typeof Vue)[]
|
||||||
name?: string
|
name?: string
|
||||||
// for SFC auto name inference w/ ts-loader check
|
// for SFC auto name inference w/ ts-loader check
|
||||||
__name?: string
|
__name?: string
|
||||||
// TODO: support properly inferred 'extends'
|
// TODO: support properly inferred 'extends'
|
||||||
extends?: ComponentOptions<Vue> | typeof Vue
|
extends?: Extends | ComponentOptions<Vue> | typeof Vue
|
||||||
delimiters?: [string, string]
|
delimiters?: [string, string]
|
||||||
comments?: boolean
|
comments?: boolean
|
||||||
inheritAttrs?: boolean
|
inheritAttrs?: boolean
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import Vue, { VNode } from '../index'
|
import Vue, { VNode, defineComponent } from '../index'
|
||||||
import { ComponentOptions } from '../options'
|
import { ComponentOptions } from '../options'
|
||||||
|
|
||||||
class Test extends Vue {
|
class Test extends Vue {
|
||||||
@ -246,3 +246,40 @@ const ComponentWithStyleInVNodeData = Vue.extend({
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// infer mixin type with new Vue() #12730
|
||||||
|
new Vue({
|
||||||
|
mixins: [
|
||||||
|
defineComponent({
|
||||||
|
props: {
|
||||||
|
p1: String,
|
||||||
|
p2: {
|
||||||
|
type: Number,
|
||||||
|
default: 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
foo: 123
|
||||||
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
bar() {
|
||||||
|
return 123
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
methods: {
|
||||||
|
hello(n: number) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
created() {
|
||||||
|
this.hello(this.foo)
|
||||||
|
this.hello(this.bar)
|
||||||
|
// @ts-expect-error
|
||||||
|
this.hello(this.p1)
|
||||||
|
this.hello(this.p2)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
4
types/v3-component-public-instance.d.ts
vendored
4
types/v3-component-public-instance.d.ts
vendored
@ -79,11 +79,11 @@ type ExtractMixin<T> = {
|
|||||||
Mixin: MixinToOptionTypes<T>
|
Mixin: MixinToOptionTypes<T>
|
||||||
}[T extends ComponentOptionsMixin ? 'Mixin' : never]
|
}[T extends ComponentOptionsMixin ? 'Mixin' : never]
|
||||||
|
|
||||||
type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
|
export type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
|
||||||
? OptionTypesType<{}, {}, {}, {}, {}, {}>
|
? OptionTypesType<{}, {}, {}, {}, {}, {}>
|
||||||
: UnionToIntersection<ExtractMixin<T>>
|
: UnionToIntersection<ExtractMixin<T>>
|
||||||
|
|
||||||
type UnwrapMixinsType<
|
export type UnwrapMixinsType<
|
||||||
T,
|
T,
|
||||||
Type extends OptionTypesKeys
|
Type extends OptionTypesKeys
|
||||||
> = T extends OptionTypesType ? T[Type] : never
|
> = T extends OptionTypesType ? T[Type] : never
|
||||||
|
124
types/vue.d.ts
vendored
124
types/vue.d.ts
vendored
@ -13,7 +13,15 @@ import {
|
|||||||
import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
|
import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
|
||||||
import { PluginFunction, PluginObject } from './plugin'
|
import { PluginFunction, PluginObject } from './plugin'
|
||||||
import { DefineComponent } from './v3-define-component'
|
import { DefineComponent } from './v3-define-component'
|
||||||
import { nextTick } from './v3-generated'
|
import { nextTick, UnwrapNestedRefs, ShallowUnwrapRef } from './v3-generated'
|
||||||
|
import {
|
||||||
|
UnwrapMixinsType,
|
||||||
|
IntersectionMixin
|
||||||
|
} from './v3-component-public-instance'
|
||||||
|
import {
|
||||||
|
ExtractComputedReturns,
|
||||||
|
ComponentOptionsMixin
|
||||||
|
} from './v3-component-options'
|
||||||
|
|
||||||
export interface CreateElement {
|
export interface CreateElement {
|
||||||
(
|
(
|
||||||
@ -100,12 +108,20 @@ export type CombinedVueInstance<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
> = Data &
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>
|
||||||
|
> = UnwrapNestedRefs<UnwrapMixinsType<PublicMixin, 'D'>> &
|
||||||
|
Data &
|
||||||
|
UnwrapMixinsType<PublicMixin, 'M'> &
|
||||||
Methods &
|
Methods &
|
||||||
|
ExtractComputedReturns<UnwrapMixinsType<PublicMixin, 'C'>> &
|
||||||
Computed &
|
Computed &
|
||||||
|
UnwrapMixinsType<PublicMixin, 'P'> &
|
||||||
Props &
|
Props &
|
||||||
Instance &
|
Instance &
|
||||||
|
ShallowUnwrapRef<UnwrapMixinsType<PublicMixin, 'B'>> &
|
||||||
(SetupBindings extends void ? {} : SetupBindings)
|
(SetupBindings extends void ? {} : SetupBindings)
|
||||||
|
|
||||||
export type ExtendedVue<
|
export type ExtendedVue<
|
||||||
@ -114,9 +130,20 @@ export type ExtendedVue<
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
> = VueConstructor<
|
> = VueConstructor<
|
||||||
CombinedVueInstance<Instance, Data, Methods, Computed, Props, SetupBindings> &
|
CombinedVueInstance<
|
||||||
|
Instance,
|
||||||
|
Data,
|
||||||
|
Methods,
|
||||||
|
Computed,
|
||||||
|
Props,
|
||||||
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
|
> &
|
||||||
Vue
|
Vue
|
||||||
>
|
>
|
||||||
|
|
||||||
@ -142,7 +169,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods = object,
|
Methods = object,
|
||||||
Computed = object,
|
Computed = object,
|
||||||
PropNames extends string = never,
|
PropNames extends string = never,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
>(
|
>(
|
||||||
options?: ThisTypedComponentOptionsWithArrayProps<
|
options?: ThisTypedComponentOptionsWithArrayProps<
|
||||||
V,
|
V,
|
||||||
@ -150,7 +179,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames,
|
PropNames,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
): CombinedVueInstance<
|
): CombinedVueInstance<
|
||||||
V,
|
V,
|
||||||
@ -158,7 +189,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Record<PropNames, any>,
|
Record<PropNames, any>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,7 +205,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods = object,
|
Methods = object,
|
||||||
Computed = object,
|
Computed = object,
|
||||||
Props = object,
|
Props = object,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
>(
|
>(
|
||||||
options?: ThisTypedComponentOptionsWithRecordProps<
|
options?: ThisTypedComponentOptionsWithRecordProps<
|
||||||
V,
|
V,
|
||||||
@ -180,7 +215,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
): CombinedVueInstance<
|
): CombinedVueInstance<
|
||||||
V,
|
V,
|
||||||
@ -188,7 +225,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Record<keyof Props, any>,
|
Record<keyof Props, any>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -211,7 +250,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames extends string = never,
|
PropNames extends string = never,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
>(
|
>(
|
||||||
options?: ThisTypedComponentOptionsWithArrayProps<
|
options?: ThisTypedComponentOptionsWithArrayProps<
|
||||||
V,
|
V,
|
||||||
@ -219,7 +260,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames,
|
PropNames,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
): ExtendedVue<
|
): ExtendedVue<
|
||||||
V,
|
V,
|
||||||
@ -227,22 +270,43 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Record<PropNames, any>,
|
Record<PropNames, any>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* extend with object props
|
* extend with object props
|
||||||
*/
|
*/
|
||||||
extend<Data, Methods, Computed, Props, SetupBindings = {}>(
|
extend<
|
||||||
|
Data,
|
||||||
|
Methods,
|
||||||
|
Computed,
|
||||||
|
Props,
|
||||||
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
|
>(
|
||||||
options?: ThisTypedComponentOptionsWithRecordProps<
|
options?: ThisTypedComponentOptionsWithRecordProps<
|
||||||
V,
|
V,
|
||||||
Data,
|
Data,
|
||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
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
|
* extend with functional + array props
|
||||||
@ -287,7 +351,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames extends string = never,
|
PropNames extends string = never,
|
||||||
SetupBindings = {}
|
SetupBindings = {},
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
>(
|
>(
|
||||||
id: string,
|
id: string,
|
||||||
definition?: ThisTypedComponentOptionsWithArrayProps<
|
definition?: ThisTypedComponentOptionsWithArrayProps<
|
||||||
@ -296,7 +362,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
PropNames,
|
PropNames,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
): ExtendedVue<
|
): ExtendedVue<
|
||||||
V,
|
V,
|
||||||
@ -304,9 +372,19 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Record<PropNames, any>,
|
Record<PropNames, any>,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
component<Data, Methods, Computed, Props, SetupBindings>(
|
component<
|
||||||
|
Data,
|
||||||
|
Methods,
|
||||||
|
Computed,
|
||||||
|
Props,
|
||||||
|
SetupBindings,
|
||||||
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||||
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
|
||||||
|
>(
|
||||||
id: string,
|
id: string,
|
||||||
definition?: ThisTypedComponentOptionsWithRecordProps<
|
definition?: ThisTypedComponentOptionsWithRecordProps<
|
||||||
V,
|
V,
|
||||||
@ -314,7 +392,9 @@ export interface VueConstructor<V extends Vue = Vue> {
|
|||||||
Methods,
|
Methods,
|
||||||
Computed,
|
Computed,
|
||||||
Props,
|
Props,
|
||||||
SetupBindings
|
SetupBindings,
|
||||||
|
Mixin,
|
||||||
|
Extends
|
||||||
>
|
>
|
||||||
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
|
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
|
||||||
component<PropNames extends string>(
|
component<PropNames extends string>(
|
||||||
|
Loading…
Reference in New Issue
Block a user