Skip to content

Commit db9d685

Browse files
author
gulewei11022
committed
feat: support mixins with defineComponent
1 parent e737a44 commit db9d685

File tree

4 files changed

+53
-70
lines changed

4 files changed

+53
-70
lines changed

types/options.d.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
33
import { SetupContext } from './v3-setup-context'
44
import { DebuggerEvent } from './v3-generated'
55
import { DefineComponent } from './v3-define-component'
6+
import { ComponentOptionsMixin } from './v3-component-options'
67

78
type Constructor = {
89
new (...args: any[]): any
@@ -104,11 +105,11 @@ export type ThisTypedComponentOptionsWithArrayProps<
104105
Computed,
105106
PropNames[],
106107
Record<PropNames, any>,
107-
SetupBindings
108-
> & {
109-
mixins?: (Mixin | typeof Vue)[]
110-
extends?: Extends | typeof Vue
111-
} & ThisType<
108+
SetupBindings,
109+
Mixin,
110+
Extends
111+
> &
112+
ThisType<
112113
CombinedVueInstance<
113114
V,
114115
Data,
@@ -141,11 +142,11 @@ export type ThisTypedComponentOptionsWithRecordProps<
141142
Computed,
142143
RecordPropsDefinition<Props>,
143144
Props,
144-
SetupBindings
145-
> & {
146-
mixins?: (Mixin | typeof Vue)[]
147-
extends?: Extends | typeof Vue
148-
} & ThisType<
145+
SetupBindings,
146+
Mixin,
147+
Extends
148+
> &
149+
ThisType<
149150
CombinedVueInstance<
150151
V,
151152
Data,
@@ -170,7 +171,9 @@ export interface ComponentOptions<
170171
Computed = DefaultComputed,
171172
PropsDef = PropsDefinition<DefaultProps>,
172173
Props = DefaultProps,
173-
RawBindings = {}
174+
RawBindings = {},
175+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
176+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
174177
> {
175178
data?: Data
176179
props?: PropsDef
@@ -229,12 +232,12 @@ export interface ComponentOptions<
229232
}
230233

231234
parent?: Vue
232-
mixins?: (ComponentOptions<Vue> | typeof Vue)[]
235+
mixins?: (Mixin | ComponentOptions<Vue> | typeof Vue)[]
233236
name?: string
234237
// for SFC auto name inference w/ ts-loader check
235238
__name?: string
236239
// TODO: support properly inferred 'extends'
237-
extends?: ComponentOptions<Vue> | typeof Vue
240+
extends?: Extends | ComponentOptions<Vue> | typeof Vue
238241
delimiters?: [string, string]
239242
comments?: boolean
240243
inheritAttrs?: boolean

types/test/vue-test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Vue, { VNode } from '../index'
1+
import Vue, { VNode, defineComponent } from '../index'
22
import { ComponentOptions } from '../options'
33

44
class Test extends Vue {
@@ -250,7 +250,14 @@ const ComponentWithStyleInVNodeData = Vue.extend({
250250
// infer mixin type with new Vue() #12730
251251
new Vue({
252252
mixins: [
253-
{
253+
defineComponent({
254+
props: {
255+
p1: String,
256+
p2: {
257+
type: Number,
258+
default: 0
259+
}
260+
},
254261
data() {
255262
return {
256263
foo: 123
@@ -261,7 +268,7 @@ new Vue({
261268
return 123
262269
}
263270
}
264-
},
271+
}),
265272
{
266273
methods: {
267274
hello(n: number) {}
@@ -271,5 +278,8 @@ new Vue({
271278
created() {
272279
this.hello(this.foo)
273280
this.hello(this.bar)
281+
// @ts-expect-error
282+
this.hello(this.p1)
283+
this.hello(this.p2)
274284
}
275285
})

types/v3-component-public-instance.d.ts

-29
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ type IsDefaultMixinComponent<T> = T extends ComponentOptionsMixin
5757
: false
5858
: false
5959

60-
type inferData<T> = T extends { data?: infer D }
61-
? D extends (this: Vue) => infer R
62-
? IfAny<R, {}, R>
63-
: IfAny<D, {}, D>
64-
: {}
65-
6660
type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
6761
infer P,
6862
infer B,
@@ -78,29 +72,6 @@ type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
7872
? OptionTypesType<P & {}, B & {}, D & {}, C & {}, M & {}, Defaults & {}> &
7973
IntersectionMixin<Mixin> &
8074
IntersectionMixin<Extends>
81-
: // V3 data not assignable to V2 data
82-
Omit<T, 'data'> extends ComponentOptionsBase<
83-
infer P,
84-
infer B,
85-
any,
86-
infer C,
87-
infer M,
88-
infer Mixin,
89-
infer Extends,
90-
any,
91-
any,
92-
infer Defaults
93-
>
94-
? OptionTypesType<
95-
P & {},
96-
B & {},
97-
inferData<T> & {},
98-
C & {},
99-
M & {},
100-
Defaults & {}
101-
> &
102-
IntersectionMixin<Mixin> &
103-
IntersectionMixin<Extends>
10475
: never
10576

10677
// ExtractMixin(map type) is used to resolve circularly references

types/vue.d.ts

+24-25
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import {
1313
import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
1414
import { PluginFunction, PluginObject } from './plugin'
1515
import { DefineComponent } from './v3-define-component'
16-
import { nextTick } from './v3-generated'
16+
import { nextTick, UnwrapNestedRefs, ShallowUnwrapRef } from './v3-generated'
1717
import {
1818
UnwrapMixinsType,
1919
IntersectionMixin
2020
} from './v3-component-public-instance'
21-
import { ExtractComputedReturns } from './v3-component-options'
21+
import {
22+
ExtractComputedReturns,
23+
ComponentOptionsMixin
24+
} from './v3-component-options'
2225

2326
export interface CreateElement {
2427
(
@@ -99,21 +102,17 @@ export interface Vue<
99102
$createElement: CreateElement
100103
}
101104

102-
type ComponentMixin = ComponentOptions<any, any, any, any, any>
103-
104105
export type CombinedVueInstance<
105106
Instance extends Vue,
106107
Data,
107108
Methods,
108109
Computed,
109110
Props,
110111
SetupBindings = {},
111-
Mixin extends ComponentMixin = ComponentMixin,
112-
Extends extends ComponentMixin = ComponentMixin,
113-
PublicMixin = IntersectionMixin<Mixin> &
114-
IntersectionMixin<Extends> &
115-
IntersectionMixin<ComponentOptions<any, any, any, any, any>> // prevent produce `never` in UnwrapMixinsType
116-
> = UnwrapMixinsType<PublicMixin, 'D'> &
112+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
113+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
114+
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>
115+
> = UnwrapNestedRefs<UnwrapMixinsType<PublicMixin, 'D'>> &
117116
Data &
118117
UnwrapMixinsType<PublicMixin, 'M'> &
119118
Methods &
@@ -122,7 +121,7 @@ export type CombinedVueInstance<
122121
UnwrapMixinsType<PublicMixin, 'P'> &
123122
Props &
124123
Instance &
125-
UnwrapMixinsType<PublicMixin, 'B'> &
124+
ShallowUnwrapRef<UnwrapMixinsType<PublicMixin, 'B'>> &
126125
(SetupBindings extends void ? {} : SetupBindings)
127126

128127
export type ExtendedVue<
@@ -132,8 +131,8 @@ export type ExtendedVue<
132131
Computed,
133132
Props,
134133
SetupBindings = {},
135-
Mixin extends ComponentMixin = ComponentMixin,
136-
Extends extends ComponentMixin = ComponentMixin
134+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
135+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
137136
> = VueConstructor<
138137
CombinedVueInstance<
139138
Instance,
@@ -171,8 +170,8 @@ export interface VueConstructor<V extends Vue = Vue> {
171170
Computed = object,
172171
PropNames extends string = never,
173172
SetupBindings = {},
174-
Mixin extends ComponentMixin = ComponentMixin,
175-
Extends extends ComponentMixin = ComponentMixin
173+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
174+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
176175
>(
177176
options?: ThisTypedComponentOptionsWithArrayProps<
178177
V,
@@ -207,8 +206,8 @@ export interface VueConstructor<V extends Vue = Vue> {
207206
Computed = object,
208207
Props = object,
209208
SetupBindings = {},
210-
Mixin extends ComponentMixin = ComponentMixin,
211-
Extends extends ComponentMixin = ComponentMixin
209+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
210+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
212211
>(
213212
options?: ThisTypedComponentOptionsWithRecordProps<
214213
V,
@@ -252,8 +251,8 @@ export interface VueConstructor<V extends Vue = Vue> {
252251
Computed,
253252
PropNames extends string = never,
254253
SetupBindings = {},
255-
Mixin extends ComponentMixin = ComponentMixin,
256-
Extends extends ComponentMixin = ComponentMixin
254+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
255+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
257256
>(
258257
options?: ThisTypedComponentOptionsWithArrayProps<
259258
V,
@@ -285,8 +284,8 @@ export interface VueConstructor<V extends Vue = Vue> {
285284
Computed,
286285
Props,
287286
SetupBindings = {},
288-
Mixin extends ComponentMixin = ComponentMixin,
289-
Extends extends ComponentMixin = ComponentMixin
287+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
288+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
290289
>(
291290
options?: ThisTypedComponentOptionsWithRecordProps<
292291
V,
@@ -353,8 +352,8 @@ export interface VueConstructor<V extends Vue = Vue> {
353352
Computed,
354353
PropNames extends string = never,
355354
SetupBindings = {},
356-
Mixin extends ComponentMixin = ComponentMixin,
357-
Extends extends ComponentMixin = ComponentMixin
355+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
356+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
358357
>(
359358
id: string,
360359
definition?: ThisTypedComponentOptionsWithArrayProps<
@@ -383,8 +382,8 @@ export interface VueConstructor<V extends Vue = Vue> {
383382
Computed,
384383
Props,
385384
SetupBindings,
386-
Mixin extends ComponentMixin = ComponentMixin,
387-
Extends extends ComponentMixin = ComponentMixin
385+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
386+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin
388387
>(
389388
id: string,
390389
definition?: ThisTypedComponentOptionsWithRecordProps<

0 commit comments

Comments
 (0)