Skip to content

Commit

Permalink
fix: improve vm types
Browse files Browse the repository at this point in the history
This improves the signature of `mount` in order to have a proper type checking of `vm`.
  • Loading branch information
cexbrayat committed Apr 17, 2020
1 parent 26a8fee commit be92e32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ interface MountingOptions {
stubs?: Record<string, any>
}

export function mount<T extends any>(
originalComponent: any,
export function mount<TestedComponent extends ComponentPublicInstance>(
originalComponent: new () => TestedComponent,
options?: MountingOptions
): VueWrapper<TestedComponent>
export function mount(
originalComponent: Component,
options?: MountingOptions
): VueWrapper<any>
export function mount<T extends ComponentPublicInstance>(
originalComponent: new () => T,
export function mount(
originalComponent: any,
options?: MountingOptions
): VueWrapper<T> {
): VueWrapper<any> {
const component = { ...originalComponent }

// Reset the document.body
Expand Down Expand Up @@ -158,5 +162,5 @@ export function mount<T extends ComponentPublicInstance>(
// mount the app!
const app = vm.mount(el)

return createWrapper<T>(app, events, setProps)
return createWrapper(app, events, setProps)
}
14 changes: 12 additions & 2 deletions test-dts/index.d-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ const App = defineComponent({
template: ''
})

const wrapper = mount(App)
expectType<any>(wrapper.vm.a) // should be string
let wrapper = mount(App)
expectType<string>(wrapper.vm.a)

const AppWithoutDefine = {
props: {
a: String
},
template: ''
}

wrapper = mount(AppWithoutDefine)
expectType<string>(wrapper.vm.a)

0 comments on commit be92e32

Please sign in to comment.