ws: importing the module doesn't work like shown in the readme for TypeScript projects
- I’ve searched for any related issues and avoided creating a duplicate issue.
Description
The module must be imported with
import * as WebSocket from 'ws';
while the readme shows the module being imported with
import WebSocket, { WebSocketServer } from 'ws';
Reproducible in:
Any TypeScript project.
Steps to reproduce:
-
install ws
-
import it like
import WebSocket, { WebSocketServer } from 'ws';in a project -
try using the module as expected
Expected result:
The module can be imported as shown in the readme.
Actual result:
The module must be imported in alternative ways.
Attachments:
Just try running this piece of code
import WebSocket, { WebSocketServer } from 'ws';
const WSServer = new WebSocketServer();
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 26 (13 by maintainers)
Commits related to this issue
- [minor] Add `WebSocket.WebSocketServer` alias Add `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consistency and improve interoperability with the ES module wrapper. Refs... — committed to websockets/ws by lpinca 3 years ago
- [minor] Add `WebSocket.WebSocket{,Server}` aliases Add `WebSocket.WebSocket` as an alias for `WebSocket` and `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consistency and... — committed to websockets/ws by lpinca 3 years ago
- [minor] Add `WebSocket.WebSocket{,Server}` aliases (#1935) Add `WebSocket.WebSocket` as an alias for `WebSocket` and `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consi... — committed to websockets/ws by lpinca 3 years ago
- [minor] Add `WebSocket.WebSocket{,Server}` aliases (#1935) Add `WebSocket.WebSocket` as an alias for `WebSocket` and `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consi... — committed to th37rose/websocket_server by th37rose 3 years ago
This is a TypeScript issue. Remove TypeScript and it works.
We do not officially support TypeScript so it’s up to TypeScript users to fix TypeScript issues.
The fix should be done in the TypeScript type definitions and not in
wsdocumentation.See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f7dc6fa52566fe3b/types/ws/index.d.ts. Those type definitions are for
ws@7.@tamuratak I think that Typescript support is necessary nowadays.
I suspect there is a problem with CJS and ESM versions having different interfaces due to the run-time shape being different.
I’m not sure if typescript definitions support having different definitions for CJS and ESM.
I’m closing this as https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55151 should fix the issue once merged.
Fair enough. I agree that it is a good practice for OSS maintainers to decrease the maintenance burden as much as possible. I have written your comment in the PR. I want to wait for the
@types/wsmaintainers’ reply.