Skip to content

Commit 8a7f621

Browse files
authored
fix(types): mount() typing compatibility for vue 3.3.8 (#2240)
1 parent dbfbef5 commit 8a7f621

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/mount.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type WithArray<T> = T | T[]
1818

1919
type ComponentData<T> = T extends { data?(...args: any): infer D } ? D : {}
2020

21-
export type ComponentMountingOptions<T> = Omit<
22-
MountingOptions<ComponentProps<T>, ComponentData<T>>,
23-
'slots'
24-
> & {
21+
export type ComponentMountingOptions<
22+
T,
23+
P extends ComponentProps<T> = ComponentProps<T>
24+
> = Omit<MountingOptions<P, ComponentData<T>>, 'slots'> & {
2525
slots?: {
2626
[K in keyof ComponentSlots<T>]: WithArray<
2727
| ShimSlotReturnType<ComponentSlots<T>[K]>
@@ -43,17 +43,20 @@ export function mount<
4343
? { [key in PropNames extends string ? PropNames : string]?: any }
4444
: Props
4545
>
46-
: DefineComponent
46+
: DefineComponent,
47+
P extends ComponentProps<C> = ComponentProps<C>
4748
>(
4849
originalComponent: T,
49-
options?: ComponentMountingOptions<C>
50+
options?: ComponentMountingOptions<C, P>
5051
): VueWrapper<
5152
ComponentProps<C> & ComponentData<C> & ComponentExposed<C>,
5253
ComponentPublicInstance<
5354
ComponentProps<C>,
54-
ComponentData<C> & ComponentExposed<C>
55+
ComponentData<C> & ComponentExposed<C> & Omit<P, keyof ComponentProps<C>>
5556
>
56-
>
57+
> & {
58+
LOOL: Exclude<P, ComponentProps<C>>
59+
}
5760

5861
// implementation
5962
export function mount(

test-dts/mount.d-test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const AppWithProps = {
6666
props: {
6767
a: {
6868
type: String,
69-
required: true
69+
required: true as true
7070
}
7171
},
7272
template: ''
@@ -91,7 +91,7 @@ expectError(
9191
)
9292

9393
const AppWithArrayProps = {
94-
props: ['a'],
94+
props: ['a'] as ['a'],
9595
template: ''
9696
}
9797

@@ -134,16 +134,16 @@ mount(AppWithoutProps, {
134134
// Functional tests
135135

136136
expectError(
137-
mount((props: { a: 1 }) => { }, {
137+
mount((props: { a: 1 }) => {}, {
138138
props: {
139139
// @ts-expect-error wrong props
140-
a: '222'
140+
a: '222'
141141
}
142142
})
143143
)
144144

145145
expectType<number>(
146-
mount((props: { a: number }, ctx: any) => { }, {
146+
mount((props: { a: number }, ctx: any) => {}, {
147147
props: {
148148
a: 22
149149
}
@@ -247,7 +247,7 @@ class CustomClassComponent<Props extends {} = {}> {
247247
return this.props
248248
}
249249
context: SetupContext
250-
render(): VNodeChild { }
250+
render(): VNodeChild {}
251251
}
252252
class NoPropCustomClassComponent extends CustomClassComponent {
253253
count = ref(0)
@@ -281,15 +281,17 @@ class WithPropCustomClassComponent extends CustomClassComponent<CustomClassCompo
281281

282282
expectError(
283283
mount(
284-
WithPropCustomClassComponent as typeof WithPropCustomClassComponent & (new () => { $props: CustomClassComponentProps }),
284+
WithPropCustomClassComponent as typeof WithPropCustomClassComponent &
285+
(new () => { $props: CustomClassComponentProps }),
285286
{
286287
// @ts-expect-error should has props error
287288
props: {}
288289
}
289290
)
290291
)
291292
mount(
292-
WithPropCustomClassComponent as typeof WithPropCustomClassComponent & (new () => { $props: CustomClassComponentProps }),
293+
WithPropCustomClassComponent as typeof WithPropCustomClassComponent &
294+
(new () => { $props: CustomClassComponentProps }),
293295
{
294296
props: { size: 'small' }
295297
}

test-dts/shallowMount.d-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const AppWithProps = {
3838
props: {
3939
a: {
4040
type: String,
41-
required: true
41+
required: true as true
4242
}
4343
},
4444
template: ''
@@ -65,7 +65,7 @@ expectError(
6565
)
6666

6767
const AppWithArrayProps = {
68-
props: ['a'],
68+
props: ['a'] as ['a'],
6969
template: ''
7070
}
7171

0 commit comments

Comments
 (0)