-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve type-safety for events
- Loading branch information
Showing
5 changed files
with
156 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export interface TypedEventTarget<TEventMap extends Record<string, unknown>> | ||
extends | ||
Omit< | ||
EventTarget, | ||
"addEventListener" | "removeEventListener" | "dispatchEvent" | ||
> { | ||
addEventListener<K extends keyof TEventMap>( | ||
type: K, | ||
callback: ( | ||
event: CustomEvent<TEventMap[K]>, | ||
) => void, | ||
options?: AddEventListenerOptions | boolean, | ||
): void; | ||
|
||
removeEventListener<K extends keyof TEventMap>( | ||
type: K, | ||
callback: ( | ||
event: CustomEvent<TEventMap[K]>, | ||
) => void, | ||
options?: EventListenerOptions | boolean, | ||
): void; | ||
} | ||
|
||
export type ConnectionEvent = Record<string, unknown>; | ||
|
||
export type ConnectionErrorEventDetails = { | ||
error: unknown; | ||
}; | ||
|
||
export type ConnectionReconnectingEventDetails = { | ||
delay: number; | ||
}; | ||
|
||
export type ConnectionEventMap = { | ||
error: ConnectionErrorEventDetails; | ||
connect: unknown; | ||
reconnecting: ConnectionReconnectingEventDetails; | ||
ready: unknown; | ||
close: unknown; | ||
end: unknown; | ||
}; | ||
|
||
export type ConnectionEventType = | ||
| "error" | ||
| "connect" | ||
| "reconnecting" | ||
| "ready" | ||
| "close" | ||
| "end"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.