Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify Type of SendJsonMessage correctly #215

Open
hellow554 opened this issue Oct 30, 2023 · 0 comments
Open

Specify Type of SendJsonMessage correctly #215

hellow554 opened this issue Oct 30, 2023 · 0 comments

Comments

@hellow554
Copy link

Currently, we have this arrangement of types

export type WebSocketHook<T = unknown, P = WebSocketEventMap['message'] | null> = {
sendMessage: SendMessage,
sendJsonMessage: SendJsonMessage,
lastMessage: P,
lastJsonMessage: T,
readyState: ReadyState,
getWebSocket: () => (WebSocketLike | null),
}

The problem is, that we T is not forwarded to the sendJsonMessage member. If we try, we get an error: Type 'SendJsonMessage' is not generic., to fix that we can change

export type SendJsonMessage = <T = unknown>(jsonMessage: T, keep?: boolean) => void;

from type SendJsonMessage = <T = unknown>(...) to type SendJsonMessage<T = unknown> = (...) (note the position of the = sign).

So my first question is:

  1. Was this an oversight?
  2. Would sendJsonMessage: SendJsonMessage<T> a proper fix, or should we allow different types for sendJsonMessage and lastJsonMessage, e.g.:
export type WebSocketHook<Ret = unknown, Send = Ret, P = WebSocketEventMap['message'] | null> = {
  sendMessage: SendMessage,
  sendJsonMessage: SendJsonMessage<Send>,
  lastMessage: P,
  lastJsonMessage: Ret,
  readyState: ReadyState,
  getWebSocket: () => (WebSocketLike | null),
}

(also update useWebSocket of course)

Here's a small playground I created to test this: Playground Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant