1
+ // @ts -expect-error No DefinitelyTyped package exists
1
2
import eventTypes from 'dom-event-types'
2
3
3
4
interface TriggerOptions {
@@ -9,7 +10,7 @@ interface TriggerOptions {
9
10
10
11
interface EventParams {
11
12
eventType : string
12
- modifiers : string [ ]
13
+ modifiers : KeyNameArray
13
14
options ?: TriggerOptions
14
15
}
15
16
@@ -21,8 +22,8 @@ const mouseKeyModifiers = ['left', 'middle', 'right']
21
22
/**
22
23
* Groups modifiers into lists
23
24
*/
24
- function generateModifiers ( modifiers : string [ ] , isOnClick : boolean ) {
25
- const keyModifiers : string [ ] = [ ]
25
+ function generateModifiers ( modifiers : KeyNameArray , isOnClick : boolean ) {
26
+ const keyModifiers : KeyNameArray = [ ]
26
27
const systemModifiers : string [ ] = [ ]
27
28
28
29
for ( let i = 0 ; i < modifiers . length ; i ++ ) {
@@ -50,6 +51,7 @@ function generateModifiers(modifiers: string[], isOnClick: boolean) {
50
51
}
51
52
}
52
53
54
+ export type KeyNameArray = Array < keyof typeof keyCodesByKeyName >
53
55
export const keyCodesByKeyName = {
54
56
backspace : 8 ,
55
57
tab : 9 ,
@@ -98,10 +100,13 @@ function getEventProperties(eventParams: EventParams) {
98
100
99
101
// convert `shift, ctrl` to `shiftKey, ctrlKey`
100
102
// 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
+ )
105
110
106
111
// get the keyCode for backwards compat
107
112
const keyCode =
@@ -148,8 +153,12 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
148
153
// split eventString like `keydown.ctrl.shift.c` into `keydown` and array of modifiers
149
154
const [ eventType , ...modifiers ] = eventString . split ( '.' )
150
155
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 )
153
162
const eventPrototype = Object . getPrototypeOf ( event )
154
163
155
164
// attach custom options to the event, like `relatedTarget` and so on.
0 commit comments