-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (49 loc) · 2.16 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type BottomEventMap = Record<string, any>;
export interface STCustomEventInit<T> extends CustomEventInit<T> {
detail: T;
}
export interface STCustomEvent<EventMap extends BottomEventMap, EventName extends keyof EventMap = keyof EventMap>
extends CustomEvent<EventMap[EventName]> {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new <T extends keyof EventMap>(
typeArg: T,
// ref: https://stackoverflow.com/a/52318137
...ConditionalOptionalArgs: EventMap[T] extends undefined
? [STCustomEventInit<EventMap[T]>?]
: [STCustomEventInit<EventMap[T]>]
): STCustomEvent<EventMap, T>;
}
export interface STEventListener<EventMap extends BottomEventMap, EventName extends keyof EventMap = keyof EventMap> {
(evt: STCustomEvent<EventMap, EventName>): void;
}
export interface STEventListenerObject<
EventMap extends BottomEventMap,
EventName extends keyof EventMap = keyof EventMap
> {
handleEvent(evt: STCustomEvent<EventMap, EventName>): void;
}
export type STEventListenerOrEventListenerObject<
EventMap extends BottomEventMap,
EventName extends keyof EventMap = keyof EventMap
> = STEventListener<EventMap, EventName> | STEventListenerObject<EventMap, EventName>;
export interface STEventTarget<EventMap extends BottomEventMap> {
// eslint-disable-next-line @typescript-eslint/no-misused-new
new (): STEventTarget<EventMap>;
addEventListener<T extends keyof EventMap>(
type: T,
listener: STEventListenerOrEventListenerObject<EventMap, T> | null,
options?: boolean | AddEventListenerOptions,
): void;
dispatchEvent<CustomEvent extends STCustomEvent<EventMap, keyof EventMap>>(event: CustomEvent): boolean;
removeEventListener<T extends keyof EventMap>(
type: T,
callback: STEventListenerOrEventListenerObject<EventMap, T> | null,
options?: EventListenerOptions | boolean,
): void;
}
export function createSTEventTarget<EventMap extends BottomEventMap>() {
const STCustomEvent = (CustomEvent as unknown) as STCustomEvent<EventMap>;
const STEventTarget = EventTarget as STEventTarget<EventMap>;
return [STCustomEvent, STEventTarget] as const;
}