Skip to content

Commit

Permalink
feat(types): support mixins inference for new Vue() (#12737)
Browse files Browse the repository at this point in the history
close #12730
  • Loading branch information
gulewei committed Aug 18, 2022
1 parent b4bf4c5 commit 89a6b5e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 34 deletions.
33 changes: 24 additions & 9 deletions types/options.d.ts
Expand Up @@ -3,6 +3,7 @@ import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
import { SetupContext } from './v3-setup-context'
import { DebuggerEvent } from './v3-generated'
import { DefineComponent } from './v3-define-component'
import { ComponentOptionsMixin } from './v3-component-options'

type Constructor = {
new (...args: any[]): any
Expand Down Expand Up @@ -93,7 +94,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Methods,
Computed,
PropNames extends string,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -102,7 +105,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Computed,
PropNames[],
Record<PropNames, any>,
SetupBindings
SetupBindings,
Mixin,
Extends
> &
ThisType<
CombinedVueInstance<
Expand All @@ -111,7 +116,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
Methods,
Computed,
Readonly<Record<PropNames, any>>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand All @@ -124,7 +131,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Methods,
Computed,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
> = object &
ComponentOptions<
V,
Expand All @@ -133,7 +142,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Computed,
RecordPropsDefinition<Props>,
Props,
SetupBindings
SetupBindings,
Mixin,
Extends
> &
ThisType<
CombinedVueInstance<
Expand All @@ -142,7 +153,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
Methods,
Computed,
Readonly<Props>,
SetupBindings
SetupBindings,
Mixin,
Extends
>
>

Expand All @@ -158,7 +171,9 @@ export interface ComponentOptions<
Computed = DefaultComputed,
PropsDef = PropsDefinition<DefaultProps>,
Props = DefaultProps,
RawBindings = {}
RawBindings = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
> {
data?: Data
props?: PropsDef
Expand Down Expand Up @@ -217,12 +232,12 @@ export interface ComponentOptions<
}

parent?: Vue
mixins?: (ComponentOptions<Vue> | typeof Vue)[]
mixins?: (Mixin | ComponentOptions<Vue> | typeof Vue)[]
name?: string
// for SFC auto name inference w/ ts-loader check
__name?: string
// TODO: support properly inferred 'extends'
extends?: ComponentOptions<Vue> | typeof Vue
extends?: Extends | ComponentOptions<Vue> | typeof Vue
delimiters?: [string, string]
comments?: boolean
inheritAttrs?: boolean
Expand Down
39 changes: 38 additions & 1 deletion types/test/vue-test.ts
@@ -1,4 +1,4 @@
import Vue, { VNode } from '../index'
import Vue, { VNode, defineComponent } from '../index'
import { ComponentOptions } from '../options'

class Test extends Vue {
Expand Down Expand Up @@ -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 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

0 comments on commit 89a6b5e

Please sign in to comment.