javascript - how to initial the socket.io-client's socket - Stack Overflow

admin2025-04-15  0

I have defined the WsParam in my project file ws_param.ts like this:

import { Socket } from "socket.io-client";
export type WsParam = { 
    new (url: string): Socket;
}

now I want to initial the WsParam, then write the code ws_param_init.ts like this:

import { Socket, io } from "socket.io-client";
import { WsParam } from "./ws_param";

export const AASocket: WsParam = {
  new(url: string): Socket {
    return io(url);
  }, 
};

but the code shows error Object literal may only specify known properties, and 'new' does not exist in type 'WsParam'.ts(2353). the socket.io version is "socket.io": "^4.8.1". Am I missing something? what should I do to init the WsParam? I also tried like this:

import { io } from "socket.io-client";
import { WsParam } from "./ws_param";

export class AASocket implements WsParam {
  constructor(url: string) {
    return io(url);
  }
}

shows error:

Class 'AASocket' incorrectly implements interface 'WsParam'.
  Type 'AASocket' provides no match for the signature 'new (url: string): Socket<DefaultEventsMap, DefaultEventsMap>'.ts(2420)
转载请注明原文地址:http://www.anycun.com/QandA/1744718903a86665.html