Skip to content

Manually add types to socket.data #4155

@T-Damer

Description

@T-Damer

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

I was working with socket.data and noticed that I couldn't define its types. I tried to search the same issue here, but didn't find

Describe the solution you'd like

  • For example I want to do socket.data<User>
  • or maybe pass it when server is being created:
new Server<{user: User}>(server, { cors: { origin: * }, })
  • Or maybe inherit types from usage socket.data = user then socket.data becomes socket.data: {User, ...any} or something like this

Describe alternatives you've considered

Right now I can create a subclass to use it:

interface SocketDataType = { user: User }

export declare class ExtendedSocketServer<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, SocketDataType = any> extends Server<ListenEvents, EmitEvents, ServerSideEvents> {
  of(name: string | RegExp | ParentNspNameMatchFn, fn?: (socket: ExtendedSocketServer<ListenEvents, EmitEvents, ServerSideEvents, SocketDataType>) => void): Namespace<ListenEvents, EmitEvents, ServerSideEvents>;
}

Additional context

Also, I can use the patch-package. I just modified the original node_modules/socket.io/dist/socket.d.ts and run yarn patch-package

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions