Typescript definition is broken if @types/express-ws is in the same package.json

Open
#161 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
node.js, typescript
Domain
backend

Research direction

Update the declarations in index.d.ts, focusing on the Server.on overloads shown in the issue. Reproduce the failure with TypeScript and @types/express-ws, then run tsc again; done means the reported TS2416 errors no longer occur and the Server callback typings match the intended Server type.

Written by the indexing model from the issue text.

Description

When @types/express-ws (npm install --save-dev @types/express-ws) is added to the same Typescript project that uses websocket-stream, tsc produces the following:

> tsc

node_modules/websocket-stream/index.d.ts:13:5 - error TS2416: Property 'on' in type 'Server' is not assignable to the same property in base type 'Server'.
  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.
    Types of parameters 'event' and 'event' are incompatible.
      Type '"error"' is not assignable to type '"connection"'.

13     on(event: 'connection', cb: (this: WebSocket.Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
       ~~

node_modules/websocket-stream/index.d.ts:14:5 - error TS2416: Property 'on' in type 'Server' is not assignable to the same property in base type 'Server'.
  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.

14     on(event: 'error', cb: (this: WebSocket, error: Error) => void): this;
       ~~

node_modules/websocket-stream/index.d.ts:15:5 - error TS2416: Property 'on' in type 'Server' is not assignable to the same property in base type 'Server'.
  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.

15     on(event: 'headers', cb: (this: WebSocket, headers: string[], request: http.IncomingMessage) => void): this;
       ~~

node_modules/websocket-stream/index.d.ts:16:5 - error TS2416: Property 'on' in type 'Server' is not assignable to the same property in base type 'Server'.
  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.

16     on(event: 'listening', cb: (this: WebSocket) => void): this;
       ~~

node_modules/websocket-stream/index.d.ts:17:5 - error TS2416: Property 'on' in type 'Server' is not assignable to the same property in base type 'Server'.
  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.

17     on(event: 'stream', cb: (this: WebSocket, stream: WebSocketDuplex, request: http.IncomingMessage) => void): this;
       ~~

  Type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: WebSocket, error: Error) => void): this; (event: "headers", cb: (this: WebSocket, headers: string[], request: IncomingMessage) => void): this; (event: "listening", cb: (this: WebSocket) => void): this; (event: "stream", cb: (this: WebSocket, stream: WebSocketDuplex, request: IncomingMessage) => void): this; (event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this; }' is not assignable to type '{ (event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this; (event: "error", cb: (this: Server, error: Error) => void): this; (event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this; (event: "close" | "listening", cb: (this: Server) => void): this; (event: string | symbol, listener: (this: Server, ...args: any[]) => void): this; }'.

18     on(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;

The fix seems to be changing index.d.ts from:

    on(event: 'connection', cb: (this: WebSocket.Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
    on(event: 'error', cb: (this: WebSocket, error: Error) => void): this;
    on(event: 'headers', cb: (this: WebSocket, headers: string[], request: http.IncomingMessage) => void): this;
    on(event: 'listening', cb: (this: WebSocket) => void): this;
    on(event: 'stream', cb: (this: WebSocket, stream: WebSocketDuplex, request: http.IncomingMessage) => void): this;
    on(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;

to:

    on(event: 'connection', cb: (this: Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
    on(event: 'error', cb: (this: Server, error: Error) => void): this;
    on(event: 'headers', cb: (this: Server, headers: string[], request: http.IncomingMessage) => void): this;
    on(event: 'listening', cb: (this: Server) => void): this;
    on(event: 'stream', cb: (this: Server, stream: WebSocketDuplex, request: http.IncomingMessage) => void): this;
    on(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;

which matches the typings for WebSocket.Server

Dominant language
JavaScript
Stars
667
Forks
110
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from max-mapper/websocket-stream

All issues in max-mapper/websocket-stream

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.