diff --git a/src/index.ts b/src/index.ts index 17672aa..0e01956 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,17 +23,27 @@ export type EventHandlerMap> = Map< export interface Emitter> { all: EventHandlerMap; - on(type: Key, handler: Handler): void; - on(type: '*', handler: WildcardHandler): void; + on( + this: void, + type: Key, + handler: Handler + ): void; + on(this: void, type: '*', handler: WildcardHandler): void; off( + this: void, type: Key, handler?: Handler ): void; - off(type: '*', handler: WildcardHandler): void; + off(this: void, type: '*', handler: WildcardHandler): void; - emit(type: Key, event: Events[Key]): void; emit( + this: void, + type: Key, + event: Events[Key] + ): void; + emit( + this: void, type: undefined extends Events[Key] ? Key : never ): void; } @@ -63,7 +73,11 @@ export default function mitt>( * @param {Function} handler Function to call in response to given event * @memberOf mitt */ - on(type: Key, handler: GenericEventHandler) { + on( + this: void, + type: Key, + handler: GenericEventHandler + ) { const handlers: Array | undefined = all!.get(type); if (handlers) { handlers.push(handler); @@ -79,7 +93,11 @@ export default function mitt>( * @param {Function} [handler] Handler function to remove * @memberOf mitt */ - off(type: Key, handler?: GenericEventHandler) { + off( + this: void, + type: Key, + handler?: GenericEventHandler + ) { const handlers: Array | undefined = all!.get(type); if (handlers) { if (handler) { @@ -100,7 +118,7 @@ export default function mitt>( * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberOf mitt */ - emit(type: Key, evt?: Events[Key]) { + emit(this: void, type: Key, evt?: Events[Key]) { let handlers = all!.get(type); if (handlers) { (handlers as EventHandlerList)