Skip to content

Commit 02cd7c9

Browse files
authored
Merge pull request #224 from pikax/fix/fix_issue_with_shim
fix: support for DefineComponent shim
2 parents dd780f8 + cc1c943 commit 02cd7c9

File tree

3 files changed

+88
-23
lines changed

3 files changed

+88
-23
lines changed

src/mount.ts

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
ComponentPropsOptions,
1818
AppConfig,
1919
VNodeProps,
20-
ComponentOptionsMixin
20+
ComponentOptionsMixin,
21+
DefineComponent,
22+
MethodOptions,
23+
AllowedComponentProps,
24+
ComponentCustomProps,
25+
ExtractDefaultPropTypes
2126
} from 'vue'
2227

2328
import { config } from './config'
@@ -34,18 +39,17 @@ import {
3439
} from './constants'
3540
import { stubComponents } from './stubs'
3641

42+
// NOTE this should come from `vue`
43+
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
44+
3745
type Slot = VNode | string | { render: Function } | Function | Component
3846

3947
type SlotDictionary = {
4048
[key: string]: Slot
4149
}
4250

4351
interface MountingOptions<Props, Data = {}> {
44-
data?: () => {} extends Data
45-
? never
46-
: Data extends object
47-
? Partial<Data>
48-
: never
52+
data?: () => {} extends Data ? any : Data extends object ? Partial<Data> : any
4953
props?: Props
5054
/** @deprecated */
5155
propsData?: Props
@@ -78,10 +82,53 @@ export function mount<
7882
): VueWrapper<ComponentPublicInstance<Props>>
7983

8084
// Component declared with defineComponent
81-
export function mount<TestedComponent extends ComponentPublicInstance>(
82-
originalComponent: { new (): TestedComponent } & Component,
83-
options?: MountingOptions<TestedComponent['$props'], TestedComponent['$data']>
84-
): VueWrapper<TestedComponent>
85+
export function mount<
86+
PropsOrPropOptions = {},
87+
RawBindings = {},
88+
D = {},
89+
C extends ComputedOptions = ComputedOptions,
90+
M extends MethodOptions = MethodOptions,
91+
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
92+
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
93+
E extends EmitsOptions = Record<string, any>,
94+
EE extends string = string,
95+
PP = PublicProps,
96+
Props = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
97+
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
98+
>(
99+
component: DefineComponent<
100+
PropsOrPropOptions,
101+
RawBindings,
102+
D,
103+
C,
104+
M,
105+
Mixin,
106+
Extends,
107+
E,
108+
EE,
109+
PP,
110+
Props,
111+
Defaults
112+
>,
113+
options?: MountingOptions<Props, D>
114+
): VueWrapper<
115+
InstanceType<
116+
DefineComponent<
117+
PropsOrPropOptions,
118+
RawBindings,
119+
D,
120+
C,
121+
M,
122+
Mixin,
123+
Extends,
124+
E,
125+
EE,
126+
PP,
127+
Props,
128+
Defaults
129+
>
130+
>
131+
>
85132

86133
// Component declared with no props
87134
export function mount<

src/vueShims.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module '*.vue' {
22
// TODO: Figure out the typing for this
3-
import Vue from 'vue'
4-
export default any
3+
import type { DefineComponent } from 'vue'
4+
const component: DefineComponent
5+
export default component
56
}

test-dts/mount.d-test.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expectError, expectType } from 'tsd'
2-
import { defineComponent } from 'vue'
2+
import { DefineComponent, defineComponent, reactive } from 'vue'
33
import { mount } from '../src'
44

55
const AppWithDefine = defineComponent({
@@ -27,16 +27,16 @@ expectType<string>(
2727
}).vm.a
2828
)
2929

30-
// no data provided
31-
expectError(
32-
mount(AppWithDefine, {
33-
data() {
34-
return {
35-
myVal: 1
36-
}
37-
}
38-
})
39-
)
30+
// // no data provided
31+
// expectError(
32+
// mount(AppWithDefine, {
33+
// data() {
34+
// return {
35+
// myVal: 1
36+
// }
37+
// }
38+
// })
39+
// )
4040

4141
// can not receive extra props
4242
expectError(
@@ -160,3 +160,20 @@ mount(AppWithProps, {
160160
}
161161
}
162162
})
163+
164+
declare const ShimComponent: DefineComponent
165+
166+
mount(ShimComponent, {
167+
props: {
168+
msg: 1
169+
}
170+
})
171+
172+
// TODO it should work
173+
mount(ShimComponent, {
174+
data() {
175+
return {
176+
a: 1
177+
}
178+
}
179+
})

0 commit comments

Comments
 (0)