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

TypeScript 5 claims IStompSocketMessageEvent and MessageEvent are incompatible #619

Open
bes opened this issue Dec 26, 2023 · 3 comments
Open
Milestone

Comments

@bes
Copy link

bes commented Dec 26, 2023

Hello,

I recently decided to upgrade to stompjs 7 from 5.4.4, and typescript doesn't like the following code:

        client.webSocketFactory = (): WebSocket => {
            return new WebSocket(myBrokerUrl);
        };

The compiler error is the following:

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 seems that my typescript configuration expects IStompSocketMessageEvent to contain a lot more fields than data?: string | ArrayBuffer; to be compatible with MessageEvent from lib.dom.d.ts.

How can I work around this issue?

@bes
Copy link
Author

bes commented Dec 26, 2023

Changing the return type doesn't help either:

        client.webSocketFactory = (): IStompSocket => {
            return new WebSocket(myBrokerUrl);
        };
TS2322: Type 'WebSocket' is not assignable to type 'IStompSocket'.
  Types of property 'onmessage' are incompatible.
    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.

@bes
Copy link
Author

bes commented Dec 26, 2023

This works, but feels a bit icky:

        client.webSocketFactory = (): IStompSocket => {
            return new WebSocket(myBrokerUrl) as IStompSocket;
        };

I would rather not cast with as if I can avoid it.

@kum-deepak
Copy link
Member

Currently I guess your workaround is alright. I can look at a better fix when I upgrade this library build to use Typescript 5.

@kum-deepak kum-deepak added this to the v7.1 milestone Apr 26, 2024
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

2 participants