socket.io
socket.io copied to clipboard
Typescript is wrongly inferring types and showing errors
Describe the bug
After defining types for the socket instance, the compiler is inferring the arguments wrongly in CLI lib
To Reproduce
Please fill the following code example:
Socket.IO server version: 4.8.1
Server
import { Server } from "socket.io";
const io = new Server(3000, {});
io.on("connection", (socket) => {
console.log(`connect ${socket.id}`);
socket.on("disconnect", () => {
console.log(`disconnect ${socket.id}`);
});
});
Socket.IO client version: 4.8.1
Client
import { io,Socket } from "socket.io-client";
interface CliToServ{
frdRequest:(user:{userId:string}) =>void;
}
interface ServToCli{
hello:() =>void;
}
const socket:Socket<ServToCli, CliToServ>= io("ws://localhost:3000/", {});
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});
socket.on("disconnect", () => {
console.log("disconnect");
});
//this is where it falsely shows the err
try{
const res=await socket.emitWithAck("frdRequest", {userId:"1234"})
} catch{
//err handling here
}
//it says I passed 2 arguments and it
//expected 1
//To resolve this I had to pass a second
//unwanted prop to the interface ie
interface CliToServ{
frdRequest:(user:{userId:string}, x:unknown) =>void;
}
//with the above the err doesn't show /
//and still it doesn't infer that useless
//prop
Expected behavior With valid type definition it had not to make any warnings, and there was no need for me to add an unwanted prop
Platform:
- Device: [Dell Latitude]
- OS: [windows 10]
Additional context
That unwanted prop added it's also not inferred on emittion