Skip to content

Commit eb1ab0d

Browse files
authored
fix: TypeScript type definitions for Event and EventTarget (#1151)
1 parent e4b7eb4 commit eb1ab0d

File tree

1 file changed

+188
-3
lines changed

1 file changed

+188
-3
lines changed

types/globals.d.ts

Lines changed: 188 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,18 +2185,203 @@ type KeyUsage =
21852185
| 'verify'
21862186
| 'wrapKey';
21872187

2188+
interface EventInit {
2189+
bubbles?: boolean;
2190+
cancelable?: boolean;
2191+
composed?: boolean;
2192+
}
2193+
2194+
interface EventListenerOptions {
2195+
capture?: boolean;
2196+
}
2197+
2198+
interface AddEventListenerOptions extends EventListenerOptions {
2199+
once?: boolean;
2200+
passive?: boolean;
2201+
// signal?: AbortSignal;
2202+
}
2203+
/**
2204+
* An event which takes place in the DOM.
2205+
*
2206+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event)
2207+
*/
2208+
interface Event {
2209+
/**
2210+
* Returns true or false depending on how event was initialized. True if event goes through its target's ancestors in reverse tree order, and false otherwise.
2211+
*
2212+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/bubbles)
2213+
*/
2214+
readonly bubbles: boolean;
2215+
/**
2216+
* @deprecated
2217+
*
2218+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble)
2219+
*/
2220+
cancelBubble: boolean;
2221+
/**
2222+
* Returns true or false depending on how event was initialized. Its return value does not always carry meaning, but true can indicate that part of the operation during which event was dispatched, can be canceled by invoking the preventDefault() method.
2223+
*
2224+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelable)
2225+
*/
2226+
readonly cancelable: boolean;
2227+
/**
2228+
* Returns true or false depending on how event was initialized. True if event invokes listeners past a ShadowRoot node that is the root of its target, and false otherwise.
2229+
*
2230+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composed)
2231+
*/
2232+
readonly composed: boolean;
2233+
/**
2234+
* Returns the object whose event listener's callback is currently being invoked.
2235+
*
2236+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/currentTarget)
2237+
*/
2238+
readonly currentTarget: EventTarget | null;
2239+
/**
2240+
* Returns true if preventDefault() was invoked successfully to indicate cancelation, and false otherwise.
2241+
*
2242+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/defaultPrevented)
2243+
*/
2244+
readonly defaultPrevented: boolean;
2245+
/**
2246+
* Returns the event's phase, which is one of NONE, CAPTURING_PHASE, AT_TARGET, and BUBBLING_PHASE.
2247+
*
2248+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/eventPhase)
2249+
*/
2250+
readonly eventPhase: number;
2251+
/**
2252+
* Returns true if event was dispatched by the user agent, and false otherwise.
2253+
*
2254+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/isTrusted)
2255+
*/
2256+
readonly isTrusted: boolean;
2257+
/**
2258+
* @deprecated
2259+
*
2260+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/returnValue)
2261+
*/
2262+
returnValue: boolean;
2263+
/**
2264+
* @deprecated
2265+
*
2266+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/srcElement)
2267+
*/
2268+
readonly srcElement: EventTarget | null;
2269+
/**
2270+
* Returns the object to which event is dispatched (its target).
2271+
*
2272+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/target)
2273+
*/
2274+
readonly target: EventTarget | null;
2275+
/**
2276+
* Returns the event's timestamp as the number of milliseconds measured relative to the time origin.
2277+
*
2278+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/timeStamp)
2279+
*/
2280+
readonly timeStamp: DOMHighResTimeStamp;
2281+
/**
2282+
* Returns the type of event, e.g. "click", "hashchange", or "submit".
2283+
*
2284+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/type)
2285+
*/
2286+
readonly type: string;
2287+
/**
2288+
* Returns the invocation target objects of event's path (objects on which listeners will be invoked), except for any nodes in shadow trees of which the shadow root's mode is "closed" that are not reachable from event's currentTarget.
2289+
*
2290+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composedPath)
2291+
*/
2292+
composedPath(): EventTarget[];
2293+
/**
2294+
* @deprecated
2295+
*
2296+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/initEvent)
2297+
*/
2298+
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
2299+
/**
2300+
* If invoked when the cancelable attribute value is true, and while executing a listener for the event with passive set to false, signals to the operation that caused event to be dispatched that it needs to be canceled.
2301+
*
2302+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/preventDefault)
2303+
*/
2304+
preventDefault(): void;
2305+
/**
2306+
* Invoking this method prevents event from reaching any registered event listeners after the current one finishes running and, when dispatched in a tree, also prevents event from reaching any other objects.
2307+
*
2308+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopImmediatePropagation)
2309+
*/
2310+
stopImmediatePropagation(): void;
2311+
/**
2312+
* When dispatched in a tree, invoking this method prevents event from reaching any objects other than the current object.
2313+
*
2314+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation)
2315+
*/
2316+
stopPropagation(): void;
2317+
readonly NONE: 0;
2318+
readonly CAPTURING_PHASE: 1;
2319+
readonly AT_TARGET: 2;
2320+
readonly BUBBLING_PHASE: 3;
2321+
}
2322+
2323+
declare var Event: {
2324+
prototype: Event;
2325+
new(type: string, eventInitDict?: EventInit): Event;
2326+
readonly NONE: 0;
2327+
readonly CAPTURING_PHASE: 1;
2328+
readonly AT_TARGET: 2;
2329+
readonly BUBBLING_PHASE: 3;
2330+
};
2331+
2332+
interface EventListener {
2333+
(evt: Event): void;
2334+
}
2335+
interface EventListenerObject {
2336+
handleEvent(object: Event): void;
2337+
}
2338+
type EventListenerOrEventListenerObject = EventListener | EventListenerObject;
2339+
21882340
/**
21892341
* EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.
21902342
*
21912343
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget)
21922344
* @group DOM Events
21932345
*/
21942346
interface EventTarget {
2195-
//addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
2196-
//dispatchEvent(event: Event): boolean;
2197-
//removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
2347+
/**
2348+
* 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.
2349+
*
2350+
* The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
2351+
*
2352+
* When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
2353+
*
2354+
* When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
2355+
*
2356+
* When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
2357+
*
2358+
* If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
2359+
*
2360+
* The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
2361+
*
2362+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener)
2363+
*/
2364+
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
2365+
/**
2366+
* 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.
2367+
*
2368+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent)
2369+
*/
2370+
dispatchEvent(event: Event): boolean;
2371+
/**
2372+
* Removes the event listener in target's event listener list with the same type, callback, and options.
2373+
*
2374+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener)
2375+
*/
2376+
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
21982377
}
21992378

2379+
declare var EventTarget: {
2380+
prototype: EventTarget;
2381+
new(): EventTarget;
2382+
};
2383+
2384+
22002385
/**
22012386
* Provides access to performance-related information for the current page. It's part of the High Resolution Time API, but is enhanced by the Performance Timeline API, the Navigation Timing API, the User Timing API, and the Resource Timing API.
22022387
*

0 commit comments

Comments
 (0)