From 50671d984a81535a6a15c704546ca7465e2ea295 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sun, 8 Nov 2020 00:00:43 +0100 Subject: [PATCH] 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 ``` --- lib/index.ts | 6 +++--- lib/namespace.ts | 7 +++---- lib/parent-namespace.ts | 4 ++-- lib/socket.ts | 9 ++++----- tsconfig.json | 1 - 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 981fef9ade..c74d065f59 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"; diff --git a/lib/namespace.ts b/lib/namespace.ts index e85c0cd019..f445cff7a9 100644 --- a/lib/namespace.ts +++ b/lib/namespace.ts @@ -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`); } @@ -209,7 +208,7 @@ export class Namespace extends EventEmitter { flags: flags }); - return this; + return true; } /** diff --git a/lib/parent-namespace.ts b/lib/parent-namespace.ts index b447b934eb..88d07df80a 100644 --- a/lib/parent-namespace.ts +++ b/lib/parent-namespace.ts @@ -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; @@ -19,7 +19,7 @@ export class ParentNamespace extends Namespace { this._rooms.clear(); this._flags = {}; - return this; + return true; } createChild(name) { diff --git a/lib/socket.ts b/lib/socket.ts index b06837d77b..57ef148874 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -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"; @@ -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`); } @@ -171,7 +170,7 @@ export class Socket extends EventEmitter { // dispatch packet this.packet(packet, flags); } - return this; + return true; } /** diff --git a/tsconfig.json b/tsconfig.json index 68e03b91c1..c21970279b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "compilerOptions": { "outDir": "./dist", - "allowJs": true, "target": "es2017", "module": "commonjs", "declaration": true,