Skip to content

Commit

Permalink
fix(typings): update the signature of the emit method
Browse files Browse the repository at this point in the history
The previous signature was not compatible with EventEmitter.emit(). The typescript compilation threw:

```
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'.
```

Note: the emit calls cannot be chained anymore:

```js
socket.emit("hello").emit("world"); // will not work anymore
```
  • Loading branch information
darrachequesne committed Nov 7, 2020
1 parent 8a69f15 commit 50671d9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import http from "http";
import http = require("http");
import { createReadStream } from "fs";
import { createDeflate, createGzip, createBrotliCompress } from "zlib";
import accepts = require("accepts");
import { pipeline } from "stream";
import path from "path";
import engine from "engine.io";
import path = require("path");
import engine = require("engine.io");
import { Client } from "./client";
import { EventEmitter } from "events";
import { ExtendedError, Namespace } from "./namespace";
Expand Down
7 changes: 3 additions & 4 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,10 @@ export class Namespace extends EventEmitter {
/**
* Emits to all clients.
*
* @return {Namespace} self
* @return {Boolean} Always true
* @public
*/
// @ts-ignore
public emit(ev: string, ...args: any[]): Namespace {
public emit(ev: string, ...args: any[]): boolean {
if (RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
Expand All @@ -209,7 +208,7 @@ export class Namespace extends EventEmitter {
flags: flags
});

return this;
return true;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/parent-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ParentNamespace extends Namespace {

_initAdapter() {}

public emit(...args): Namespace {
public emit(...args: any[]): boolean {
this.children.forEach(nsp => {
nsp._rooms = this._rooms;
nsp._flags = this._flags;
Expand All @@ -19,7 +19,7 @@ export class ParentNamespace extends Namespace {
this._rooms.clear();
this._flags = {};

return this;
return true;
}

createChild(name) {
Expand Down
9 changes: 4 additions & 5 deletions lib/socket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from "events";
import { PacketType } from "socket.io-parser";
import url from "url";
import url = require("url");
import debugModule from "debug";
import { Server } from "./index";
import { Client } from "./client";
Expand Down Expand Up @@ -129,11 +129,10 @@ export class Socket extends EventEmitter {
/**
* Emits to this client.
*
* @return {Socket} self
* @return {Boolean} Always true
* @public
*/
// @ts-ignore
public emit(ev: string, ...args: any[]) {
public emit(ev: string, ...args: any[]): boolean {
if (RESERVED_EVENTS.has(ev)) {
throw new Error(`"${ev}" is a reserved event name`);
}
Expand Down Expand Up @@ -171,7 +170,7 @@ export class Socket extends EventEmitter {
// dispatch packet
this.packet(packet, flags);
}
return this;
return true;
}

/**
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"outDir": "./dist",
"allowJs": true,
"target": "es2017",
"module": "commonjs",
"declaration": true,
Expand Down

0 comments on commit 50671d9

Please sign in to comment.