Skip to content

Commit

Permalink
fix: support Type generic in StrictMessageEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Dec 20, 2022
1 parent cb4360b commit 667708a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"author": "Artem Zakharchenko <[email protected]>",
"license": "MIT",
"scripts": {
"dev": "tsc -w",
"test": "jest",
"test:ts": "tsc -p test/typings.tsconfig.json",
"clean": "rimraf ./lib",
Expand All @@ -31,4 +32,4 @@
"dependencies": {
"events": "^3.3.0"
}
}
}
20 changes: 11 additions & 9 deletions src/StrictEventTarget.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { EventMapType } from './StrictEventEmitter'

export type StrictListener<Data> = (data: Data) => void
export type StrictListener<Type extends string, Data> = (
message: StrictMessageEvent<Type, Data>
) => void

/**
* A type-safe implementation of the `EventTarget` interface.
*/
export class StrictEventTarget<
EventMap extends EventMapType
> extends EventTarget {
public dispatchEvent<Event extends keyof EventMap>(
event: StrictMessageEvent<Event & string, EventMap[Event]>
public dispatchEvent<Event extends keyof EventMap & string>(
event: StrictMessageEvent<Event, EventMap[Event]>
): boolean {
return super.dispatchEvent(event)
}

public addEventListener<Type extends keyof EventMap>(
type: Type & string,
listener: StrictListener<EventMap[Type]>,
type: Type,
listener: StrictListener<Type & string, EventMap[Type]>,
options?: boolean | AddEventListenerOptions
): void {
return super.addEventListener(type, listener, options)
return super.addEventListener(type as string, listener, options)
}

public removeEventListener<Type extends keyof EventMap>(
type: Type & string,
listener: StrictListener<EventMap[Type]>,
type: Type,
listener: StrictListener<Type & string, EventMap[Type]>,
options?: boolean | EventListenerOptions
): void {
return super.removeEventListener(type, listener, options)
return super.removeEventListener(type as string, listener, options)
}
}

Expand Down

0 comments on commit 667708a

Please sign in to comment.