Skip to content

Commit a84af8f

Browse files
committed
fix: infer V2 data from mixins
1 parent 4b95a29 commit a84af8f

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

Diff for: types/test/vue-test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ new Vue({
255255
return {
256256
foo: 123
257257
}
258+
},
259+
computed: {
260+
bar() {
261+
return 123
262+
}
258263
}
259264
},
260265
{
@@ -265,5 +270,6 @@ new Vue({
265270
],
266271
created() {
267272
this.hello(this.foo)
273+
this.hello(this.bar)
268274
}
269275
})

Diff for: types/v3-component-public-instance.d.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
ShallowUnwrapRef,
44
UnwrapNestedRefs
55
} from './v3-generated'
6-
import { UnionToIntersection } from './common'
6+
import { IfAny, UnionToIntersection } from './common'
77

88
import { Vue, VueConstructor } from './vue'
99
import {
@@ -57,6 +57,12 @@ 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+
6066
type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
6167
infer P,
6268
infer B,
@@ -72,6 +78,29 @@ type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
7278
? OptionTypesType<P & {}, B & {}, D & {}, C & {}, M & {}, Defaults & {}> &
7379
IntersectionMixin<Mixin> &
7480
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>
75104
: never
76105

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

Diff for: types/vue.d.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
UnwrapMixinsType,
1919
IntersectionMixin
2020
} from './v3-component-public-instance'
21+
import { ExtractComputedReturns } from './v3-component-options'
2122

2223
export interface CreateElement {
2324
(
@@ -100,14 +101,6 @@ export interface Vue<
100101

101102
type ComponentMixin = ComponentOptions<any, any, any, any, any>
102103

103-
// IntersectionMixin cannot infer `data` from ComponentOptions<...>
104-
type PrepareMixinData<M extends ComponentMixin, D = Required<M>['data']> = {
105-
[K in keyof M]: K extends 'data'
106-
? D extends (...args: any) => infer R
107-
? (this: any) => R
108-
: (this: any) => D
109-
: M[K]
110-
}
111104
export type CombinedVueInstance<
112105
Instance extends Vue,
113106
Data,
@@ -117,14 +110,14 @@ export type CombinedVueInstance<
117110
SetupBindings = {},
118111
Mixin extends ComponentMixin = ComponentMixin,
119112
Extends extends ComponentMixin = ComponentMixin,
120-
PublicMixin = IntersectionMixin<PrepareMixinData<Mixin>> &
113+
PublicMixin = IntersectionMixin<Mixin> &
121114
IntersectionMixin<Extends> &
122115
IntersectionMixin<ComponentOptions<any, any, any, any, any>> // prevent produce `never` in UnwrapMixinsType
123116
> = UnwrapMixinsType<PublicMixin, 'D'> &
124117
Data &
125118
UnwrapMixinsType<PublicMixin, 'M'> &
126119
Methods &
127-
UnwrapMixinsType<PublicMixin, 'C'> &
120+
ExtractComputedReturns<UnwrapMixinsType<PublicMixin, 'C'>> &
128121
Computed &
129122
UnwrapMixinsType<PublicMixin, 'P'> &
130123
Props &

0 commit comments

Comments
 (0)