Skip to content

Commit 9e8f288

Browse files
david-fongdarrachequesne
authored andcommitted
fix(typings): add return types and general-case overload signatures (#3776)
See also: https://stackoverflow.com/questions/52760509/typescript-returntype-of-overloaded-function/52760599#52760599
1 parent 86eb422 commit 9e8f288

File tree

4 files changed

+94
-76
lines changed

4 files changed

+94
-76
lines changed

lib/client.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class Client {
7777
* @param {Object} auth - the auth parameters
7878
* @private
7979
*/
80-
private connect(name: string, auth: object = {}) {
80+
private connect(name: string, auth: object = {}): void {
8181
if (this.server._nsps.has(name)) {
8282
debug("connecting to namespace %s", name);
8383
return this.doConnect(name, auth);
@@ -112,7 +112,7 @@ export class Client {
112112
*
113113
* @private
114114
*/
115-
private doConnect(name: string, auth: object) {
115+
private doConnect(name: string, auth: object): void {
116116
const nsp = this.server.of(name);
117117

118118
const socket = nsp._add(this, auth, () => {
@@ -131,7 +131,7 @@ export class Client {
131131
*
132132
* @private
133133
*/
134-
_disconnect() {
134+
_disconnect(): void {
135135
for (const socket of this.sockets.values()) {
136136
socket.disconnect();
137137
}
@@ -144,7 +144,7 @@ export class Client {
144144
*
145145
* @private
146146
*/
147-
_remove(socket: Socket) {
147+
_remove(socket: Socket): void {
148148
if (this.sockets.has(socket.id)) {
149149
const nsp = this.sockets.get(socket.id)!.nsp.name;
150150
this.sockets.delete(socket.id);
@@ -159,7 +159,7 @@ export class Client {
159159
*
160160
* @private
161161
*/
162-
private close() {
162+
private close(): void {
163163
if ("open" === this.conn.readyState) {
164164
debug("forcing transport close");
165165
this.conn.close();
@@ -174,12 +174,13 @@ export class Client {
174174
* @param {Object} opts
175175
* @private
176176
*/
177-
_packet(packet, opts?) {
177+
_packet(packet: Packet, opts?: any): void {
178178
opts = opts || {};
179179
const self = this;
180180

181181
// this writes to the actual connection
182-
function writeToEngine(encodedPackets) {
182+
function writeToEngine(encodedPackets: any) {
183+
// TODO clarify this.
183184
if (opts.volatile && !self.conn.transport.writable) return;
184185
for (let i = 0; i < encodedPackets.length; i++) {
185186
self.conn.write(encodedPackets[i], { compress: opts.compress });
@@ -205,7 +206,7 @@ export class Client {
205206
*
206207
* @private
207208
*/
208-
private ondata(data) {
209+
private ondata(data): void {
209210
// try/catch is needed for protocol violations (GH-1880)
210211
try {
211212
this.decoder.add(data);
@@ -219,7 +220,7 @@ export class Client {
219220
*
220221
* @private
221222
*/
222-
private ondecoded(packet: Packet) {
223+
private ondecoded(packet: Packet): void {
223224
if (PacketType.CONNECT === packet.type) {
224225
this.connect(packet.nsp, packet.data);
225226
} else {
@@ -240,7 +241,7 @@ export class Client {
240241
* @param {Object} err object
241242
* @private
242243
*/
243-
private onerror(err) {
244+
private onerror(err): void {
244245
for (const socket of this.sockets.values()) {
245246
socket._onerror(err);
246247
}
@@ -253,7 +254,7 @@ export class Client {
253254
* @param reason
254255
* @private
255256
*/
256-
private onclose(reason: string) {
257+
private onclose(reason: string): void {
257258
debug("client close with reason %s", reason);
258259

259260
// ignore a potential subsequent `close` event
@@ -272,7 +273,7 @@ export class Client {
272273
* Cleans up event listeners.
273274
* @private
274275
*/
275-
private destroy() {
276+
private destroy(): void {
276277
this.conn.removeListener("data", this.ondata);
277278
this.conn.removeListener("error", this.onerror);
278279
this.conn.removeListener("close", this.onclose);

lib/index.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ export class Server extends EventEmitter {
191191
*/
192192
constructor(opts?: Partial<ServerOptions>);
193193
constructor(srv?: http.Server | number, opts?: Partial<ServerOptions>);
194+
constructor(
195+
srv: undefined | Partial<ServerOptions> | http.Server | number,
196+
opts?: Partial<ServerOptions>
197+
);
194198
constructor(
195199
srv: undefined | Partial<ServerOptions> | http.Server | number,
196200
opts: Partial<ServerOptions> = {}
@@ -222,9 +226,10 @@ export class Server extends EventEmitter {
222226
* @return self when setting or value when getting
223227
* @public
224228
*/
225-
public serveClient(v: boolean): Server;
229+
public serveClient(v: boolean): this;
226230
public serveClient(): boolean;
227-
public serveClient(v?: boolean): Server | boolean {
231+
public serveClient(v?: boolean): this | boolean;
232+
public serveClient(v?: boolean): this | boolean {
228233
if (!arguments.length) return this._serveClient;
229234
this._serveClient = v!;
230235
return this;
@@ -234,7 +239,7 @@ export class Server extends EventEmitter {
234239
* Executes the middleware for an incoming namespace not already created on the server.
235240
*
236241
* @param name - name of incoming namespace
237-
* @param {Object} auth - the auth parameters
242+
* @param auth - the auth parameters
238243
* @param fn - callback
239244
*
240245
* @private
@@ -243,7 +248,7 @@ export class Server extends EventEmitter {
243248
name: string,
244249
auth: object,
245250
fn: (nsp: Namespace | false) => void
246-
) {
251+
): void {
247252
if (this.parentNsps.size === 0) return fn(false);
248253

249254
const keysIterator = this.parentNsps.keys();
@@ -272,9 +277,10 @@ export class Server extends EventEmitter {
272277
* @return {Server|String} self when setting or value when getting
273278
* @public
274279
*/
275-
public path(v: string): Server;
280+
public path(v: string): this;
276281
public path(): string;
277-
public path(v?: string): Server | string {
282+
public path(v?: string): this | string;
283+
public path(v?: string): this | string {
278284
if (!arguments.length) return this._path;
279285

280286
this._path = v!.replace(/\/$/, "");
@@ -293,9 +299,10 @@ export class Server extends EventEmitter {
293299
* @param v
294300
* @public
295301
*/
296-
public connectTimeout(v: number): Server;
302+
public connectTimeout(v: number): this;
297303
public connectTimeout(): number;
298-
public connectTimeout(v?: number): Server | number {
304+
public connectTimeout(v?: number): this | number;
305+
public connectTimeout(v?: number): this | number {
299306
if (v === undefined) return this._connectTimeout;
300307
this._connectTimeout = v;
301308
return this;
@@ -309,8 +316,9 @@ export class Server extends EventEmitter {
309316
* @public
310317
*/
311318
public adapter(): typeof Adapter | undefined;
312-
public adapter(v: typeof Adapter): Server;
313-
public adapter(v?: typeof Adapter): typeof Adapter | undefined | Server {
319+
public adapter(v: typeof Adapter): this;
320+
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
321+
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this {
314322
if (!arguments.length) return this._adapter;
315323
this._adapter = v;
316324
for (const nsp of this._nsps.values()) {
@@ -330,7 +338,7 @@ export class Server extends EventEmitter {
330338
public listen(
331339
srv: http.Server | number,
332340
opts: Partial<ServerOptions> = {}
333-
): Server {
341+
): this {
334342
return this.attach(srv, opts);
335343
}
336344

@@ -345,7 +353,7 @@ export class Server extends EventEmitter {
345353
public attach(
346354
srv: http.Server | number,
347355
opts: Partial<ServerOptions> = {}
348-
): Server {
356+
): this {
349357
if ("function" == typeof srv) {
350358
const msg =
351359
"You are trying to attach socket.io to an express " +
@@ -385,7 +393,10 @@ export class Server extends EventEmitter {
385393
* @param opts - options passed to engine.io
386394
* @private
387395
*/
388-
private initEngine(srv: http.Server, opts: Partial<EngineAttachOptions>) {
396+
private initEngine(
397+
srv: http.Server,
398+
opts: Partial<EngineAttachOptions>
399+
): void {
389400
// initialize engine
390401
debug("creating engine.io instance with opts %j", opts);
391402
this.eio = engine.attach(srv, opts);
@@ -406,7 +417,7 @@ export class Server extends EventEmitter {
406417
* @param srv http server
407418
* @private
408419
*/
409-
private attachServe(srv: http.Server) {
420+
private attachServe(srv: http.Server): void {
410421
debug("attaching client serving req handler");
411422

412423
const evs = srv.listeners("request").slice(0);
@@ -429,7 +440,7 @@ export class Server extends EventEmitter {
429440
* @param res
430441
* @private
431442
*/
432-
private serve(req: http.IncomingMessage, res: http.ServerResponse) {
443+
private serve(req: http.IncomingMessage, res: http.ServerResponse): void {
433444
const filename = req.url!.replace(this._path, "");
434445
const isMap = dotMapRegex.test(filename);
435446
const type = isMap ? "map" : "source";
@@ -474,7 +485,7 @@ export class Server extends EventEmitter {
474485
filename: string,
475486
req: http.IncomingMessage,
476487
res: http.ServerResponse
477-
) {
488+
): void {
478489
const readStream = createReadStream(
479490
path.join(__dirname, "../client-dist/", filename)
480491
);
@@ -513,7 +524,7 @@ export class Server extends EventEmitter {
513524
* @return self
514525
* @public
515526
*/
516-
public bind(engine): Server {
527+
public bind(engine): this {
517528
this.engine = engine;
518529
this.engine.on("connection", this.onconnection.bind(this));
519530
return this;
@@ -526,7 +537,7 @@ export class Server extends EventEmitter {
526537
* @return self
527538
* @private
528539
*/
529-
private onconnection(conn): Server {
540+
private onconnection(conn): this {
530541
debug("incoming connection with id %s", conn.id);
531542
const client = new Client(this, conn);
532543
if (conn.protocol === 3) {
@@ -540,13 +551,13 @@ export class Server extends EventEmitter {
540551
* Looks up a namespace.
541552
*
542553
* @param {String|RegExp|Function} name nsp name
543-
* @param [fn] optional, nsp `connection` ev handler
554+
* @param fn optional, nsp `connection` ev handler
544555
* @public
545556
*/
546557
public of(
547558
name: string | RegExp | ParentNspNameMatchFn,
548559
fn?: (socket: Socket) => void
549-
) {
560+
): Namespace {
550561
if (typeof name === "function" || name instanceof RegExp) {
551562
const parentNsp = new ParentNamespace(this);
552563
debug("initializing parent namespace %s", parentNsp.name);
@@ -605,7 +616,7 @@ export class Server extends EventEmitter {
605616
*/
606617
public use(
607618
fn: (socket: Socket, next: (err?: ExtendedError) => void) => void
608-
): Server {
619+
): this {
609620
this.sockets.use(fn);
610621
return this;
611622
}
@@ -617,7 +628,7 @@ export class Server extends EventEmitter {
617628
* @return self
618629
* @public
619630
*/
620-
public to(name: Room): Server {
631+
public to(name: Room): this {
621632
this.sockets.to(name);
622633
return this;
623634
}
@@ -629,7 +640,7 @@ export class Server extends EventEmitter {
629640
* @return self
630641
* @public
631642
*/
632-
public in(name: Room): Server {
643+
public in(name: Room): this {
633644
this.sockets.in(name);
634645
return this;
635646
}
@@ -640,7 +651,7 @@ export class Server extends EventEmitter {
640651
* @return self
641652
* @public
642653
*/
643-
public send(...args: readonly any[]): Server {
654+
public send(...args: readonly any[]): this {
644655
this.sockets.emit("message", ...args);
645656
return this;
646657
}
@@ -651,7 +662,7 @@ export class Server extends EventEmitter {
651662
* @return self
652663
* @public
653664
*/
654-
public write(...args: readonly any[]): Server {
665+
public write(...args: readonly any[]): this {
655666
this.sockets.emit("message", ...args);
656667
return this;
657668
}
@@ -672,7 +683,7 @@ export class Server extends EventEmitter {
672683
* @return self
673684
* @public
674685
*/
675-
public compress(compress: boolean): Server {
686+
public compress(compress: boolean): this {
676687
this.sockets.compress(compress);
677688
return this;
678689
}
@@ -685,7 +696,7 @@ export class Server extends EventEmitter {
685696
* @return self
686697
* @public
687698
*/
688-
public get volatile(): Server {
699+
public get volatile(): this {
689700
this.sockets.volatile;
690701
return this;
691702
}
@@ -696,7 +707,7 @@ export class Server extends EventEmitter {
696707
* @return self
697708
* @public
698709
*/
699-
public get local(): Server {
710+
public get local(): this {
700711
this.sockets.local;
701712
return this;
702713
}

0 commit comments

Comments
 (0)