Skip to content

Commit 50671d9

Browse files
fix(typings): update the signature of the emit method
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 ```
1 parent 8a69f15 commit 50671d9

File tree

5 files changed

+12
-15
lines changed

5 files changed

+12
-15
lines changed

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import http from "http";
1+
import http = require("http");
22
import { createReadStream } from "fs";
33
import { createDeflate, createGzip, createBrotliCompress } from "zlib";
44
import accepts = require("accepts");
55
import { pipeline } from "stream";
6-
import path from "path";
7-
import engine from "engine.io";
6+
import path = require("path");
7+
import engine = require("engine.io");
88
import { Client } from "./client";
99
import { EventEmitter } from "events";
1010
import { ExtendedError, Namespace } from "./namespace";

lib/namespace.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,10 @@ export class Namespace extends EventEmitter {
178178
/**
179179
* Emits to all clients.
180180
*
181-
* @return {Namespace} self
181+
* @return {Boolean} Always true
182182
* @public
183183
*/
184-
// @ts-ignore
185-
public emit(ev: string, ...args: any[]): Namespace {
184+
public emit(ev: string, ...args: any[]): boolean {
186185
if (RESERVED_EVENTS.has(ev)) {
187186
throw new Error(`"${ev}" is a reserved event name`);
188187
}
@@ -209,7 +208,7 @@ export class Namespace extends EventEmitter {
209208
flags: flags
210209
});
211210

212-
return this;
211+
return true;
213212
}
214213

215214
/**

lib/parent-namespace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class ParentNamespace extends Namespace {
1010

1111
_initAdapter() {}
1212

13-
public emit(...args): Namespace {
13+
public emit(...args: any[]): boolean {
1414
this.children.forEach(nsp => {
1515
nsp._rooms = this._rooms;
1616
nsp._flags = this._flags;
@@ -19,7 +19,7 @@ export class ParentNamespace extends Namespace {
1919
this._rooms.clear();
2020
this._flags = {};
2121

22-
return this;
22+
return true;
2323
}
2424

2525
createChild(name) {

lib/socket.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from "events";
22
import { PacketType } from "socket.io-parser";
3-
import url from "url";
3+
import url = require("url");
44
import debugModule from "debug";
55
import { Server } from "./index";
66
import { Client } from "./client";
@@ -129,11 +129,10 @@ export class Socket extends EventEmitter {
129129
/**
130130
* Emits to this client.
131131
*
132-
* @return {Socket} self
132+
* @return {Boolean} Always true
133133
* @public
134134
*/
135-
// @ts-ignore
136-
public emit(ev: string, ...args: any[]) {
135+
public emit(ev: string, ...args: any[]): boolean {
137136
if (RESERVED_EVENTS.has(ev)) {
138137
throw new Error(`"${ev}" is a reserved event name`);
139138
}
@@ -171,7 +170,7 @@ export class Socket extends EventEmitter {
171170
// dispatch packet
172171
this.packet(packet, flags);
173172
}
174-
return this;
173+
return true;
175174
}
176175

177176
/**

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"compilerOptions": {
33
"outDir": "./dist",
4-
"allowJs": true,
54
"target": "es2017",
65
"module": "commonjs",
76
"declaration": true,

0 commit comments

Comments
 (0)