I'm trying to extend a class (from an external websocket library) with a custom function by adding my function to the prototype. But i'm get the Property 'customFuction' does not exist on type 'WebSocket' error which is understandable.
import WebSocket from "ws";
WebSocket.prototype.customFunction = function customFunction(): void {
^^^^^^^^^^^^^^
"Property 'customFuction' does not exist on type 'WebSocket'
};
But my question is how do I add my function to the existing class definition?
I don't want to create a new type (like MyWebSocket extends WebSocket) because I want the method to be available on the WebSocket class.
Edit: I also tried the solution below suggested by @Balastrong but this still doesn't work. I'm also using the @types/ws package for the type definitions but I don't think module augmentation updates those definitions, could this be the reason for why this solution isn't working?
// WebSocketExtensions.ts
import WebSocket from "ws";
declare module "ws" {
export interface WebbSocket {
customFunction(): void;
}
}
WebSocket.prototype.customFunction = () => console.log('Hey');
WebSocketis the default export of thewsmodule. Your augmented module declares an additional named export.exportkeyword (and fix the misspelling), so thatinterface WebSocketextends the declaration from github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/ws/…