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

[Typescript] Socket from socket.io cannot be unioned with Socket from socket.io-client #3872

Closed
evanpurkhiser opened this issue Apr 12, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@evanpurkhiser
Copy link

evanpurkhiser commented Apr 12, 2021

Describe the bug

In 3.x of socket.io I could do the following:

import {Socket as ServerSocket} from 'socket.io';
import {Socket as ClientSocket} from 'socket.io-client';

function doStuff(ws: ClientSocket | ServerSocket) => {
	ws.on('some-event', () => {
		// ...
	});
}

In 4.x this now produces an error on the ws.on call:

[tsserver 2349] [E] This expression is not callable.
  Each member of the union type '(<Ev extends string>(ev: Ev, listener: ReservedOrUserListener<SocketReservedEvents, DefaultEventsMap, Ev>) => Socket<...>) | (<Ev extends string>(ev: Ev, listener: FallbackToUntypedListener<...>) => Socket<...>)' has signatures, but none of those signatures are compatible with each other.

To Reproduce

Socket.IO server / client version: 4.0.0

See above

Expected behavior
I realize this was a major version bump between 3 and 4, so I can understand if this issue needs to be closed.

This also might be more of a typescript problem than anything else. I know the templated emitter feature was added in 4 (which I am looking forward to using once I get around this!) which I assume is what has made the method signatures incompatible?

I noticed I can "fix" this by doing

const doStuff = (ws: ClientSocket | ServerSocket) => {
  if (ws instanceof ClientSocket) {
    ws.on('some-event', () => {
      // ...
    });
  }

  if (ws instanceof ServerSocket) {
    ws.on('some-event', () => {
      // ...
    });
  }
};

which is pretty goofy.

Additional context
I am on Typescript 4.0.2

@evanpurkhiser evanpurkhiser added the bug Something isn't working label Apr 12, 2021
@darrachequesne
Copy link
Member

I could indeed reproduce the issue. Looks like it's related to 0107510, but I'm not sure how we can fix this.

@darrachequesne
Copy link
Member

For future readers:

It seems this was fixed in TypeScript between 4.1 and 4.2:

$ npm i [email protected]

changed 1 package, and audited 86 packages in 904ms

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

$ npx tsc
test.ts:5:8 - error TS2349: This expression is not callable.
  Each member of the union type '(<Ev extends string>(ev: Ev, listener: FallbackToUntypedListener<Ev extends "connect" | "connect_error" | "disconnect" ? SocketReservedEvents[Ev] : Ev extends string ? (...args: any[]) => void : never>) => Socket<...>) | (<Ev extends string>(ev: Ev, listener: FallbackToUntypedListener<...>) => Socket<...>)' has signatures, but none of those signatures are compatible with each other.

5     ws.on('some-event', () => {
         ~~


Found 1 error.

$ npm i [email protected]

changed 1 package, and audited 86 packages in 924ms

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

$ npx tsc
$

Please reopen if needed!

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

2 participants