Skip to content

Types #59

Closed
Closed
@imsergiobernal

Description

@imsergiobernal

Hello, I'm facing issues when trying to extend this base adapter for local memory use in a TS project.

Declaring a module,

declare module 'socket.io-adapter' {
  import * as Events from 'events';
  import * as SocketIO from 'socket.io';

  export default class Adapter extends Events.EventEmitter {
    constructor(nsp: SocketIO.Namespace);

    nsp: SocketIO.Namespace;

    rooms: SocketIO.Rooms

    sids: { [id: string]: { [room: string]: boolean } };

    encoder: any;

    public add(id: string, room: string, fn?: (err?: any) => void): void;
    public addAll(id: string, rooms: string[], fn?: (err?: any) => void): void;
    public del(id: string, room: string, fn?: (err?: any) => void): void;
    public delAll(id: string, rooms: string[], fn?: () => void): void;
    public broadcast(packet: any, opts: any): void;
    public clients(rooms: string[], fn: (this: this, error: null, sids: string[]) => void): void;
    public clientRooms(id: string, fn: (this: this, error: null, rooms: string[] | null) => void): void;
  }

  class Room {
    sockets: { [key: string]: boolean };
    length: number;
    add(id: string): void;
    del(id: string): void;
  }
}

extending it

import Adapter from 'socket.io-adapter';

export default class LocalAdapter extends Adapter {}

and trying to use it

import SocketIO from 'socket.io';
import LocalAdapter from './LocalAdapter';
...
io = SocketIO(server, { adapter: LocalAdapter });

results on

(alias) SocketIO(): SocketIO.Server (+3 overloads)
import SocketIO
Default Server constructor
No overload matches this call.
  Overload 1 of 4, '(srv: any, opts?: ServerOptions): Server', gave the following error.
    Type 'typeof LocalAdapter' is missing the following properties from type 'Adapter': nsp, rooms, sids, add, and 18 more.
  Overload 2 of 4, '(port: string | number, opts?: ServerOptions): Server', gave the following error.
    Argument of type 'Server' is not assignable to parameter of type 'string | number'.
      Type 'Server' is not assignable to type 'number'.ts(2769)
index.d.ts(314, 3): The expected type comes from property 'adapter' which is declared here on type 'ServerOptions'

Seems like class has no properties...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions