Skip to content

Commit 37fe25e

Browse files
committed
fix(types): Fix mount and shallowMount types
1 parent 42762b9 commit 37fe25e

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/mount.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { attachEmitListener } from './emitMixin'
3838
import { createDataMixin } from './dataMixin'
3939
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
4040
import { stubComponents } from './stubs'
41-
import { VueConstructor } from 'vue-class-component'
4241

4342
// NOTE this should come from `vue`
4443
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
@@ -74,10 +73,10 @@ export type ObjectEmitsOptions = Record<
7473
export type EmitsOptions = ObjectEmitsOptions | string[]
7574

7675
// Class component
77-
export function mount(
78-
originalComponent: VueConstructor,
76+
export function mount<V>(
77+
originalComponent: new (...args: any[]) => V,
7978
options?: MountingOptions<any>
80-
): VueWrapper<ComponentPublicInstance<any>>
79+
): VueWrapper<ComponentPublicInstance<V>>
8180

8281
// Functional component with emits
8382
export function mount<Props, E extends EmitsOptions = {}>(

test-dts/mount.d-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FunctionalComponent,
66
reactive
77
} from 'vue'
8+
import { Options, Vue } from 'vue-class-component'
89
import { mount } from '../src'
910

1011
const AppWithDefine = defineComponent({
@@ -203,3 +204,25 @@ mount(FunctionalComponentEmit)
203204

204205
// @ts-ignore vue 3.0.2 doesn't work. FIX: https://github.com/vuejs/vue-next/pull/2494
205206
mount(defineComponent(FunctionalComponentEmit))
207+
208+
// class component
209+
210+
@Options({
211+
props: {
212+
msg: String
213+
}
214+
})
215+
class ClassComponent extends Vue {
216+
dataText: string = ''
217+
get computedMsg(): string {
218+
return `Message: ${(this.$props as any).msg}`
219+
}
220+
221+
changeMessage(text: string): void {
222+
this.dataText = 'Updated'
223+
}
224+
}
225+
226+
// @ts-expect-error it requires an argument
227+
expectError(mount(ClassComponent, {}).vm.changeMessage())
228+
mount(ClassComponent, {}).vm.changeMessage('')

test-dts/shallowMount.d-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expectError, expectType } from 'tsd'
22
import { defineComponent } from 'vue'
3+
import { Options, Vue } from 'vue-class-component'
34
import { shallowMount } from '../src'
45

56
const AppWithDefine = defineComponent({
@@ -99,3 +100,24 @@ expectError(
99100
shallowMount(AppWithoutProps, {
100101
props: { b: 'Hello' } as never
101102
})
103+
104+
// class component
105+
@Options({
106+
props: {
107+
msg: String
108+
}
109+
})
110+
class ClassComponent extends Vue {
111+
dataText: string = ''
112+
get computedMsg(): string {
113+
return `Message: ${(this.$props as any).msg}`
114+
}
115+
116+
changeMessage(text: string): void {
117+
this.dataText = 'Updated'
118+
}
119+
}
120+
121+
// @ts-expect-error it requires an argument
122+
expectError(mount(ClassComponent, {}).vm.changeMessage())
123+
shallowMount(ClassComponent, {}).vm.changeMessage('')

0 commit comments

Comments
 (0)