Skip to content

Commit 6ed45db

Browse files
leondummdidumm
andauthored
fix: add document and window to svelte/events on types. (#13260)
fixes #13259 fixes #12665 put it into dts for now because of microsoft/TypeScript#59980 --------- Co-authored-by: Simon Holthausen <[email protected]>
1 parent 9204d69 commit 6ed45db

File tree

6 files changed

+112
-40
lines changed

6 files changed

+112
-40
lines changed

packages/svelte/scripts/generate-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ await createBundle({
3737
[`${pkg.name}/server`]: `${dir}/src/server/index.d.ts`,
3838
[`${pkg.name}/store`]: `${dir}/src/store/public.d.ts`,
3939
[`${pkg.name}/transition`]: `${dir}/src/transition/public.d.ts`,
40-
[`${pkg.name}/events`]: `${dir}/src/events/index.js`,
40+
[`${pkg.name}/events`]: `${dir}/src/events/public.d.ts`,
4141
// TODO remove in Svelte 6
4242
[`${pkg.name}/types/compiler/preprocess`]: `${dir}/src/compiler/preprocess/legacy-public.d.ts`,
4343
[`${pkg.name}/types/compiler/interfaces`]: `${dir}/src/compiler/types/legacy-interfaces.d.ts`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Once https://github.com/microsoft/TypeScript/issues/59980 is fixed we can put these overloads into the JSDoc comments of the `on` function
2+
3+
/**
4+
* Attaches an event handler to the window and returns a function that removes the handler. Using this
5+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
6+
* (with attributes like `onclick`), which use event delegation for performance reasons
7+
*/
8+
export function on<Type extends keyof WindowEventMap>(
9+
window: Window,
10+
type: Type,
11+
handler: (this: Window, event: WindowEventMap[Type]) => any,
12+
options?: AddEventListenerOptions | undefined
13+
): () => void;
14+
/**
15+
* Attaches an event handler to the document and returns a function that removes the handler. Using this
16+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
17+
* (with attributes like `onclick`), which use event delegation for performance reasons
18+
*/
19+
export function on<Type extends keyof DocumentEventMap>(
20+
document: Document,
21+
type: Type,
22+
handler: (this: Document, event: DocumentEventMap[Type]) => any,
23+
options?: AddEventListenerOptions | undefined
24+
): () => void;
25+
/**
26+
* Attaches an event handler to an element and returns a function that removes the handler. Using this
27+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
28+
* (with attributes like `onclick`), which use event delegation for performance reasons
29+
*/
30+
export function on<Element extends HTMLElement, Type extends keyof HTMLElementEventMap>(
31+
element: Element,
32+
type: Type,
33+
handler: (this: Element, event: HTMLElementEventMap[Type]) => any,
34+
options?: AddEventListenerOptions | undefined
35+
): () => void;
36+
/**
37+
* Attaches an event handler to an element and returns a function that removes the handler. Using this
38+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
39+
* (with attributes like `onclick`), which use event delegation for performance reasons
40+
*/
41+
export function on<Element extends MediaQueryList, Type extends keyof MediaQueryListEventMap>(
42+
element: Element,
43+
type: Type,
44+
handler: (this: Element, event: MediaQueryListEventMap[Type]) => any,
45+
options?: AddEventListenerOptions | undefined
46+
): () => void;
47+
/**
48+
* Attaches an event handler to an element and returns a function that removes the handler. Using this
49+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
50+
* (with attributes like `onclick`), which use event delegation for performance reasons
51+
*/
52+
export function on(
53+
element: EventTarget,
54+
type: string,
55+
handler: EventListener,
56+
options?: AddEventListenerOptions | undefined
57+
): () => void;

packages/svelte/src/internal/client/dom/elements/events.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,6 @@ export function create_event(event_name, dom, handler, options) {
8383
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
8484
* (with attributes like `onclick`), which use event delegation for performance reasons
8585
*
86-
* @template {HTMLElement} Element
87-
* @template {keyof HTMLElementEventMap} Type
88-
* @overload
89-
* @param {Element} element
90-
* @param {Type} type
91-
* @param {(this: Element, event: HTMLElementEventMap[Type]) => any} handler
92-
* @param {AddEventListenerOptions} [options]
93-
* @returns {() => void}
94-
*/
95-
96-
/**
97-
* Attaches an event handler to an element and returns a function that removes the handler. Using this
98-
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
99-
* (with attributes like `onclick`), which use event delegation for performance reasons
100-
*
101-
* @overload
102-
* @param {EventTarget} element
103-
* @param {string} type
104-
* @param {EventListener} handler
105-
* @param {AddEventListenerOptions} [options]
106-
* @returns {() => void}
107-
*/
108-
109-
/**
11086
* @param {EventTarget} element
11187
* @param {string} type
11288
* @param {EventListener} handler

packages/svelte/tests/types/events.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { on } from 'svelte/events';
44

55
on(document.body, 'click', (e) => e.button);
66

7+
on(window, 'click', (e) => e.button);
8+
9+
on(document, 'click', (e) => e.button);
10+
711
on(
812
document.body,
913
'clidck',
@@ -12,14 +16,6 @@ on(
1216
e.button
1317
);
1418

15-
on(
16-
window,
17-
'click',
18-
(e) =>
19-
// @ts-expect-error ideally we'd know this is a MouseEvent here, too, but for keeping the types sane, we currently don't
20-
e.button
21-
);
22-
2319
on(
2420
// @ts-expect-error
2521
'asd',

packages/svelte/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"svelte": ["./src/index.d.ts"],
2020
"svelte/action": ["./src/action/public.d.ts"],
2121
"svelte/compiler": ["./src/compiler/public.d.ts"],
22-
"svelte/events": ["./src/events/index.js"],
22+
"svelte/events": ["./src/events/public.d.ts"],
2323
"svelte/internal/client": ["./src/internal/client/index.js"],
2424
"svelte/legacy": ["./src/legacy/legacy-client.js"],
2525
"svelte/motion": ["./src/motion/public.d.ts"],

packages/svelte/types/index.d.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,20 +1991,63 @@ declare module 'svelte/transition' {
19911991
}
19921992

19931993
declare module 'svelte/events' {
1994+
// Once https://github.com/microsoft/TypeScript/issues/59980 is fixed we can put these overloads into the JSDoc comments of the `on` function
1995+
1996+
/**
1997+
* Attaches an event handler to the window and returns a function that removes the handler. Using this
1998+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
1999+
* (with attributes like `onclick`), which use event delegation for performance reasons
2000+
*/
2001+
export function on<Type extends keyof WindowEventMap>(
2002+
window: Window,
2003+
type: Type,
2004+
handler: (this: Window, event: WindowEventMap[Type]) => any,
2005+
options?: AddEventListenerOptions | undefined
2006+
): () => void;
2007+
/**
2008+
* Attaches an event handler to the document and returns a function that removes the handler. Using this
2009+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
2010+
* (with attributes like `onclick`), which use event delegation for performance reasons
2011+
*/
2012+
export function on<Type extends keyof DocumentEventMap>(
2013+
document: Document,
2014+
type: Type,
2015+
handler: (this: Document, event: DocumentEventMap[Type]) => any,
2016+
options?: AddEventListenerOptions | undefined
2017+
): () => void;
19942018
/**
19952019
* Attaches an event handler to an element and returns a function that removes the handler. Using this
19962020
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
19972021
* (with attributes like `onclick`), which use event delegation for performance reasons
1998-
*
1999-
* */
2000-
export function on<Element extends HTMLElement, Type extends keyof HTMLElementEventMap>(element: Element, type: Type, handler: (this: Element, event: HTMLElementEventMap[Type]) => any, options?: AddEventListenerOptions | undefined): () => void;
2022+
*/
2023+
export function on<Element extends HTMLElement, Type extends keyof HTMLElementEventMap>(
2024+
element: Element,
2025+
type: Type,
2026+
handler: (this: Element, event: HTMLElementEventMap[Type]) => any,
2027+
options?: AddEventListenerOptions | undefined
2028+
): () => void;
20012029
/**
20022030
* Attaches an event handler to an element and returns a function that removes the handler. Using this
20032031
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
20042032
* (with attributes like `onclick`), which use event delegation for performance reasons
2005-
*
2006-
* */
2007-
export function on(element: EventTarget, type: string, handler: EventListener, options?: AddEventListenerOptions | undefined): () => void;
2033+
*/
2034+
export function on<Element extends MediaQueryList, Type extends keyof MediaQueryListEventMap>(
2035+
element: Element,
2036+
type: Type,
2037+
handler: (this: Element, event: MediaQueryListEventMap[Type]) => any,
2038+
options?: AddEventListenerOptions | undefined
2039+
): () => void;
2040+
/**
2041+
* Attaches an event handler to an element and returns a function that removes the handler. Using this
2042+
* rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
2043+
* (with attributes like `onclick`), which use event delegation for performance reasons
2044+
*/
2045+
export function on(
2046+
element: EventTarget,
2047+
type: string,
2048+
handler: EventListener,
2049+
options?: AddEventListenerOptions | undefined
2050+
): () => void;
20082051

20092052
export {};
20102053
}

0 commit comments

Comments
 (0)