Skip to content

Commit 30b3eae

Browse files
committed
feat(types): new Vue() improvements (vuejs#12730)
1 parent 00458cd commit 30b3eae

File tree

4 files changed

+135
-31
lines changed

4 files changed

+135
-31
lines changed

types/options.d.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export type ThisTypedComponentOptionsWithArrayProps<
9393
Methods,
9494
Computed,
9595
PropNames extends string,
96-
SetupBindings
96+
SetupBindings,
97+
Mixin,
98+
Extends
9799
> = object &
98100
ComponentOptions<
99101
V,
@@ -103,15 +105,19 @@ export type ThisTypedComponentOptionsWithArrayProps<
103105
PropNames[],
104106
Record<PropNames, any>,
105107
SetupBindings
106-
> &
107-
ThisType<
108+
> & {
109+
mixin?: Mixin[]
110+
extends?: Extends
111+
} & ThisType<
108112
CombinedVueInstance<
109113
V,
110114
Data,
111115
Methods,
112116
Computed,
113117
Readonly<Record<PropNames, any>>,
114-
SetupBindings
118+
SetupBindings,
119+
Mixin,
120+
Extends
115121
>
116122
>
117123

@@ -124,7 +130,9 @@ export type ThisTypedComponentOptionsWithRecordProps<
124130
Methods,
125131
Computed,
126132
Props,
127-
SetupBindings
133+
SetupBindings,
134+
Mixin,
135+
Extends
128136
> = object &
129137
ComponentOptions<
130138
V,
@@ -134,15 +142,19 @@ export type ThisTypedComponentOptionsWithRecordProps<
134142
RecordPropsDefinition<Props>,
135143
Props,
136144
SetupBindings
137-
> &
138-
ThisType<
145+
> & {
146+
mixin?: Mixin[]
147+
extends?: Extends
148+
} & ThisType<
139149
CombinedVueInstance<
140150
V,
141151
Data,
142152
Methods,
143153
Computed,
144154
Readonly<Props>,
145-
SetupBindings
155+
SetupBindings,
156+
Mixin,
157+
Extends
146158
>
147159
>
148160

types/test/vue-test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,17 @@ const ComponentWithStyleInVNodeData = Vue.extend({
246246
])
247247
}
248248
})
249+
250+
// infer mixin type with new Vue() #12730
251+
new Vue({
252+
mixin: [
253+
{
254+
methods: {
255+
hello() {}
256+
}
257+
}
258+
],
259+
created() {
260+
this.hello()
261+
}
262+
})

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ type ExtractMixin<T> = {
7979
Mixin: MixinToOptionTypes<T>
8080
}[T extends ComponentOptionsMixin ? 'Mixin' : never]
8181

82-
type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
82+
export type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
8383
? OptionTypesType<{}, {}, {}, {}, {}, {}>
8484
: UnionToIntersection<ExtractMixin<T>>
8585

86-
type UnwrapMixinsType<
86+
export type UnwrapMixinsType<
8787
T,
8888
Type extends OptionTypesKeys
8989
> = T extends OptionTypesType ? T[Type] : never

types/vue.d.ts

+99-21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
1414
import { PluginFunction, PluginObject } from './plugin'
1515
import { DefineComponent } from './v3-define-component'
1616
import { nextTick } from './v3-generated'
17+
import {
18+
UnwrapMixinsType,
19+
IntersectionMixin
20+
} from './v3-component-public-instance'
1721

1822
export interface CreateElement {
1923
(
@@ -94,18 +98,28 @@ export interface Vue<
9498
$createElement: CreateElement
9599
}
96100

101+
type ComponentMixin = ComponentOptions<any, any, any, any, any>
102+
97103
export type CombinedVueInstance<
98104
Instance extends Vue,
99105
Data,
100106
Methods,
101107
Computed,
102108
Props,
103-
SetupBindings = {}
104-
> = Data &
109+
SetupBindings = {},
110+
Mixin extends ComponentMixin = ComponentMixin,
111+
Extends extends ComponentMixin = ComponentMixin,
112+
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>
113+
> = UnwrapMixinsType<PublicMixin, 'D'> &
114+
Data &
115+
UnwrapMixinsType<PublicMixin, 'M'> &
105116
Methods &
117+
UnwrapMixinsType<PublicMixin, 'C'> &
106118
Computed &
119+
UnwrapMixinsType<PublicMixin, 'P'> &
107120
Props &
108121
Instance &
122+
UnwrapMixinsType<PublicMixin, 'B'> &
109123
(SetupBindings extends void ? {} : SetupBindings)
110124

111125
export type ExtendedVue<
@@ -114,9 +128,20 @@ export type ExtendedVue<
114128
Methods,
115129
Computed,
116130
Props,
117-
SetupBindings = {}
131+
SetupBindings = {},
132+
Mixin extends ComponentMixin = ComponentMixin,
133+
Extends extends ComponentMixin = ComponentMixin
118134
> = VueConstructor<
119-
CombinedVueInstance<Instance, Data, Methods, Computed, Props, SetupBindings> &
135+
CombinedVueInstance<
136+
Instance,
137+
Data,
138+
Methods,
139+
Computed,
140+
Props,
141+
SetupBindings,
142+
Mixin,
143+
Extends
144+
> &
120145
Vue
121146
>
122147

@@ -142,23 +167,29 @@ export interface VueConstructor<V extends Vue = Vue> {
142167
Methods = object,
143168
Computed = object,
144169
PropNames extends string = never,
145-
SetupBindings = {}
170+
SetupBindings = {},
171+
Mixin extends ComponentMixin = ComponentMixin,
172+
Extends extends ComponentMixin = ComponentMixin
146173
>(
147174
options?: ThisTypedComponentOptionsWithArrayProps<
148175
V,
149176
Data,
150177
Methods,
151178
Computed,
152179
PropNames,
153-
SetupBindings
180+
SetupBindings,
181+
Mixin,
182+
Extends
154183
>
155184
): CombinedVueInstance<
156185
V,
157186
Data,
158187
Methods,
159188
Computed,
160189
Record<PropNames, any>,
161-
SetupBindings
190+
SetupBindings,
191+
Mixin,
192+
Extends
162193
>
163194

164195
/**
@@ -172,23 +203,29 @@ export interface VueConstructor<V extends Vue = Vue> {
172203
Methods = object,
173204
Computed = object,
174205
Props = object,
175-
SetupBindings = {}
206+
SetupBindings = {},
207+
Mixin extends ComponentMixin = ComponentMixin,
208+
Extends extends ComponentMixin = ComponentMixin
176209
>(
177210
options?: ThisTypedComponentOptionsWithRecordProps<
178211
V,
179212
Data,
180213
Methods,
181214
Computed,
182215
Props,
183-
SetupBindings
216+
SetupBindings,
217+
Mixin,
218+
Extends
184219
>
185220
): CombinedVueInstance<
186221
V,
187222
Data,
188223
Methods,
189224
Computed,
190225
Record<keyof Props, any>,
191-
SetupBindings
226+
SetupBindings,
227+
Mixin,
228+
Extends
192229
>
193230

194231
/**
@@ -211,38 +248,63 @@ export interface VueConstructor<V extends Vue = Vue> {
211248
Methods,
212249
Computed,
213250
PropNames extends string = never,
214-
SetupBindings = {}
251+
SetupBindings = {},
252+
Mixin extends ComponentMixin = ComponentMixin,
253+
Extends extends ComponentMixin = ComponentMixin
215254
>(
216255
options?: ThisTypedComponentOptionsWithArrayProps<
217256
V,
218257
Data,
219258
Methods,
220259
Computed,
221260
PropNames,
222-
SetupBindings
261+
SetupBindings,
262+
Mixin,
263+
Extends
223264
>
224265
): ExtendedVue<
225266
V,
226267
Data,
227268
Methods,
228269
Computed,
229270
Record<PropNames, any>,
230-
SetupBindings
271+
SetupBindings,
272+
Mixin,
273+
Extends
231274
>
232275

233276
/**
234277
* extend with object props
235278
*/
236-
extend<Data, Methods, Computed, Props, SetupBindings = {}>(
279+
extend<
280+
Data,
281+
Methods,
282+
Computed,
283+
Props,
284+
SetupBindings = {},
285+
Mixin extends ComponentMixin = ComponentMixin,
286+
Extends extends ComponentMixin = ComponentMixin
287+
>(
237288
options?: ThisTypedComponentOptionsWithRecordProps<
238289
V,
239290
Data,
240291
Methods,
241292
Computed,
242293
Props,
243-
SetupBindings
294+
SetupBindings,
295+
Mixin,
296+
Extends
244297
>
245-
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
298+
): ExtendedVue<
299+
V,
300+
Data,
301+
Methods,
302+
Computed,
303+
Props,
304+
SetupBindings,
305+
Mixin,
306+
Extends
307+
>
246308

247309
/**
248310
* extend with functional + array props
@@ -287,7 +349,9 @@ export interface VueConstructor<V extends Vue = Vue> {
287349
Methods,
288350
Computed,
289351
PropNames extends string = never,
290-
SetupBindings = {}
352+
SetupBindings = {},
353+
Mixin extends ComponentMixin = ComponentMixin,
354+
Extends extends ComponentMixin = ComponentMixin
291355
>(
292356
id: string,
293357
definition?: ThisTypedComponentOptionsWithArrayProps<
@@ -296,25 +360,39 @@ export interface VueConstructor<V extends Vue = Vue> {
296360
Methods,
297361
Computed,
298362
PropNames,
299-
SetupBindings
363+
SetupBindings,
364+
Mixin,
365+
Extends
300366
>
301367
): ExtendedVue<
302368
V,
303369
Data,
304370
Methods,
305371
Computed,
306372
Record<PropNames, any>,
307-
SetupBindings
373+
SetupBindings,
374+
Mixin,
375+
Extends
308376
>
309-
component<Data, Methods, Computed, Props, SetupBindings>(
377+
component<
378+
Data,
379+
Methods,
380+
Computed,
381+
Props,
382+
SetupBindings,
383+
Mixin extends ComponentMixin = ComponentMixin,
384+
Extends extends ComponentMixin = ComponentMixin
385+
>(
310386
id: string,
311387
definition?: ThisTypedComponentOptionsWithRecordProps<
312388
V,
313389
Data,
314390
Methods,
315391
Computed,
316392
Props,
317-
SetupBindings
393+
SetupBindings,
394+
Mixin,
395+
Extends
318396
>
319397
): ExtendedVue<V, Data, Methods, Computed, Props, SetupBindings>
320398
component<PropNames extends string>(

0 commit comments

Comments
 (0)