-
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
Issues with io.on() typings #3833
Comments
I'm able to replicate this, and I think I've found the root cause of the error. We use TypeScript conditional types internally to determine whether to get the listener type from a reserved events map, or from a user-provided events map. However, it seems that conditional types have some very weird behavior around enums. I've been able to minimize the issue to the following: enum Events {
CONNECTION = "connection",
TEST = "test",
}
type Names = "test" | "connection";
type NameOrNull<N> = N extends Names ? N : null;
// Sanity checks:
type test1 = NameOrNull<"test">; // returns type `"test"`
type test2 = NameOrNull<"not-a-name">; // returns type `null`
type test3 = Events.TEST extends Names ? Events.TEST : null // returns type `Events.TEST`
// Weird behavior:
type test4 = NameOrNull<Events.TEST>; // returns type `never` (???) I would expect Until we find a solution to this, there are two possible ways of dealing with this problem:
I realize that these are unfortunately less than ideal solutions |
This seems to be linked to microsoft/TypeScript#41778. I'll add the above bug reproduction to that issue. In the meantime, I've submitted a workaround in #3834. |
It appears that this issue is fixed in Typescript 4.9 without the fallback. |
Describe the bug
After upgrading to socket.io 4.0.0, my code no longer compiles when using string enums for event names.
To Reproduce
Please fill the following code example:
Socket.IO server version:
4.0.0
[email protected]
Server
Client
N/A
Expected behavior
I expect my code to compile like it does for version 2.x and 3.x.
Platform:
N/A
The text was updated successfully, but these errors were encountered: