Typescript definition is broken if @types/express-ws is in the same package.json
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from max-mapper/websocket-stream
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
max-mapper/websocket-stream#168 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
max-mapper/websocket-stream#165 · 1 comment · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
max-mapper/websocket-stream#164 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
max-mapper/websocket-stream#163 · 2 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 25/100
max-mapper/websocket-stream#162 · 2 comments · 1 reaction ·
All issues in max-mapper/websocket-stream
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·