Skip to content

Typescript type issues #5368

@turnerhayes

Description

@turnerhayes

Describe the bug

Typescript types are not working correctly, specifically for event listeners. on() and related methods don't seem to accept the callbacks.

For example, I have a client class that I want to expose passthrough methods for event listeners. I defined the passthrough method as

    listen<E extends keyof ServerToClientEvents>(eventName: E, callback: ServerToClientEvents[E]) {
        this.socket.on(eventName, callback);
    }

It yields this error:

TS2345: Argument of type 'ServerToClientEvents[E]' is not assignable to parameter of type 'FallbackToUntypedListener<E extends "connect" | "disconnect" | "connect_error" ? SocketReservedEvents[E] : E extends "server-to-client-1" ? ServerToClientEvents[E] : never>'.
  Type '(arg: string[]) => void' is not assignable to type 'FallbackToUntypedListener<E extends "connect" | "disconnect" | "connect_error" ? SocketReservedEvents[E] : E extends "server-to-client-1" ? ServerToClientEvents[E] : never>'.

#5367 is also demonstrated in this repo.

To Reproduce

Reproduction repo: https://github.com/turnerhayes/socket-io-type-confusion

Metadata

Metadata

Assignees

No one assigned

    Labels

    to triageWaiting to be triaged by a member of the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions