Fixes Websocket Typescript issues (#1277)

This commit is contained in:
Mihovil Ilakovac 2023-06-22 10:30:03 +02:00 committed by GitHub
parent 84766299cb
commit 67f88cc50e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -28,9 +28,13 @@ export function useSocketListener<Event extends keyof ServerToClientEvents>(
// 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<keyof ServerToClientEvents, string>;
socket.on(event as AllowedEvents, handler)
return () => {
socket.off(event as keyof ServerToClientEvents, handler)
socket.off(event as AllowedEvents, handler)
}
}, [event, handler])
}

View File

@ -35,7 +35,7 @@ export async function init(server: http.Server): Promise<void> {
}
}
await {= userWebSocketFn.importIdentifier =}(io, context)
await ({= userWebSocketFn.importIdentifier =} as any)(io, context)
}
{=# isAuthEnabled =}