Skip to content
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

feat: add type parameter to EventTarget #1712

Open
wants to merge 1 commit 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
8 changes: 5 additions & 3 deletions baselines/audioworklet.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ interface EventListenerObject {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
*/
interface EventTarget {
interface EventTarget<T = any> {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
*
Expand All @@ -601,7 +601,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
addEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: AddEventListenerOptions | boolean): void;
addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
Expand All @@ -613,7 +614,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
removeEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: EventListenerOptions | boolean): void;
removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: EventListenerOptions | boolean): void;
}

declare var EventTarget: {
Expand Down
8 changes: 5 additions & 3 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8298,7 +8298,7 @@ declare var EventSource: {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
*/
interface EventTarget {
interface EventTarget<T = any> {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
*
Expand All @@ -8316,7 +8316,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
addEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: AddEventListenerOptions | boolean): void;
addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
Expand All @@ -8328,7 +8329,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
removeEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: EventListenerOptions | boolean): void;
removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: EventListenerOptions | boolean): void;
}

declare var EventTarget: {
Expand Down
8 changes: 5 additions & 3 deletions baselines/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ declare var EventSource: {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
*/
interface EventTarget {
interface EventTarget<T = any> {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
*
Expand All @@ -2420,7 +2420,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
addEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: AddEventListenerOptions | boolean): void;
addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
Expand All @@ -2432,7 +2433,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
removeEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: EventListenerOptions | boolean): void;
removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: EventListenerOptions | boolean): void;
}

declare var EventTarget: {
Expand Down
8 changes: 5 additions & 3 deletions baselines/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,7 @@ declare var EventSource: {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
*/
interface EventTarget {
interface EventTarget<T = any> {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
*
Expand All @@ -2347,7 +2347,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
addEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: AddEventListenerOptions | boolean): void;
addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
Expand All @@ -2359,7 +2360,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
removeEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: EventListenerOptions | boolean): void;
removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: EventListenerOptions | boolean): void;
}

declare var EventTarget: {
Expand Down
8 changes: 5 additions & 3 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2612,7 +2612,7 @@ declare var EventSource: {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
*/
interface EventTarget {
interface EventTarget<T = any> {
/**
* Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
*
Expand All @@ -2630,7 +2630,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
*/
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
addEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: AddEventListenerOptions | boolean): void;
addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: AddEventListenerOptions | boolean): void;
/**
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
*
Expand All @@ -2642,7 +2643,8 @@ interface EventTarget {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
*/
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
removeEventListener<K extends keyof T>(type: K, callback: (this: EventTarget<T>, ev: T[K]) => any, options?: EventListenerOptions | boolean): void;
removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: EventListenerOptions | boolean): void;
}

declare var EventTarget: {
Expand Down
70 changes: 68 additions & 2 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2998,29 +2998,95 @@
}
},
"EventTarget": {
"typeParameters": [
{
"name": "T",
"default": "any"
}
],
"methods": {
"method": {
"addEventListener": {
"signature": {
"0": {
"typeParameters": [
{
"name": "K extends keyof T",
"default": ""
}
],
"param": [
{
"name": "type",
"overrideType": "K"
},
{
"name": "callback",
"overrideType": "(this: EventTarget<T>, ev: T[K]) => any",
"nullable": false
}
],
"overrideType": "void"
},
"1": {
"param": [
{
"name": "type",
"overrideType": "string"
},
{
"name": "callback",
"overrideType": "EventListenerOrEventListenerObject"
},
{
"name": "options",
"overrideType": "AddEventListenerOptions | boolean",
"optional": true
}
]
],
"overrideType": "void"
}
}
},
"removeEventListener": {
"signature": {
"0": {
"typeParameters": [
{
"name": "K extends keyof T",
"default": ""
}
],
"param": [
{
"name": "type",
"overrideType": "K"
},
{
"name": "callback",
"overrideType": "(this: EventTarget<T>, ev: T[K]) => any",
"nullable": false
}
],
"overrideType": "void"
},
"1": {
"param": [
{
"name": "type",
"overrideType": "string"
},
{
"name": "callback",
"overrideType": "EventListenerOrEventListenerObject"
},
{
"name": "options",
"overrideType": "EventListenerOptions | boolean",
"optional": true
}
]
],
"overrideType": "void"
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions unittests/files/eventtarget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare const target: EventTarget;

target.addEventListener("custom", (event) => {
event.target;
});

interface CustomEventMap {
custom: CustomEvent<{ data: number }>;
}

declare const customTarget: EventTarget<CustomEventMap>;

customTarget.addEventListener("custom", (event) => {
event.detail.data;
});
Loading