Socket.io: typescript compile error

Created on 6 Nov 2020  ·  7Comments  ·  Source: socketio/socket.io

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

I have some troubles in compiling my code from typescript to javascript.
Compilation fails due to type error in namespace.d.ts and socket.d.ts。The Namespace and Socket class extends EventEmitter. However, they override 'emit' function and the signature is not compatible with the EventEmitter's emit function.

Is there any way to work around it?

Steps to reproduce (if the current behaviour is a bug)

Below is my tsconfig setting:

{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "./dist",
        "lib": ["es5", "es6"],
        "target": "es3",
        "module": "commonjs",
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "allowSyntheticDefaultImports": true,
    },
    "include": ["./src/**/*"],
}

Here is the dependencies of the project:

"dependencies": {
    "debug": "^3.1.0",
    "koa": "^2.13.0",
    "socket.io": "^3.0.0"
  },
  "devDependencies": {
    "@types/koa": "^2.11.6",
    "@types/node": "^14.14.6",
    "supervisor": "^0.12.0",
    "typescript": "^4.0.5"
  }

when I run: 'tsc', I got the following error:

node_modules/socket.io/dist/namespace.d.ts(89,5): error TS2416: Property 'emit' in type 'Namespace' is not assignable to the same property in base type 'EventEmitter'.
  Type '(ev: string, ...args: any[]) => Namespace' is not assignable to type '(event: string | symbol, ...args: any[]) => boolean'.
    Type 'Namespace' is not assignable to type 'boolean'.
node_modules/socket.io/dist/socket.d.ts(84,5): error TS2416: Property 'emit' in type 'Socket' is not assignable to the same property in base type 'EventEmitter'.
  Type '(ev: string, ...args: any[]) => this' is not assignable to type '(event: string | symbol, ...args: any[]) => boolean'.
    Type 'this' is not assignable to type 'boolean'.
      Type 'Socket' is not assignable to type 'boolean'.

Setup

  • OS: Windows
  • socket.io version: 3.0.0
bug

Most helpful comment

Can you release this version ASAP?

Or let us target fix/typescript-emit branch directly!

All 7 comments

Having the same problem here!

The same problem.
Debian, socket.io 3.0

Thanks, I could indeed reproduce the issue.

This should be fixed by https://github.com/socketio/socket.io/commit/50671d984a81535a6a15c704546ca7465e2ea295.

Can you release this version ASAP?

Or let us target fix/typescript-emit branch directly!

This seems worthy of a 3.0.1 release.

3.0.1 is out!

I've updated the TypeScript example here: https://github.com/socketio/socket.io/tree/master/examples/typescript

Thank you!

Was this page helpful?
0 / 5 - 0 ratings