max-mapper / websocket-stream

websockets with the node stream API
BSD 2-Clause "Simplified" License
667 stars 114 forks source link

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

Open shaunco opened 4 years ago

shaunco commented 4 years ago

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

Maximvdw commented 2 years ago

still an issue 1 year later