Skip to content

Fix: Calling io.emit("event") with events that have no arguments results in TypeScript errors #4915

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Jan 8, 2024

Conversation

ZachHaber
Copy link
Contributor

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Typescript type fixe relating to #4817, also type failures in the tests due to the client#id type becoming string|undefined (f9c16f2)

Current behavior

import { Server } from "socket.io";

interface ServerToClientEvents {
  noArg: () => void;
}

const io = new Server<{}, ServerToClientEvents>();

io.emit("noArg"); // <- Error

New behavior

import { Server } from "socket.io";

interface ServerToClientEvents {
  noArg: () => void;
}

const io = new Server<{}, ServerToClientEvents>();

io.emit("noArg"); // ✔️ - no error!

Other information (e.g. related issues)

#4914

@ZachHaber ZachHaber changed the title Typescript no arg bug Fix: Calling io.emit("event") with events that have no arguments results in TypeScript errors Jan 5, 2024
@lts20050703
Copy link

Hmm does this also fix #4917?

@ZachHaber
Copy link
Contributor Author

Yes, they all use the same utility type to set up which events are allowed. I just added in a test I was missing for a namespace emit with noArgs.

@darrachequesne darrachequesne merged commit d9fb2f6 into socketio:main Jan 8, 2024
@darrachequesne
Copy link
Member

@ZachHaber thanks 👍

@ZachHaber ZachHaber deleted the typescript-no-arg-bug branch January 8, 2024 11:12
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants