-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Server emitWithAck on typed Server has "never" typing #5257
Comments
I have exactly the same problem |
I think this is intentional, because:
|
If it's intentional, it's unclear why you wouldn't be able to use a callback signature of |
Since both |
I don't see how they're unclear: For the latter, what's more indicative that it's a bug is that |
// example
import { Server } from "socket.io"
interface ServerToClientEvents {
test: (cb: (result: number) => void) => void
}
const io = new Server<{}, ServerToClientEvents>()
io.on("connection", socket => {
const ackResult = socket.emitWithAck("test")
// ackResult: Promise<number>
const emitResult = socket.emit("test", () => {})
// emitResult: boolean (irrelevant to the definition of events)
}) But, if there is no return type specified, what should the function return? Of course we could return |
I understand what you're saying from the "how it's implemented" perspective, but I'm looking at it from the "what would a user expect" perspective. This is a perfectly reasonable thing to want to do and expect it will work: import { Server } from "socket.io"
interface ServerToClientEvents {
test: (cb: () => void) => void
}
const io = new Server<{}, ServerToClientEvents>()
io.on("connection", async socket => {
try {
await socket.emitWithAck("test")
catch (socketErr: any) {
console.error(socketErr)
}
}) Especially since it's doable in a non-async way: import { Server } from "socket.io"
interface ServerToClientEvents {
test: (cb: () => void) => void
}
const io = new Server<{}, ServerToClientEvents>()
io.on("connection", socket => {
await socket.emit("test", (socketErr) => {
console.error(socketErr)
})
}) |
(This comment is not intended to defend the current behavior) interface ServerToClientEvents {
test: (cb: (result: void) => void) => void
}
// server side
socket.emit("test", () => {})
// client side
socket.on("test", cb => {
cb() // ok
}) |
Describe the bug
emitWithAck
doesn't seem to be picking up types correctly, so the type variableEv
is assignednever
.To Reproduce
Socket.IO server version:
4.8.1
Server
Client
No client code necessary.
Expected behavior
No Typescript error (similar to
emit
behavior)Platform:
Additional context
Seems to be the same bug as mentioned in the comment #4925 (comment), but different from that issue.
The text was updated successfully, but these errors were encountered: