New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importing the module doesn't work like shown in the readme for TypeScript projects #1932
Comments
It works fine as documented. |
For me it does not work either. Reproduction:
Code: import WebSocket, { WebSocketServer } from 'ws';
....
export class SocketServer extends WebSocketServer {
public static instance: SocketServer;
constructor(server: Http.Server) {
super(SocketServer._options(server));
if (SocketServer.instance)
return SocketServer.instance;
SocketServer.instance = this;
}
private static _options(server: Http.Server): WebSocket.ServerOptions {
return { clientTracking: false, noServer: true}
}
} This does not work, even client can not connect to the server via WebSocket |
That is not what happens when you try and import Just take a look here: https://replit.com/@javanotti/UnwieldyHotpinkRedundancy#index.ts |
This is a TypeScript issue. Remove TypeScript and it works. |
I maybe suboptimally worded the issue, but still this should be either adapted to work as expected or documented to show how to use the module in typescript. "Removing TypeScript" is not an option in TypeScript projects, I guess. |
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 |
See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f7dc6fa52566fe3b/types/ws/index.d.ts. Those type definitions are for |
Yes, I understand but I think you can remove the |
Exploring source-code, it only seems that instead of {WebSocketServer} one needs to import {Server} (see index.d.ts, line 300) |
@lpinca Is it intentional to export different names for the same class? I think it makes things unnecessarily complicated. I am not sure we can define
Line 5 in f38247e
Line 6 in f38247e
|
We could use
|
If there is no way to work around this in |
It would be very helpful. |
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 have same issue, importing Default.Server returns undefined, importing WebSocketServer as individual works, but typescript is screaming with errors |
Add `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consistency and improve interoperability with the ES module wrapper. Refs: #1932
Please take a look at #1935. Anyway, FWIW, an ESM only declaration file as suggested by @Pokute in DefinitelyTyped/DefinitelyTyped#55151 (comment) makes sense to me now that That said, the |
I think you had better have the I am the author of the PR. However, I am not one of the maintainers of the file. The decision of making breaking changes is up to them. I am not sure who is an active maintainer in the listed names in the file. With more people involved, the decision-making would be slow. If you have the |
@tamuratak I don't want to maintain the TypeScript type definitions. It's an additional burden and source of breaking changes I don't want to deal with. JSDoc annotations are sufficient for me. |
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 |
@tamuratak I think that Typescript support is necessary nowadays. |
I've merged #1935 and I will cut a new release with it shortly. Once the |
Having working TypeScript definitions will still be impossible in my opinion, since |
I think there is no viable solution to that. Things are different. What matters is that import WebSocket, { WebSocketServer } from 'ws'; works as expected for TypeScript. I think TypeScript will always use the CJS implementation as conditional exports are not supported. |
With DefinitelyTyped/DefinitelyTyped#55151 (comment) and #1935, the above If
|
It still feels a bit over my head, but I read more about TS definition files and it seems like the runtime side might be all right. After that it's just some work to get the type definitions working properly. |
I'm closing this as DefinitelyTyped/DefinitelyTyped#55151 should fix the issue once merged. |
Add `WebSocket.WebSocket` as an alias for `WebSocket` and `WebSocket.WebSocketServer` as an alias for `WebSocket.Server` to fix name consistency and improve interoperability with the ES module wrapper. Refs: websockets/ws#1877 Refs: websockets/ws#1932
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 projecttry 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
The text was updated successfully, but these errors were encountered: