Skip to content

Commit ae79211

Browse files
committed
chore: improve types and add type test
1 parent 6079cfe commit ae79211

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/mount.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,18 @@ export type ObjectEmitsOptions = Record<
6868
export type EmitsOptions = ObjectEmitsOptions | string[]
6969

7070
// Functional component
71-
export function mount<
72-
TestedComponent extends FunctionalComponent<Props>,
73-
Props
74-
>(
75-
originalComponent: TestedComponent,
71+
// export function mount<
72+
// TestedComponent extends FunctionalComponent<Props, E>,
73+
// Props,
74+
// E extends EmitsOptions = {}
75+
// >(
76+
// originalComponent: TestedComponent,
77+
// options?: MountingOptions<Props>
78+
// ): VueWrapper<ComponentPublicInstance<Props>>
79+
80+
// Functional component with emits
81+
export function mount<Props, E extends EmitsOptions = {}>(
82+
originalComponent: FunctionalComponent<Props, E>,
7683
options?: MountingOptions<Props>
7784
): VueWrapper<ComponentPublicInstance<Props>>
7885

test-dts/mount.d-test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { expectError, expectType } from 'tsd'
2-
import { DefineComponent, defineComponent, reactive } from 'vue'
2+
import {
3+
DefineComponent,
4+
defineComponent,
5+
FunctionalComponent,
6+
reactive
7+
} from 'vue'
38
import { mount } from '../src'
49

510
const AppWithDefine = defineComponent({
@@ -177,3 +182,24 @@ mount(ShimComponent, {
177182
}
178183
}
179184
})
185+
186+
// functional components
187+
declare const FunctionalComponent: FunctionalComponent<{
188+
bar: string
189+
level: number
190+
}>
191+
declare const FunctionalComponentEmit: FunctionalComponent<
192+
{
193+
bar: string
194+
level: number
195+
},
196+
{ hello: (foo: string, bar: string) => void }
197+
>
198+
199+
mount(FunctionalComponent)
200+
mount(defineComponent(FunctionalComponent))
201+
202+
mount(FunctionalComponentEmit)
203+
204+
// @ts-ignore vue 3.0.2 doesn't work. FIX: https://github.com/vuejs/vue-next/pull/2494
205+
mount(defineComponent(FunctionalComponentEmit))

tests/emit.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ describe('emitted', () => {
134134
})
135135

136136
it('gives a useful warning for functional components', () => {
137-
const Component: FunctionalComponent<{ bar: string; level: number }> = (
138-
props,
139-
ctx
140-
) => {
137+
const Component: FunctionalComponent<
138+
{ bar: string; level: number },
139+
{ hello: (foo: string, bar: string) => void }
140+
> = (props, ctx) => {
141141
return h(`h${props.level}`, {
142142
onClick: () => ctx.emit('hello', 'foo', props.bar)
143143
})

0 commit comments

Comments
 (0)