Skip to content
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

Fix #3833: Add fallback to untyped event listener #3834

Merged

Conversation

MaximeKjaer
Copy link
Contributor

@MaximeKjaer MaximeKjaer commented Mar 12, 2021

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

Current behavior

Due to a compiler bug, we had the following issue (see #3833):

import { Server } from "socket.io";

const io = new Server(3000, {});

// this works fine
io.on("connection", (socket) => {});

enum Event {
    CONNECTION = "connection",
}
// Fails to compile!
// Argument of type '(socket:Socket) => void' is not assignable to parameter of type 'never'.
// ts(2345)
io.on(Event.CONNECTION, (socket) => {});

New behavior

The above code now compiles. However, in the line io.on(Event.CONNECTION, (socket) => {});, the lambda parameter socket is inferred to be of type any instead of Socket if an enum member is used.

Note that this only affects the situation where an enum member is used: If a string literal is used, the correct type (Socket) is still inferred.

Also note that inferring any in this situation is what was already happening in 3.x. So we are essentially reverting to that, at least until the compiler bug is fixed.

Other information (e.g. related issues)

Fixes #3833

@westy92
Copy link
Contributor

westy92 commented Mar 30, 2021

@darrachequesne can you please take a look at this? This fixes a regression in 4.0.0 that is preventing me from updating.

@darrachequesne darrachequesne merged commit a11152f into socketio:master Mar 31, 2021
@westy92
Copy link
Contributor

westy92 commented Mar 31, 2021

Thank you! 🙏

@MaximeKjaer MaximeKjaer deleted the fix-enum-event-name-typings branch March 31, 2021 14:24
dzad pushed a commit to dzad/socket.io that referenced this pull request May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues with io.on() typings
3 participants