I tried to create a collection of WebSocket instances in my node js application. The type of the class WebSocket cannot be found. If the class WebSocket is declared within the index.d.ts file in the namespace, the class can be used as desired.
const websockets: WebSocket[] = [];
version: 6.0.1
@loyd
@elithrar
@mlamp
@TitaneBoy
@orblazer
I just noticed this myself since I wanted to extends that particular class as my own Client class that still has the methods of the WebSocket itself. As of right now, you might as well use a custom fork - in the meantime I'll attempt to create a PR for that.
Just a minor update, even though locally it is possible to move the WebSocket class into the WebSocket namespace, this breaks tests unfortunately since both WebSocket classes in the native environment as well as the ws package will collide.
Please excuse my spam on this issue but I found a solution without the need to rewrite the typings. I don't know why I didn't notice it in the first place, but the default import of ws is the WebSocket class itself, while the rest is named (even though I dislike this structure personally).
So just import as follows:
import WebSocket, { Server } from "ws";
Most helpful comment
Please excuse my spam on this issue but I found a solution without the need to rewrite the typings. I don't know why I didn't notice it in the first place, but the default import of
wsis theWebSocketclass itself, while the rest is named (even though I dislike this structure personally).So just import as follows: