Skip to content

Commit 7c8caf7

Browse files
nandi95lmiller1990
authored andcommitted
Worked on typings
1 parent b08cb49 commit 7c8caf7

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/emit.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { setDevtoolsHook, devtools, ComponentPublicInstance } from 'vue'
2+
import { ComponentInternalInstance } from '@vue/runtime-core'
23

34
const enum DevtoolsHooks {
45
COMPONENT_EMIT = 'component:emit'
@@ -10,43 +11,44 @@ let events: Record<string, typeof componentEvents>
1011
export function emitted<T = unknown>(
1112
vm: ComponentPublicInstance,
1213
eventName?: string
13-
): T[] | Record<string, T[]> {
14+
): undefined | T[] | Record<string, T[]> {
1415
const cid = vm.$.uid
1516

1617
const vmEvents = (events as Record<string, Record<string, T[]>>)[cid] || {}
1718
if (eventName) {
18-
const emitted = vmEvents
19-
? (vmEvents as Record<string, T[]>)[eventName]
20-
: undefined
21-
return emitted
19+
return vmEvents ? (vmEvents as Record<string, T[]>)[eventName] : undefined
2220
}
2321

2422
return vmEvents as Record<string, T[]>
2523
}
24+
type Events = { [id: number]: Record<string, any> }
2625

2726
export const attachEmitListener = () => {
2827
events = {}
2928
// use devtools to capture this "emit"
3029
setDevtoolsHook(createDevTools(events))
3130
}
3231

33-
function createDevTools(events): any {
34-
const devTools: Partial<typeof devtools> = {
32+
function createDevTools(events: Events): any {
33+
return {
3534
emit(eventType, ...payload) {
3635
if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return
3736

3837
const [rootVM, componentVM, event, eventArgs] = payload
3938
recordEvent(events, componentVM, event, eventArgs)
4039
}
41-
}
42-
43-
return devTools
40+
} as Partial<typeof devtools>
4441
}
4542

46-
function recordEvent(events, vm, event, args): void {
43+
function recordEvent(
44+
events: Events,
45+
vm: ComponentInternalInstance,
46+
event: string,
47+
args: Events[number]
48+
): void {
4749
// Functional component wrapper creates a parent component
4850
let wrapperVm = vm
49-
while (typeof wrapperVm.type === 'function') wrapperVm = wrapperVm.parent
51+
while (typeof wrapperVm?.type === 'function') wrapperVm = wrapperVm.parent!
5052

5153
const cid = wrapperVm.uid
5254
if (!(cid in events)) {

src/mount.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
h,
33
createApp,
44
defineComponent,
5-
VNodeNormalizedChildren,
65
reactive,
76
FunctionalComponent,
87
ComponentPublicInstance,
@@ -230,7 +229,7 @@ export function mount(
230229
options?: MountingOptions<any>
231230
): VueWrapper<any> {
232231
// normalise the incoming component
233-
let component
232+
let component: DefineComponent
234233

235234
if (isFunctionalComponent(originalComponent)) {
236235
component = defineComponent({
@@ -261,7 +260,7 @@ export function mount(
261260
}
262261

263262
// handle any slots passed via mounting options
264-
const slots: VNodeNormalizedChildren =
263+
const slots =
265264
options?.slots &&
266265
Object.entries(options.slots).reduce(
267266
(
@@ -270,7 +269,7 @@ export function mount(
270269
): { [key: string]: Function } => {
271270
// case of an SFC getting passed
272271
if (typeof slot === 'object' && 'render' in slot) {
273-
acc[name] = slot.render
272+
acc[name] = slot.render!
274273
return acc
275274
}
276275

src/vueWrapper.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ export class VueWrapper<T extends ComponentPublicInstance> {
7474
return true
7575
}
7676

77-
emitted<T = unknown>(): Record<string, T[]>
78-
emitted<T = unknown>(eventName?: string): T[]
79-
emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
77+
emitted<T = unknown>(): undefined | Record<string, T[]>
78+
emitted<T = unknown>(eventName?: string): undefined | T[]
79+
emitted<T = unknown>(
80+
eventName?: string
81+
): undefined | T[] | Record<string, T[]> {
8082
return emitted(this.vm, eventName)
8183
}
8284

0 commit comments

Comments
 (0)