Skip to content

Specify Type of SendJsonMessage correctly #215

Open
@hellow554

Description

@hellow554

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions