events.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * An implementation of the `EventTarget` interface.
  3. * @see https://dom.spec.whatwg.org/#eventtarget
  4. */
  5. declare class EventTarget<TEventMap extends Record<string, Event$1> = Record<string, Event$1>, TMode extends "standard" | "strict" = "standard"> {
  6. /**
  7. * Initialize this instance.
  8. */
  9. constructor();
  10. /**
  11. * Add an event listener.
  12. * @param type The event type.
  13. * @param callback The event listener.
  14. * @param options Options.
  15. */
  16. addEventListener<T extends string & keyof TEventMap>(type: T, callback?: EventTarget.EventListener<this, TEventMap[T]> | null, options?: EventTarget.AddOptions): void;
  17. /**
  18. * Add an event listener.
  19. * @param type The event type.
  20. * @param callback The event listener.
  21. * @param options Options.
  22. */
  23. addEventListener(type: string, callback?: EventTarget.FallbackEventListener<this, TMode>, options?: EventTarget.AddOptions): void;
  24. /**
  25. * Add an event listener.
  26. * @param type The event type.
  27. * @param callback The event listener.
  28. * @param capture The capture flag.
  29. * @deprecated Use `{capture: boolean}` object instead of a boolean value.
  30. */
  31. addEventListener<T extends string & keyof TEventMap>(type: T, callback: EventTarget.EventListener<this, TEventMap[T]> | null | undefined, capture: boolean): void;
  32. /**
  33. * Add an event listener.
  34. * @param type The event type.
  35. * @param callback The event listener.
  36. * @param capture The capture flag.
  37. * @deprecated Use `{capture: boolean}` object instead of a boolean value.
  38. */
  39. addEventListener(type: string, callback: EventTarget.FallbackEventListener<this, TMode>, capture: boolean): void;
  40. /**
  41. * Remove an added event listener.
  42. * @param type The event type.
  43. * @param callback The event listener.
  44. * @param options Options.
  45. */
  46. removeEventListener<T extends string & keyof TEventMap>(type: T, callback?: EventTarget.EventListener<this, TEventMap[T]> | null, options?: EventTarget.Options): void;
  47. /**
  48. * Remove an added event listener.
  49. * @param type The event type.
  50. * @param callback The event listener.
  51. * @param options Options.
  52. */
  53. removeEventListener(type: string, callback?: EventTarget.FallbackEventListener<this, TMode>, options?: EventTarget.Options): void;
  54. /**
  55. * Remove an added event listener.
  56. * @param type The event type.
  57. * @param callback The event listener.
  58. * @param capture The capture flag.
  59. * @deprecated Use `{capture: boolean}` object instead of a boolean value.
  60. */
  61. removeEventListener<T extends string & keyof TEventMap>(type: T, callback: EventTarget.EventListener<this, TEventMap[T]> | null | undefined, capture: boolean): void;
  62. /**
  63. * Remove an added event listener.
  64. * @param type The event type.
  65. * @param callback The event listener.
  66. * @param capture The capture flag.
  67. * @deprecated Use `{capture: boolean}` object instead of a boolean value.
  68. */
  69. removeEventListener(type: string, callback: EventTarget.FallbackEventListener<this, TMode>, capture: boolean): void;
  70. /**
  71. * Dispatch an event.
  72. * @param event The `Event` object to dispatch.
  73. */
  74. dispatchEvent<T extends string & keyof TEventMap>(event: EventTarget.EventData<TEventMap, TMode, T>): boolean;
  75. /**
  76. * Dispatch an event.
  77. * @param event The `Event` object to dispatch.
  78. */
  79. dispatchEvent(event: EventTarget.FallbackEvent<TMode>): boolean;
  80. }
  81. declare namespace EventTarget {
  82. /**
  83. * The event listener.
  84. */
  85. type EventListener<TEventTarget extends EventTarget<any, any>, TEvent extends Event$1> = CallbackFunction<TEventTarget, TEvent> | CallbackObject<TEvent>;
  86. /**
  87. * The event listener function.
  88. */
  89. interface CallbackFunction<TEventTarget extends EventTarget<any, any>, TEvent extends Event$1> {
  90. (this: TEventTarget, event: TEvent): void;
  91. }
  92. /**
  93. * The event listener object.
  94. * @see https://dom.spec.whatwg.org/#callbackdef-eventlistener
  95. */
  96. interface CallbackObject<TEvent extends Event$1> {
  97. handleEvent(event: TEvent): void;
  98. }
  99. /**
  100. * The common options for both `addEventListener` and `removeEventListener` methods.
  101. * @see https://dom.spec.whatwg.org/#dictdef-eventlisteneroptions
  102. */
  103. interface Options {
  104. capture?: boolean;
  105. }
  106. /**
  107. * The options for the `addEventListener` methods.
  108. * @see https://dom.spec.whatwg.org/#dictdef-addeventlisteneroptions
  109. */
  110. interface AddOptions extends Options {
  111. passive?: boolean;
  112. once?: boolean;
  113. signal?: AbortSignal | null | undefined;
  114. }
  115. /**
  116. * The abort signal.
  117. * @see https://dom.spec.whatwg.org/#abortsignal
  118. */
  119. interface AbortSignal extends EventTarget<{
  120. abort: Event$1;
  121. }> {
  122. readonly aborted: boolean;
  123. onabort: CallbackFunction<this, Event$1> | null;
  124. }
  125. /**
  126. * The event data to dispatch in strict mode.
  127. */
  128. type EventData<TEventMap extends Record<string, Event$1>, TMode extends "standard" | "strict", TEventType extends string> = TMode extends "strict" ? IsValidEventMap<TEventMap> extends true ? ExplicitType<TEventType> & Omit<TEventMap[TEventType], keyof Event$1> & Partial<Omit<Event$1, "type">> : never : never;
  129. /**
  130. * Define explicit `type` property if `T` is a string literal.
  131. * Otherwise, never.
  132. */
  133. type ExplicitType<T extends string> = string extends T ? never : {
  134. readonly type: T;
  135. };
  136. /**
  137. * The event listener type in standard mode.
  138. * Otherwise, never.
  139. */
  140. type FallbackEventListener<TEventTarget extends EventTarget<any, any>, TMode extends "standard" | "strict"> = TMode extends "standard" ? EventListener<TEventTarget, Event$1> | null | undefined : never;
  141. /**
  142. * The event type in standard mode.
  143. * Otherwise, never.
  144. */
  145. type FallbackEvent<TMode extends "standard" | "strict"> = TMode extends "standard" ? Event$1 : never;
  146. /**
  147. * Check if given event map is valid.
  148. * It's valid if the keys of the event map are narrower than `string`.
  149. */
  150. type IsValidEventMap<T> = string extends keyof T ? false : true;
  151. }
  152. /**
  153. * An implementation of `Event` interface, that wraps a given event object.
  154. * `EventTarget` shim can control the internal state of this `Event` objects.
  155. * @see https://dom.spec.whatwg.org/#event
  156. */
  157. declare class Event$1<TEventType extends string = string> {
  158. /**
  159. * @see https://dom.spec.whatwg.org/#dom-event-none
  160. */
  161. static get NONE(): number;
  162. /**
  163. * @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
  164. */
  165. static get CAPTURING_PHASE(): number;
  166. /**
  167. * @see https://dom.spec.whatwg.org/#dom-event-at_target
  168. */
  169. static get AT_TARGET(): number;
  170. /**
  171. * @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
  172. */
  173. static get BUBBLING_PHASE(): number;
  174. /**
  175. * Initialize this event instance.
  176. * @param type The type of this event.
  177. * @param eventInitDict Options to initialize.
  178. * @see https://dom.spec.whatwg.org/#dom-event-event
  179. */
  180. constructor(type: TEventType, eventInitDict?: Event$1.EventInit);
  181. /**
  182. * The type of this event.
  183. * @see https://dom.spec.whatwg.org/#dom-event-type
  184. */
  185. get type(): TEventType;
  186. /**
  187. * The event target of the current dispatching.
  188. * @see https://dom.spec.whatwg.org/#dom-event-target
  189. */
  190. get target(): EventTarget | null;
  191. /**
  192. * The event target of the current dispatching.
  193. * @deprecated Use the `target` property instead.
  194. * @see https://dom.spec.whatwg.org/#dom-event-srcelement
  195. */
  196. get srcElement(): EventTarget | null;
  197. /**
  198. * The event target of the current dispatching.
  199. * @see https://dom.spec.whatwg.org/#dom-event-currenttarget
  200. */
  201. get currentTarget(): EventTarget | null;
  202. /**
  203. * The event target of the current dispatching.
  204. * This doesn't support node tree.
  205. * @see https://dom.spec.whatwg.org/#dom-event-composedpath
  206. */
  207. composedPath(): EventTarget[];
  208. /**
  209. * @see https://dom.spec.whatwg.org/#dom-event-none
  210. */
  211. get NONE(): number;
  212. /**
  213. * @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
  214. */
  215. get CAPTURING_PHASE(): number;
  216. /**
  217. * @see https://dom.spec.whatwg.org/#dom-event-at_target
  218. */
  219. get AT_TARGET(): number;
  220. /**
  221. * @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
  222. */
  223. get BUBBLING_PHASE(): number;
  224. /**
  225. * The current event phase.
  226. * @see https://dom.spec.whatwg.org/#dom-event-eventphase
  227. */
  228. get eventPhase(): number;
  229. /**
  230. * Stop event bubbling.
  231. * Because this shim doesn't support node tree, this merely changes the `cancelBubble` property value.
  232. * @see https://dom.spec.whatwg.org/#dom-event-stoppropagation
  233. */
  234. stopPropagation(): void;
  235. /**
  236. * `true` if event bubbling was stopped.
  237. * @deprecated
  238. * @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
  239. */
  240. get cancelBubble(): boolean;
  241. /**
  242. * Stop event bubbling if `true` is set.
  243. * @deprecated Use the `stopPropagation()` method instead.
  244. * @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
  245. */
  246. set cancelBubble(value: boolean);
  247. /**
  248. * Stop event bubbling and subsequent event listener callings.
  249. * @see https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation
  250. */
  251. stopImmediatePropagation(): void;
  252. /**
  253. * `true` if this event will bubble.
  254. * @see https://dom.spec.whatwg.org/#dom-event-bubbles
  255. */
  256. get bubbles(): boolean;
  257. /**
  258. * `true` if this event can be canceled by the `preventDefault()` method.
  259. * @see https://dom.spec.whatwg.org/#dom-event-cancelable
  260. */
  261. get cancelable(): boolean;
  262. /**
  263. * `true` if the default behavior will act.
  264. * @deprecated Use the `defaultPrevented` proeprty instead.
  265. * @see https://dom.spec.whatwg.org/#dom-event-returnvalue
  266. */
  267. get returnValue(): boolean;
  268. /**
  269. * Cancel the default behavior if `false` is set.
  270. * @deprecated Use the `preventDefault()` method instead.
  271. * @see https://dom.spec.whatwg.org/#dom-event-returnvalue
  272. */
  273. set returnValue(value: boolean);
  274. /**
  275. * Cancel the default behavior.
  276. * @see https://dom.spec.whatwg.org/#dom-event-preventdefault
  277. */
  278. preventDefault(): void;
  279. /**
  280. * `true` if the default behavior was canceled.
  281. * @see https://dom.spec.whatwg.org/#dom-event-defaultprevented
  282. */
  283. get defaultPrevented(): boolean;
  284. /**
  285. * @see https://dom.spec.whatwg.org/#dom-event-composed
  286. */
  287. get composed(): boolean;
  288. /**
  289. * @see https://dom.spec.whatwg.org/#dom-event-istrusted
  290. */
  291. get isTrusted(): boolean;
  292. /**
  293. * @see https://dom.spec.whatwg.org/#dom-event-timestamp
  294. */
  295. get timeStamp(): number;
  296. /**
  297. * @deprecated Don't use this method. The constructor did initialization.
  298. */
  299. initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;
  300. }
  301. declare namespace Event$1 {
  302. /**
  303. * The options of the `Event` constructor.
  304. * @see https://dom.spec.whatwg.org/#dictdef-eventinit
  305. */
  306. interface EventInit {
  307. bubbles?: boolean;
  308. cancelable?: boolean;
  309. composed?: boolean;
  310. }
  311. }
  312. declare const EventTargetConstructor: typeof EventTarget
  313. declare const EventConstructor: typeof Event
  314. declare class FetchEvent {
  315. awaiting: Set<Promise<void>>
  316. constructor(request: Request)
  317. }
  318. export { EventConstructor as Event, EventTargetConstructor as EventTarget, FetchEvent, EventTarget as PromiseRejectionEvent };