stomp-js / stompjs

Javascript and Typescript Stomp client for Web browsers and node.js apps
Apache License 2.0
753 stars 81 forks source link

Node.js Typescript compiler cannot find (DOM) types #218

Closed genadis closed 4 years ago

genadis commented 4 years ago

Please see FAQs at https://stomp-js.github.io/faqs/2019/05/20/faqs.html before reporting.

Thank you for the great project!

My setup is Node.Js + typescript (no webpack). Typescript > 12 I've followed the guide, added 'ws' for websockets support.

in my tsconfig:

"target": "es5",
    "module": "commonjs",
    "lib": [
        "es2017",
        "es2015",
    ], 

When compiling I get a number of errors regarding missing DOM types:

> tsc

node_modules/@stomp/stompjs/esm5/client.d.ts:50:29 - error TS2304: Cannot find name 'WebSocket'.

50     webSocketFactory: () => WebSocket;
                               ~~~~~~~~~

node_modules/@stomp/stompjs/esm5/client.d.ts:105:25 - error TS2304: Cannot find name 'WebSocket'.

105     readonly webSocket: WebSocket;
                            ~~~~~~~~~

node_modules/@stomp/stompjs/esm5/client.d.ts:110:27 - error TS2304: Cannot find name 'WebSocket'.

110     protected _webSocket: WebSocket;
                              ~~~~~~~~~

node_modules/@stomp/stompjs/esm5/types.d.ts:28:52 - error TS2304: Cannot find name 'CloseEvent'.

28 export declare type closeEventCallbackType = (evt: CloseEvent) => void;
                                                      ~~~~~~~~~~

node_modules/@stomp/stompjs/esm5/types.d.ts:35:49 - error TS2304: Cannot find name 'Event'.

35 export declare type wsErrorCallbackType = (evt: Event) => void;
                                                   ~~~~~

Found 5 errors.

I've tied adding @types/ws no luck. But even if it worked, there are still errors regarding Event and CloseEvent.

The only workaround that worked for me so far is adding 'dom' to 'libs' of the tsonfig. and probably skipLibCheck would have worked as well.

It would be great if the library didn't have dependency on the DOM types

Thanks!

kum-deepak commented 4 years ago

This is an interesting case. It has come up earlier as well. Following is a summary:

Do you have any idea on how to avoid it without compromising type checking abilities?

genadis commented 4 years ago

Since the internal usage of the types is very limited, a few callbacks, send and constructor, I would suggest defining a minimum interface for the Websocket calling it StompWebsocket for example. webSocketFactory could leverage typescript generics, the default being StompWebsocket. The user of the library could path it's own type.

That way as library author you define the minimum required interfaces, and the user could use any compliant websocket library.

@kum-deepak what do you think?

kum-deepak commented 4 years ago

That makes sense. We can try an interface say IWebSocket with only needed subset of the actual WebSocket interface. Will you take a shot and create a PR?

This opens up another interesting option - for NodeJS users - it is possible to create a thin wrapper over TCP socket for this library to work, avoiding WebSockets completely.

Considering this, we can try the name to be IStompSocket or ISocket.

genadis commented 4 years ago

@kum-deepak I've created a pull request, please see if it's ok by you. it should allow full flexibility for wrapping the socket for node and for the browser

kum-deepak commented 4 years ago

Resolved by #224