-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
30 lines (24 loc) · 1.34 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export interface TypedEventListener<E extends Event> {
(evt: E): void;
}
export interface TypedEventListenerObject<E extends Event> {
handleEvent(object: E): void;
}
export type TypedEventListenerOrEventListenerObject<E extends Event> = TypedEventListener<E> | TypedEventListenerObject<E>;
type StringKeyOf<T> = keyof T extends string ? keyof T : never;
export interface TypedEventTarget<
EventMap extends Record<K, Event> = Record<string, Event>,
K extends string = StringKeyOf<EventMap>
> extends EventTarget {
addEventListener<K extends keyof EventMap>(type: K, listener: TypedEventListenerOrEventListenerObject<EventMap[K]>|null, options?: boolean|AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject|null, options?: boolean|AddEventListenerOptions): void;
removeEventListener<K extends keyof EventMap>(type: K, listener: TypedEventListenerOrEventListenerObject<EventMap[K]>|null, options?: boolean|EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject|null, options?: boolean|EventListenerOptions): void;
};
export const TypedEventTarget: {
prototype: EventTarget;
new<
EventMap extends Record<K, Event> = Record<string, Event>,
K extends string = StringKeyOf<EventMap>
>(): TypedEventTarget<EventMap, K>;
} = EventTarget;