Skip to content

Commit 43c3cfd

Browse files
authored
fix(types): improve return type withKeys and withModifiers (#9734)
1 parent 9ea2b86 commit 43c3cfd

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/dts-test/defineComponent.test-d.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
h,
1212
SlotsType,
1313
Slots,
14-
VNode
14+
VNode,
15+
withKeys,
16+
withModifiers
1517
} from 'vue'
1618
import { describe, expectType, IsUnion } from './utils'
1719

@@ -1497,6 +1499,12 @@ describe('should work when props type is incompatible with setup returned type '
14971499
expectType<SizeType>(CompA.$props.size)
14981500
})
14991501

1502+
describe('withKeys and withModifiers as pro', () => {
1503+
const onKeydown = withKeys(e => {}, [''])
1504+
const onClick = withModifiers(e => {}, [''])
1505+
;<input onKeydown={onKeydown} onClick={onClick} />
1506+
})
1507+
15001508
import {
15011509
DefineComponent,
15021510
ComponentOptionsMixin,

packages/runtime-dom/src/directives/vOn.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ const modifierGuards: Record<
3232
/**
3333
* @private
3434
*/
35-
export const withModifiers = (
36-
fn: Function & { _withMods?: Function },
35+
export const withModifiers = <
36+
T extends (event: Event, ...args: unknown[]) => any
37+
>(
38+
fn: T & { _withMods?: T },
3739
modifiers: string[]
3840
) => {
3941
return (
4042
fn._withMods ||
41-
(fn._withMods = (event: Event, ...args: unknown[]) => {
43+
(fn._withMods = ((event, ...args) => {
4244
for (let i = 0; i < modifiers.length; i++) {
4345
const guard = modifierGuards[modifiers[i]]
4446
if (guard && guard(event, modifiers)) return
4547
}
4648
return fn(event, ...args)
47-
})
49+
}) as T)
4850
)
4951
}
5052

@@ -63,8 +65,8 @@ const keyNames: Record<string, string | string[]> = {
6365
/**
6466
* @private
6567
*/
66-
export const withKeys = (
67-
fn: Function & { _withKeys?: Function },
68+
export const withKeys = <T extends (event: KeyboardEvent) => any>(
69+
fn: T & { _withKeys?: T },
6870
modifiers: string[]
6971
) => {
7072
let globalKeyCodes: LegacyConfig['keyCodes']
@@ -88,7 +90,7 @@ export const withKeys = (
8890

8991
return (
9092
fn._withKeys ||
91-
(fn._withKeys = (event: KeyboardEvent) => {
93+
(fn._withKeys = (event => {
9294
if (!('key' in event)) {
9395
return
9496
}
@@ -123,6 +125,6 @@ export const withKeys = (
123125
}
124126
}
125127
}
126-
})
128+
}) as T)
127129
)
128130
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy