Skip to content

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

Open
@shaunco

Description

@shaunco

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions