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

Expose DisconnectReason as part of public API #4556

Open
nathanjcochran opened this issue Dec 12, 2022 · 0 comments
Open

Expose DisconnectReason as part of public API #4556

nathanjcochran opened this issue Dec 12, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@nathanjcochran
Copy link

nathanjcochran commented Dec 12, 2022

Is your feature request related to a problem? Please describe.

When you define a listener function for the socket disconnect event in-line, TypeScript correctly infers the type of the reason argument to be DisconnectReason:

socket.on('disconnect', (reason) => {
  // Type of  'reason' is 'DisconnectReason'
  console.log(`reason: ${reason}`);
});

However, I want to create a listener function for the socket.on('disconnect') event ahead of time (so that I can later call removeListener to remove it). But when I do that, the compiler cannot infer that the type of reason is DisconnectReason:

const listener = (reason) => {
  // Type of 'reason' is 'any'
  console.log(`reason: ${reason}`);
};
socket.on('disconnect', listener);

// Later...
socket.removeListener('disconnect', listener);

I can import DisconnectReason from socket.io/dist/socket and create my listener with reason: DisconnectReason, but it would be ideal if DisconnectReason was exposed as part of the public API, so that I could import it from socket.io directly.

Describe the solution you'd like

I would like to be able to import DisconnectReason from socket.io directly, instead of from socket.io/dist/socket, like so:

import { DisconnectReason } from 'socket.io';

Describe alternatives you've considered

I considered trying to determine the type of the listener function via some clever TypeScript, like so:

const listener: Parameters<typeof socket.on<'disconnect'>>[1] = (reason) => {
  // Type of 'reason' is 'DisconnectReason'
  console.log(`reason: ${reason}`);
};

That kind of worked, but I was running into eslint/prettier errors about the syntax, and I don't actually think it's more readable/clear than just using DisconnectReason directly:

const listener = (reason: DisconnectReason) => {
  // Type of 'reason' is 'DisconnectReason'
  console.log(`reason: ${reason}`);
};

Thanks!

@nathanjcochran nathanjcochran added the enhancement New feature or request label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant