fix(eio-client/types): remove ws type from .d.ts file

Before this change, the following error would be thrown when compiling
with TypeScript:

```
node_modules/engine.io-client/build/esm/transports/websocket.node.d.ts:12:101 - error TS1340: Module 'ws' does not refer to a type, but is used as a type here. Did you mean 'typeof import('ws')'?

12     createSocket(uri: string, protocols: string | string[] | undefined, opts: Record<string, any>): import("ws");
                                                                                                       ~~~~~~~~~~~~
```

This behavior was introduced in [1], included in version `6.6.0`.

The return type is forced as `any`, so that the `@types/ws` dependency
is optional.

[1]: f4d898ee96

Related: https://github.com/socketio/socket.io/issues/5202
This commit is contained in:
Damien Arrachequesne
2024-10-19 06:52:08 +02:00
parent 9b80ab42d6
commit 175a2c58c1
8 changed files with 115 additions and 1 deletions

View File

@@ -19,6 +19,8 @@ jobs:
- custom-parsers
- typescript-example/cjs
- typescript-example/esm
- typescript-client-example/cjs
- typescript-client-example/esm
- webpack-build
- webpack-build-server
- basic-crud-application/angular-client

View File

@@ -0,0 +1,30 @@
import { io, type Socket } from "socket.io-client";
interface ServerToClientEvents {
hello: (val: string) => void;
}
interface ClientToServerEvents {
ping: (cb: () => void) => void;
}
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io("ws://localhost:8080/");
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});
socket.on("hello", (val) => {
console.log(`got ${val}`);
});
socket.on("disconnect", () => {
console.log(`disconnect`);
});
setInterval(() => {
const start = Date.now();
socket.emit("ping", () => {
console.log(`pong (latency: ${Date.now() - start} ms)`);
});
}, 1000);

View File

@@ -0,0 +1,17 @@
{
"name": "typescript-client-example-cjs",
"version": "0.0.1",
"description": "An example with TypeScript",
"type": "commonjs",
"private": true,
"scripts": {
"build": "tsc",
"start": "ts-node client.ts"
},
"license": "MIT",
"dependencies": {
"socket.io-client": "^4.8.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}

View File

@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true
}
}

View File

@@ -0,0 +1,30 @@
import { io, type Socket } from "socket.io-client";
interface ServerToClientEvents {
hello: (val: string) => void;
}
interface ClientToServerEvents {
ping: (cb: () => void) => void;
}
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io("ws://localhost:8080/");
socket.on("connect", () => {
console.log(`connect ${socket.id}`);
});
socket.on("hello", (val) => {
console.log(`got ${val}`);
});
socket.on("disconnect", () => {
console.log(`disconnect`);
});
setInterval(() => {
const start = Date.now();
socket.emit("ping", () => {
console.log(`pong (latency: ${Date.now() - start} ms)`);
});
}, 1000);

View File

@@ -0,0 +1,17 @@
{
"name": "typescript-client-example-esm",
"version": "0.0.1",
"description": "An example with TypeScript",
"type": "module",
"private": true,
"scripts": {
"build": "tsc",
"start": "node --no-warnings=ExperimentalWarning --loader ts-node/esm client.ts"
},
"license": "MIT",
"dependencies": {
"socket.io-client": "^4.8.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}

View File

@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"module": "esnext",
"moduleResolution": "node",
"strict": true
}
}

View File

@@ -15,7 +15,7 @@ export class WS extends BaseWS {
uri: string,
protocols: string | string[] | undefined,
opts: Record<string, any>,
) {
): any {
if (this.socket?._cookieJar) {
opts.headers = opts.headers || {};