Ws: WebSocket objects not assignable to TypeScript WebSocket type because they do not implement the `dispatchEvent` method

Created on 12 Jun 2019  路  4Comments  路  Source: websockets/ws

  • [x] I've searched for any related issues and avoided creating a duplicate
    issue.

Description

The WebSocket objects which ws uses are not assignable to the typescript WebSocket type because the dispatchEvent method is missing from the implementation.

Reproducible in:

  • version: 7.0.0
  • Node.js version: 10.16.0
  • TypeScript version: 3.4.5
  • OS version(s): MacOS 10.14.4

Steps to reproduce:

  1. Create a ws server

  2. Listen for the connection event

  3. Pass the websocket passed to the connection handler to a typescript function which expects a WebSocket object.

Expected result:

No typechecking errors occur

Actual result:

    src/connection/__tests__/socket.ts:104:33 - error TS2345: Argument of type 'WebSocket' is not assignable to parameter of type 'Socket'.
      Property 'dispatchEvent' is missing in type 'WebSocket' but required in type 'WebSocket'.

    104         resolve(new SocketProxy(socket, storage));
                                        ~~~~~~

      ../../node_modules/typescript/lib/lib.dom.d.ts:5226:5
        5226     dispatchEvent(event: Event): boolean;
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'dispatchEvent' is declared here.

Attachments:

Most helpful comment

A stub method which throws a not implemented error would be enough to satisfy the type definition.

Please file a PR if you are interested in having it.

All 4 comments

There is no TypeScript on this project and there is no dispatchEvent() because we don't fully implement the EventTarget interface.

There is no TypeScript on this project

Understood! I鈥檓 using the types from https://www.npmjs.com/package/@types/ws

there is no dispatchEvent() because we don't fully implement the EventTarget interface.

A stub method which throws a not implemented error would be enough to satisfy the type definition.

A stub method which throws a not implemented error would be enough to satisfy the type definition.

Please file a PR if you are interested in having it.

I encountered this issue as well. In my case, I have an interface that contains:

ws: WebSocket;

By default, TS assumes you mean the definition in _lib.dom.d.ts_. Since the only WebSocket I cared about was the class declaration defined in the @types/ws package, I added the following at the top of the file containing my interface:

import WebSocket from 'ws';

This explicit import overrides the "default" one. If you are already importing other stuff from 'ws', you can do:

import WebSocket, { Server /* etc */} from 'ws';

(note: I am using @types/ws version 7.4.0.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

makc picture makc  路  4Comments

quesurifn picture quesurifn  路  3Comments

sherikapotein picture sherikapotein  路  3Comments

robertmylne picture robertmylne  路  3Comments

dcflow picture dcflow  路  4Comments