-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
46 lines (35 loc) · 2.11 KB
/
index.d.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
import type {ValueOf} from 'type-fest';
type EventListenerOrEventListenerObject<Value extends Event | CustomEvent> = (value: Value) => void | {
handleEvent(value: Value): void;
};
/**
Strictly typed definitions for [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
Only used for type-checking; compiles to no code.
@example
```
import type StrictEventTarget from 'strict-event-target';
const target = new (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>);
target.dispatchEvent(new CustomEvent('foo', {detail: 'The'}));
target.dispatchEvent(new Event('bar'));
```
@example
```
import type StrictEventTarget from 'strict-event-target';
class Foo extends (EventTarget as typeof StrictEventTarget<{
foo: string; // Event with data
}, [
'bar', // Event without data
]>) {}
```
*/
export default class StrictEventTarget<EventsWithData extends Record<string, unknown> = {}, EventsWithoutData extends string[] = []> implements EventTarget { // eslint-disable-line @typescript-eslint/ban-types
addEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
addEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
dispatchEvent(event: Event | CustomEvent<ValueOf<EventsWithData>>): boolean;
removeEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
removeEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types
}