Skip to content

Commit

Permalink
fix(eio-client/types): remove ws type from .d.ts file
Browse files Browse the repository at this point in the history
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]: f4d898e

Related: #5202
  • Loading branch information
darrachequesne committed Oct 19, 2024
1 parent 9b80ab4 commit 175a2c5
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions examples/typescript-client-example/cjs/client.ts
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions examples/typescript-client-example/cjs/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions examples/typescript-client-example/cjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true
}
}
30 changes: 30 additions & 0 deletions examples/typescript-client-example/esm/client.ts
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions examples/typescript-client-example/esm/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions examples/typescript-client-example/esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "es2022",
"module": "esnext",
"moduleResolution": "node",
"strict": true
}
}
2 changes: 1 addition & 1 deletion packages/engine.io-client/lib/transports/websocket.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};

Expand Down

0 comments on commit 175a2c5

Please sign in to comment.