/** * @packageDocumentation * * Adds types to the EventTarget class. * * Hopefully this won't be necessary * forever: * * - https://github.com/microsoft/TypeScript/issues/28357 * - https://github.com/microsoft/TypeScript/issues/43477 * - https://github.com/microsoft/TypeScript/issues/299 * - https://www.npmjs.com/package/typed-events * - https://www.npmjs.com/package/typed-event-emitter * - https://www.npmjs.com/package/typed-event-target * - etc * * In addition to types, a `safeDispatchEvent` method is available which * prevents dispatching events that aren't in the event map, and a * `listenerCount` method which reports the number of listeners that are * currently registered for a given event. * * @example * * ```ts * import { TypedEventEmitter } from 'main-event' * import type { TypedEventTarget } from 'main-event' * * interface EventTypes { * 'test': CustomEvent * } * * const target = new TypedEventEmitter() * * // it's a regular EventTarget * console.info(target instanceof EventTarget) // true * * // register listeners normally * target.addEventListener('test', (evt) => { * // evt is CustomEvent * }) * * // @ts-expect-error 'derp' is not in the event map * target.addEventListener('derp', () => {}) * * // use normal dispatchEvent method * target.dispatchEvent(new CustomEvent('test', { * detail: 'hello' * })) * * // use type safe dispatch method * target.safeDispatchEvent('test', { * detail: 'world' * }) * * // report listener count * console.info(target.listenerCount('test')) // 0 * * // event emitters can be used purely as interfaces too * function acceptTarget (target: TypedEventTarget) { * // ... * } * ``` */ import { setMaxListeners } from './events.ts'; export interface EventCallback { (evt: EventType): void; } export interface EventObject { handleEvent: EventCallback; } export type EventHandler = EventCallback | EventObject; /** * An event target with a typed map of supported events */ export interface TypedEventTarget> extends EventTarget { /** * Add a listener for an event */ addEventListener(type: K, listener: EventHandler | null, options?: boolean | AddEventListenerOptions): void; /** * Returns the number of listeners for the event type */ listenerCount(type: string): number; /** * Remove a listener previously registered for an event */ removeEventListener(type: K, listener?: EventHandler | null, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener?: EventHandler, options?: boolean | EventListenerOptions): void; /** * Type-safe version of `EventTarget.dispatchEvent` - only events from the event map are supported. * * Attempting to dispatch an unsupported event results in a TypeScript compilation error. */ safeDispatchEvent(type: keyof EventMap, detail?: CustomEventInit): boolean; } /** * An implementation of a typed event target */ export declare class TypedEventEmitter> extends EventTarget implements TypedEventTarget { #private; constructor(); listenerCount(type: string): number; addEventListener(type: K, listener: EventHandler | null, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener?: EventHandler | null, options?: boolean | EventListenerOptions): void; safeDispatchEvent(type: keyof EventMap, detail?: CustomEventInit): boolean; } export { setMaxListeners }; //# sourceMappingURL=index.d.ts.map