diff --git a/src/emit.ts b/src/emit.ts index 25df51459..df2239cfc 100644 --- a/src/emit.ts +++ b/src/emit.ts @@ -1,4 +1,5 @@ import { setDevtoolsHook, devtools, ComponentPublicInstance } from 'vue' +import { ComponentInternalInstance } from '@vue/runtime-core' const enum DevtoolsHooks { COMPONENT_EMIT = 'component:emit' @@ -10,19 +11,17 @@ let events: Record export function emitted( vm: ComponentPublicInstance, eventName?: string -): T[] | Record { +): undefined | T[] | Record { const cid = vm.$.uid const vmEvents = (events as Record>)[cid] || {} if (eventName) { - const emitted = vmEvents - ? (vmEvents as Record)[eventName] - : undefined - return emitted + return vmEvents ? (vmEvents as Record)[eventName] : undefined } return vmEvents as Record } +type Events = { [id: number]: Record } export const attachEmitListener = () => { events = {} @@ -30,23 +29,26 @@ export const attachEmitListener = () => { setDevtoolsHook(createDevTools(events)) } -function createDevTools(events): any { - const devTools: Partial = { +function createDevTools(events: Events): any { + return { emit(eventType, ...payload) { if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return const [rootVM, componentVM, event, eventArgs] = payload recordEvent(events, componentVM, event, eventArgs) } - } - - return devTools + } as Partial } -function recordEvent(events, vm, event, args): void { +function recordEvent( + events: Events, + vm: ComponentInternalInstance, + event: string, + args: Events[number] +): void { // Functional component wrapper creates a parent component let wrapperVm = vm - while (typeof wrapperVm.type === 'function') wrapperVm = wrapperVm.parent + while (typeof wrapperVm?.type === 'function') wrapperVm = wrapperVm.parent! const cid = wrapperVm.uid if (!(cid in events)) { diff --git a/src/mount.ts b/src/mount.ts index 3e850c30c..38872b59f 100644 --- a/src/mount.ts +++ b/src/mount.ts @@ -2,7 +2,6 @@ import { h, createApp, defineComponent, - VNodeNormalizedChildren, reactive, FunctionalComponent, ComponentPublicInstance, @@ -230,7 +229,7 @@ export function mount( options?: MountingOptions ): VueWrapper { // normalise the incoming component - let component + let component: DefineComponent if (isFunctionalComponent(originalComponent)) { component = defineComponent({ @@ -261,7 +260,7 @@ export function mount( } // handle any slots passed via mounting options - const slots: VNodeNormalizedChildren = + const slots = options?.slots && Object.entries(options.slots).reduce( ( @@ -270,7 +269,7 @@ export function mount( ): { [key: string]: Function } => { // case of an SFC getting passed if (typeof slot === 'object' && 'render' in slot) { - acc[name] = slot.render + acc[name] = slot.render! return acc } diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 732d1c0c4..94d8c846e 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -74,9 +74,11 @@ export class VueWrapper { return true } - emitted(): Record - emitted(eventName?: string): T[] - emitted(eventName?: string): T[] | Record { + emitted(): undefined | Record + emitted(eventName?: string): undefined | T[] + emitted( + eventName?: string + ): undefined | T[] | Record { return emitted(this.vm, eventName) }