-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type annotations to events of WebSocketServer/Client #30
Comments
Looking at VS code's internals, they do something like this for HTML: interface DocumentEventMap extends GlobalEventHandlersEventMap, DocumentAndElementEventHandlersEventMap {
"fullscreenchange": Event;
"fullscreenerror": Event;
"pointerlockchange": Event;
"pointerlockerror": Event;
"readystatechange": Event;
"visibilitychange": Event;
}
/** The HTMLDocument property of Window objects is an alias that browsers expose for the Document interface object. */
interface HTMLDocument extends Document {
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: HTMLDocument, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: HTMLDocument, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
} For this class, which extends Deno's type WebSockerServerEventMap = {
'connection': (ws: WebSocketClient) => any,
'error': (error: Error) => any,
}
class WSServer extends EventEmitter {
on<K extends keyof WSServerEventMap>(eventName: K, listener: WSServerEventMap[K]): this;
on(eventName: string | symbol, listener: GenericFunction | WrappedFunction): this {
return super.on(eventName, listener);
}
once<K extends keyof WSServerEventMap>(eventName: K, listener: WSServerEventMap[K]): this;
once(eventName: string | symbol, listener: GenericFunction): this {
return super.once(eventName, listener);
}
addListener<K extends keyof WSServerEventMap>(eventName: K, listener: WSServerEventMap[K]): this;
addListener(eventName: string | symbol, listener: GenericFunction | WrappedFunction): this {
return super.addListener(eventName, listener);
}
} Not a huge fan of having to wrap the |
// copy from #31 // |
Waiting for update in std library, hopefully they will approve my changes :) |
Being able to having things like
connection
auto-complete would be very helpful. Eg.The text was updated successfully, but these errors were encountered: