Skip to content

Commit b08cb49

Browse files
nandi95lmiller1990
authored andcommitted
Started resolving typing issues
1 parent dd051cd commit b08cb49

File tree

12 files changed

+46
-29
lines changed

12 files changed

+46
-29
lines changed

src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ interface Plugin<Instance, O> {
2121
class Pluggable<Instance = DOMWrapper<Element>> {
2222
installedPlugins: Plugin<Instance, any>[] = []
2323

24-
install<O>(handler: (instance: Instance) => Record<string, any>)
24+
install<O>(handler: (instance: Instance) => Record<string, any>): void
2525
install<O>(
2626
handler: (instance: Instance, options: O) => Record<string, any>,
2727
options: O
28-
)
28+
): void
2929
install<O>(
3030
handler: (instance: Instance, options?: O) => Record<string, any>,
3131
options?: O
32-
) {
32+
): void {
3333
if (typeof handler !== 'function') {
3434
console.error('plugin.install must receive a function')
3535
handler = () => ({})

src/createDomEvent.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-expect-error No DefinitelyTyped package exists
12
import eventTypes from 'dom-event-types'
23

34
interface TriggerOptions {
@@ -9,7 +10,7 @@ interface TriggerOptions {
910

1011
interface EventParams {
1112
eventType: string
12-
modifiers: string[]
13+
modifiers: KeyNameArray
1314
options?: TriggerOptions
1415
}
1516

@@ -21,8 +22,8 @@ const mouseKeyModifiers = ['left', 'middle', 'right']
2122
/**
2223
* Groups modifiers into lists
2324
*/
24-
function generateModifiers(modifiers: string[], isOnClick: boolean) {
25-
const keyModifiers: string[] = []
25+
function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
26+
const keyModifiers: KeyNameArray = []
2627
const systemModifiers: string[] = []
2728

2829
for (let i = 0; i < modifiers.length; i++) {
@@ -50,6 +51,7 @@ function generateModifiers(modifiers: string[], isOnClick: boolean) {
5051
}
5152
}
5253

54+
export type KeyNameArray = Array<keyof typeof keyCodesByKeyName>
5355
export const keyCodesByKeyName = {
5456
backspace: 8,
5557
tab: 9,
@@ -98,10 +100,13 @@ function getEventProperties(eventParams: EventParams) {
98100

99101
// convert `shift, ctrl` to `shiftKey, ctrlKey`
100102
// allows trigger('keydown.shift.ctrl.n') directly
101-
const systemModifiersMeta = systemModifiers.reduce((all, key) => {
102-
all[`${key}Key`] = true
103-
return all
104-
}, {})
103+
const systemModifiersMeta = systemModifiers.reduce(
104+
(all: Record<string, boolean>, key) => {
105+
all[`${key}Key`] = true
106+
return all
107+
},
108+
{}
109+
)
105110

106111
// get the keyCode for backwards compat
107112
const keyCode =
@@ -148,8 +153,12 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
148153
// split eventString like `keydown.ctrl.shift.c` into `keydown` and array of modifiers
149154
const [eventType, ...modifiers] = eventString.split('.')
150155

151-
const eventParams: EventParams = { eventType, modifiers, options }
152-
const event: Event = createEvent(eventParams)
156+
const eventParams: EventParams = {
157+
eventType,
158+
modifiers: modifiers as KeyNameArray,
159+
options
160+
}
161+
const event: Event & TriggerOptions = createEvent(eventParams)
153162
const eventPrototype = Object.getPrototypeOf(event)
154163

155164
// attach custom options to the event, like `relatedTarget` and so on.

src/domWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ export class DOMWrapper<ElementType extends Element> {
158158
}
159159

160160
element.selected = true
161-
let parentElement = element.parentElement
161+
let parentElement = element.parentElement!
162162

163163
if (parentElement.tagName === 'OPTGROUP') {
164-
parentElement = parentElement.parentElement
164+
parentElement = parentElement.parentElement!
165165
}
166166

167167
return new DOMWrapper(parentElement).trigger('change')

src/stubs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { matchName } from './utils/matchName'
1717
import { ComponentInternalInstance } from '@vue/runtime-core'
1818

1919
interface StubOptions {
20-
name?: string
20+
name: string
2121
props?: any
2222
propsDeclaration?: any
2323
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,5 @@ export type GlobalMountOptions = {
141141
*/
142142
renderStubDefaultSlot?: boolean
143143
}
144+
145+
export type VueElement = Element & { __vue_app__?: any }

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const mergeDeep = (
4242
target: Record<string, any>,
4343
source: Record<string, any>
4444
) => {
45-
const isObject = (obj: unknown): obj is Object =>
46-
obj && typeof obj === 'object'
45+
const isObject = (obj: unknown): obj is Record<string, any> =>
46+
!!obj && typeof obj === 'object'
4747

4848
if (!isObject(target) || !isObject(source)) {
4949
return source
@@ -101,6 +101,6 @@ export function textContent(element: Element): string {
101101
// we check if the element is a comment first
102102
// to return an empty string in that case, instead of the comment content
103103
return element.nodeType !== Node.COMMENT_NODE
104-
? element.textContent?.trim()
104+
? element.textContent?.trim() ?? ''
105105
: ''
106106
}

src/utils/compileSlots.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { compile } from '@vue/compiler-dom'
22
import * as vue from 'vue'
3+
import type { SetupContext } from 'vue'
34

45
export function processSlot(source = '', Vue = vue) {
56
let template = source.trim()
@@ -25,14 +26,14 @@ export function processSlot(source = '', Vue = vue) {
2526
components: {
2627
SlotWrapper: {
2728
inheritAttrs: false,
28-
setup(_, { slots, attrs }) {
29+
setup(_: Record<string, any>, ctx: SetupContext) {
2930
return () => {
30-
const names = Object.keys(slots)
31+
const names = Object.keys(ctx.slots)
3132
if (names.length === 0) {
3233
return []
3334
} else {
3435
const slotName = names[0]
35-
return slots[slotName](attrs)
36+
return ctx.slots[slotName]!(ctx.attrs)
3637
}
3738
}
3839
}

src/utils/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function matches(
3535
return true
3636
}
3737

38-
let componentName
38+
let componentName: string | undefined
3939
if ('name' in nodeType || 'displayName' in nodeType) {
4040
// match normal component definitions or functional components
4141
componentName = nodeType.name || nodeType.displayName

src/utils/isElementVisible.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function isAttributeVisible<T extends Element>(element: T) {
2626
)
2727
}
2828

29-
export function isElementVisible<T extends Element>(element: T) {
29+
export function isElementVisible<T extends Element>(element: T): boolean {
3030
return (
3131
element.nodeName !== '#comment' &&
3232
isStyleVisible(element) &&

src/utils/matchName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { camelize, capitalize } from './vueShared'
22

3-
export function matchName(target: string, sourceName: string) {
3+
export function matchName(target: string, sourceName: string): boolean {
44
const camelized = camelize(target)
55
const capitalized = capitalize(camelized)
66

77
return (
8-
sourceName &&
8+
!!sourceName &&
99
(sourceName === target ||
1010
sourceName === camelized ||
1111
sourceName === capitalized ||

0 commit comments

Comments
 (0)