Skip to content

Cannot assign WebSocket or SockJS to IStompSocket on TypeScript #645

@tearfur

Description

@tearfur

Got this error:

TS2322: Type '() => WebSocket' is not assignable to type '() => IStompSocket'.
  Call signature return types 'WebSocket' and 'IStompSocket' are incompatible.
    The types of 'onmessage' are incompatible between these types.
      Type '((this: WebSocket, ev: MessageEvent<any>) => any) | null' is not assignable to type '((ev: IStompSocketMessageEvent) => any) | null | undefined'.
        Type '(this: WebSocket, ev: MessageEvent<any>) => any' is not assignable to type '(ev: IStompSocketMessageEvent) => any'.
          Types of parameters 'ev' and 'ev' are incompatible.
            Type 'IStompSocketMessageEvent' is missing the following properties from type 'MessageEvent<any>': lastEventId, origin, ports, source, and 23 more.

It's really easy to reproduce with code like this:

const stompClient = new Client({ /* ... */ });

stompClient.webSocketFactory = function () {
  return new WebSocket(url);
};

This is what I have to do as a workaround:

const stompClient = new Client({ /* ... */ });

stompClient.webSocketFactory = function () {
  return new WebSocket(url) as IStompSocket;
};

stompjs 7.0.0
TypeScript 5.7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions