Skip to content

fix(types): Fix mount and shallowMount types #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { config } from './config'
import { GlobalMountOptions } from './types'
import {
isClassComponent,
isFunctionalComponent,
isObjectComponent,
mergeGlobalProperties
Expand All @@ -38,7 +37,6 @@ import { attachEmitListener } from './emitMixin'
import { createDataMixin } from './dataMixin'
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
import { stubComponents } from './stubs'
import { VueConstructor } from 'vue-class-component'

// NOTE this should come from `vue`
type PublicProps = VNodeProps & AllowedComponentProps & ComponentCustomProps
Expand Down Expand Up @@ -73,11 +71,24 @@ export type ObjectEmitsOptions = Record<
>
export type EmitsOptions = ObjectEmitsOptions | string[]

// Class component
export function mount(
originalComponent: VueConstructor,
// Class component - no props
export function mount<V>(
originalComponent: {
new (...args: any[]): V
registerHooks(keys: string[]): void
},
options?: MountingOptions<any>
): VueWrapper<ComponentPublicInstance<any>>
): VueWrapper<ComponentPublicInstance<V>>

// Class component - props
export function mount<V, P>(
originalComponent: {
new (...args: any[]): V
props(Props: P): any
registerHooks(keys: string[]): void
},
options?: MountingOptions<P>
): VueWrapper<ComponentPublicInstance<V>>
Comment on lines +84 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function mount<V, P>(
originalComponent: {
new (...args: any[]): V
props(Props: P): any
registerHooks(keys: string[]): void
},
options?: MountingOptions<P>
): VueWrapper<ComponentPublicInstance<V>>
export function mount<V extends ComponentPublicInstance<P>, P>(
originalComponent: {
new (...args: any[]): V
},
options?: MountingOptions<P>
): VueWrapper<V>

As class component implements component public instance, just using the interface should be enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bit too lenient, because the defineComponent will also return a valid component public instance to be used on JSX

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, agree, having props intellisense is pretty cool. I don't think it's possible for data in class component though.


// Functional component with emits
export function mount<Props, E extends EmitsOptions = {}>(
Expand Down
23 changes: 23 additions & 0 deletions test-dts/mount.d-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FunctionalComponent,
reactive
} from 'vue'
import { Options, Vue } from 'vue-class-component'
import { mount } from '../src'

const AppWithDefine = defineComponent({
Expand Down Expand Up @@ -203,3 +204,25 @@ mount(FunctionalComponentEmit)

// @ts-ignore vue 3.0.2 doesn't work. FIX: https://github.com/vuejs/vue-next/pull/2494
mount(defineComponent(FunctionalComponentEmit))

// class component

@Options({
props: {
msg: String
}
})
class ClassComponent extends Vue {
dataText: string = ''
get computedMsg(): string {
return `Message: ${(this.$props as any).msg}`
}

changeMessage(text: string): void {
this.dataText = 'Updated'
}
}

// @ts-expect-error it requires an argument
expectError(mount(ClassComponent, {}).vm.changeMessage())
mount(ClassComponent, {}).vm.changeMessage('')
22 changes: 22 additions & 0 deletions test-dts/shallowMount.d-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expectError, expectType } from 'tsd'
import { defineComponent } from 'vue'
import { Options, Vue } from 'vue-class-component'
import { shallowMount } from '../src'

const AppWithDefine = defineComponent({
Expand Down Expand Up @@ -99,3 +100,24 @@ expectError(
shallowMount(AppWithoutProps, {
props: { b: 'Hello' } as never
})

// class component
@Options({
props: {
msg: String
}
})
class ClassComponent extends Vue {
dataText: string = ''
get computedMsg(): string {
return `Message: ${(this.$props as any).msg}`
}

changeMessage(text: string): void {
this.dataText = 'Updated'
}
}

// @ts-expect-error it requires an argument
expectError(shallowMount(ClassComponent, {}).vm.changeMessage())
shallowMount(ClassComponent, {}).vm.changeMessage('')