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

Calling io.emit("event") with events that has no arguments result in TypeScript errors #4914

Closed
lts20050703 opened this issue Jan 5, 2024 · 4 comments
Labels
bug Something isn't working
Milestone

Comments

@lts20050703
Copy link

lts20050703 commented Jan 5, 2024

Describe the bug
Calling io.emit("event") with events that either no argument result in TypeScript errors.

To Reproduce

Socket.IO server version: 4.7.3

Server

import { Server } from "socket.io";

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

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

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

Expected behavior
No errors

Actual behavior
Argument of type 'string' is not assignable to parameter of type 'never'. ts(2345)

Platform:

  • Device: Lenovo IdeaPad 5 14ITL05
  • OS: Windows 11 Home 23H2 (22631.2861)

Additional context
Seems to be a regression, after downgrading to 4.7.2 the error disappeared.

@lts20050703 lts20050703 added the to triage Waiting to be triaged by a member of the team label Jan 5, 2024
@lts20050703
Copy link
Author

This seems to be caused by f6ef267

@lts20050703 lts20050703 mentioned this issue Jan 5, 2024
5 tasks
@ZachHaber
Copy link
Contributor

@lts20050703 - from #4813 (comment)

I think most of the issues come from the fact that calling .timeout() is (at least currently) mandatory on the server side, since there is no default timeout value (the client has an ackTimeout option though).

So, for example:

// INVALID (will time out directly anyway)
io.emit('withAck', 'hello', (...args) => { });
// VALID
io.timeout(1).emit('withAck', 'hello', (...args) => { });

For emitting with ack from the server, it is correctly an error now, because it wouldn't do what you'd expect from the beginning and would timeout immediately as darrachequesne pointed out. Plus the type definition was misleading for it in 4.7.2 anyway. e would actually be a number[] rather than a number - if it was valid at all.

io.emit("withAck", "4", (e) => {}); // <- Error

I will however look into the no-callback one, since that should be valid.

@ZachHaber
Copy link
Contributor

@lts20050703, PR is up with the fix for the noArgs issue.

Until the fix is merged in and a new version is cut, you can use patch-package (or something similar) to update the dist/typed-events.d.ts file.

- export declare type EventNamesWithoutAck<Map extends EventsMap, K extends EventNames<Map> = EventNames<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? never : K) ? K : never>;
+ export declare type EventNamesWithoutAck<Map extends EventsMap, K extends EventNames<Map> = EventNames<Map>> = IfAny<Last<Parameters<Map[K]>> | Map[K], K, K extends (Parameters<Map[K]> extends never[] ? K : never) ? K : K extends (Last<Parameters<Map[K]>> extends (...args: any[]) => any ? never : K) ? K : never>;

I believe it would be line 32, but I don't have the current version installed anywhere to double check that line number (also, the line might not be exactly the same, but you are replacing the declaration of EventNamesWithoutAck).

@lts20050703 lts20050703 changed the title Calling io.emit("event") with events that either has no arguments or has acknowledgement callback result in TypeScript errors Calling io.emit("event") with events that has no arguments result in TypeScript errors Jan 7, 2024
@darrachequesne darrachequesne added bug Something isn't working and removed to triage Waiting to be triaged by a member of the team labels Jan 8, 2024
@darrachequesne
Copy link
Member

This should be fixed by cb6d2e0, included in version 4.7.4.

Could you please check?

@darrachequesne darrachequesne added this to the 4.7.4 milestone Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants