From 67f88cc50ea1543c8bc9a22c8029972c460c8e50 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Thu, 22 Jun 2023 10:30:03 +0200 Subject: [PATCH] Fixes Websocket Typescript issues (#1277) --- waspc/data/Generator/templates/react-app/src/webSocket.ts | 8 ++++++-- .../templates/server/src/webSocket/initialization.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/waspc/data/Generator/templates/react-app/src/webSocket.ts b/waspc/data/Generator/templates/react-app/src/webSocket.ts index 7a756955f..22cf32278 100644 --- a/waspc/data/Generator/templates/react-app/src/webSocket.ts +++ b/waspc/data/Generator/templates/react-app/src/webSocket.ts @@ -28,9 +28,13 @@ export function useSocketListener( // Casting to `keyof ServerToClientEvents` is necessary because TypeScript // reports the handler function as incompatible with the event type. // See https://github.com/wasp-lang/wasp/pull/1203#discussion_r1232068898 - socket.on(event as keyof ServerToClientEvents, handler) + + // We are wrapping it in `Extract<...>` due to Typescript infering string | number + // in the case of default events being used. + type AllowedEvents = Extract; + socket.on(event as AllowedEvents, handler) return () => { - socket.off(event as keyof ServerToClientEvents, handler) + socket.off(event as AllowedEvents, handler) } }, [event, handler]) } diff --git a/waspc/data/Generator/templates/server/src/webSocket/initialization.ts b/waspc/data/Generator/templates/server/src/webSocket/initialization.ts index ae00fcb08..f467d0de7 100644 --- a/waspc/data/Generator/templates/server/src/webSocket/initialization.ts +++ b/waspc/data/Generator/templates/server/src/webSocket/initialization.ts @@ -35,7 +35,7 @@ export async function init(server: http.Server): Promise { } } - await {= userWebSocketFn.importIdentifier =}(io, context) + await ({= userWebSocketFn.importIdentifier =} as any)(io, context) } {=# isAuthEnabled =}