Skip to content

types(jsx): Allow modifiers to jSX event #9542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/dts-test/tsx.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ expectType<JSX.Element>(
// infer correct event type
expectType<EventTarget | null>(e.target)
}}
onInputCapture={e => {
expectType<EventTarget | null>(e.target)
}}
onInputOnce={e => {
expectType<EventTarget | null>(e.target)
}}
onInputOnceCapture={e => {
expectType<EventTarget | null>(e.target)
}}
onInputCaptureOnce={e => {
expectType<EventTarget | null>(e.target)
}}
onInputPassive={e => {
// infer correct event type
expectType<EventTarget | null>(e.target)
}}
onInputCapturePassive={e => {
expectType<EventTarget | null>(e.target)
}}
onInputOncePassive={e => {
expectType<EventTarget | null>(e.target)
}}
onInputOnceCapturePassive={e => {
expectType<EventTarget | null>(e.target)
}}
onInputPassiveCaptureOnce={e => {
expectType<EventTarget | null>(e.target)
}}
/>
)

Expand Down
25 changes: 24 additions & 1 deletion packages/runtime-dom/src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ export interface IntrinsicElementAttributes {
view: SVGAttributes
}

export interface Events {
export interface BaseEvents {
// clipboard events
onCopy: ClipboardEvent
onCut: ClipboardEvent
Expand Down Expand Up @@ -1351,6 +1351,29 @@ export interface Events {
onTransitionstart: TransitionEvent
}

// All possible combinations, could be generated programmatically but
// probably too much trouble for little gain, especially it will incur more overhead on the typing
type EventModifiers =
| 'Capture'
| 'Once'
| 'Passive'
| 'CaptureOnce'
| 'OnceCapture'
| 'CapturePassive'
| 'PassiveCapture'
| 'OncePassive'
| 'PassiveOnce'
| 'CaptureOncePassive'
| 'CapturePassiveOnce'
| 'OnceCapturePassive'
| 'OncePassiveCapture'
| 'PassiveCaptureOnce'
| 'PassiveOnceCapture'

type Events = BaseEvents & {
[K in keyof BaseEvents as `${K & string}${EventModifiers}`]: BaseEvents[K]
}

type EventHandlers<E> = {
[K in keyof E]?: E[K] extends (...args: any) => any
? E[K]
Expand Down
Loading