Skip to content

Commit

Permalink
fix(typings): properly type server-side events
Browse files Browse the repository at this point in the history
See also: 93cce05
  • Loading branch information
darrachequesne committed May 11, 2021
1 parent fb6b0ef commit b84ed1e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ interface ServerOptions extends EngineAttachOptions {
export class Server<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = {}
ServerSideEvents extends EventsMap = DefaultEventsMap
> extends StrictEventEmitter<
ServerSideEvents,
EmitEvents,
Expand Down
7 changes: 3 additions & 4 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set<
export class Namespace<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = {}
ServerSideEvents extends EventsMap = DefaultEventsMap
> extends StrictEventEmitter<
ServerSideEvents,
EmitEvents,
Expand Down Expand Up @@ -306,9 +306,8 @@ export class Namespace<
*
* @private
*/
_onServerSideEmit(args: any[]) {
const event = args.shift();
this.emitUntyped(event, args);
_onServerSideEmit(args: [eventName: string, ...args: any[]]) {

This comment has been minimized.

Copy link
@JordanPawlett

JordanPawlett May 13, 2021

Shouldn't this be args: [string, ...any[]]? As far as i'm aware you're unable to define properties names as an individual element in an array.

This may cause issues for people compiling this dependency with certain typescript versions.

This comment has been minimized.

Copy link
@MaximeKjaer

MaximeKjaer May 13, 2021

Contributor

This is a new feature since TypeScript 4.0, labeled tuple types.

I think you might be correct about older TypeScript versions. It's possible that this is what caused #3916.

This comment has been minimized.

Copy link
@darrachequesne

darrachequesne May 17, 2021

Author Member

This should be fixed by 0cb6ac9, included in [email protected].

super.emitUntyped.apply(this, args);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/parent-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { BroadcastOptions } from "socket.io-adapter";
export class ParentNamespace<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = {}
ServerSideEvents extends EventsMap = DefaultEventsMap
> extends Namespace<ListenEvents, EmitEvents, ServerSideEvents> {
private static count: number = 0;
private children: Set<
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface Handshake {
export class Socket<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = {}
ServerSideEvents extends EventsMap = DefaultEventsMap
> extends StrictEventEmitter<
ListenEvents,
EmitEvents,
Expand Down
4 changes: 3 additions & 1 deletion test/socket.io.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ describe("server", () => {

it("does not accept arguments of wrong types", (done) => {
const srv = createServer();
const sio = new Server<BidirectionalEvents>(srv);
const sio = new Server<BidirectionalEvents, BidirectionalEvents, {}>(
srv
);
expectError(sio.on("random", (a, b, c) => {}));
srv.listen(() => {
expectError(sio.on("wrong name", (s) => {}));
Expand Down

0 comments on commit b84ed1e

Please sign in to comment.