home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type errors with typescript and nodejs

Julusian opened this issue · comments

When using this library with nodejs and typescript, there is a type error as Websocket is not a known type.

This happens with both 4.5.0 and 4.4.0.
The readme section https://github.com/home-assistant/home-assistant-js-websocket#using-this-in-nodejs is no help here, as it does not use typescript, so will not exhibit these type errors.

[17:29:58] File change detected. Starting incremental compilation...

node_modules/home-assistant-js-websocket/dist/socket.d.ts:2:38 - error TS2304: Cannot find name 'WebSocket'.

2 export interface HaWebSocket extends WebSocket {
                                       ~~~~~~~~~

[17:29:58] Found 1 error. Watching for file changes.

I suggest you disable typechecking of node modules.

Ah, I did not find the skipLibCheck option in my searching.
While that does resolve the type error, I do still have to reimplement createSocket to be able to use this. I am using an exact copy to do that which isnt nice, especially as 4.5 added the haVersion field which is done in that block

That is correct. We've tried to make it browser/node agnostic but were not able to. In the end all consumers end up implementing their own version with custom retry logic etc so that's the way we roll now.

This lib's focus is on browsers. NodeJS support works but is not a goal.

I have opened #111 to improve on the remaining typing issues