TypeScript and Socket.io TypeScript and Socket.io node.js node.js

TypeScript and Socket.io


There is @types/socket.io now, just install it by running:

npm i --save @types/socket.io


You should use socket.io-client d.ts file in the client and while using socket.io d.ts file on the server.


I created my own .d.ts file, it's rather short but it works well:

declare var io : {    connect(url: string): Socket;};interface Socket {    on(event: string, callback: (data: any) => void );    emit(event: string, data: any);}

This declaration file can be imported to client side Typescript and the socket.io standard example will work, here's my Typescript version:

var socket=io.connect("localhost");socket.on("news",(data:any)=>alert(data));socket.emit("news","hello");