h,
SlotsType,
Slots,
- VNode
+ VNode,
+ withKeys,
+ withModifiers
} from 'vue'
import { describe, expectType, IsUnion } from './utils'
expectType<SizeType>(CompA.$props.size)
})
+describe('withKeys and withModifiers as pro', () => {
+ const onKeydown = withKeys(e => {}, [''])
+ const onClick = withModifiers(e => {}, [''])
+ ;<input onKeydown={onKeydown} onClick={onClick} />
+})
+
import {
DefineComponent,
ComponentOptionsMixin,
/**
* @private
*/
-export const withModifiers = (
- fn: Function & { _withMods?: Function },
+export const withModifiers = <
+ T extends (event: Event, ...args: unknown[]) => any
+>(
+ fn: T & { _withMods?: T },
modifiers: string[]
) => {
return (
fn._withMods ||
- (fn._withMods = (event: Event, ...args: unknown[]) => {
+ (fn._withMods = ((event, ...args) => {
for (let i = 0; i < modifiers.length; i++) {
const guard = modifierGuards[modifiers[i]]
if (guard && guard(event, modifiers)) return
}
return fn(event, ...args)
- })
+ }) as T)
)
}
/**
* @private
*/
-export const withKeys = (
- fn: Function & { _withKeys?: Function },
+export const withKeys = <T extends (event: KeyboardEvent) => any>(
+ fn: T & { _withKeys?: T },
modifiers: string[]
) => {
let globalKeyCodes: LegacyConfig['keyCodes']
return (
fn._withKeys ||
- (fn._withKeys = (event: KeyboardEvent) => {
+ (fn._withKeys = (event => {
if (!('key' in event)) {
return
}
}
}
}
- })
+ }) as T)
)
}