Typescript ReconnectingWebSocket无法调用构造函数

时间:2018-09-27 03:07:03

标签: javascript typescript

当我尝试创建一个新的ReconnectingWebSocket时,我得到以下信息:

count

打字稿代码为:

reconnecting_websocket_1.default is not a constructor

在运行时,chrome开发工具中生成的javascript显示import ReconnectingWebSocket, { Options } from 'reconnecting-websocket'; export class RealtimeClient { private reconnectingWebSocket: ReconnectingWebSocket constructor(private url: string, private dispatch: IDispatcher) {} public connect() { const wsOptions: Options = { minReconnectionDelay: 1000, reconnectionDelayGrowFactor: 1.0, connectionTimeout: 2000, } this.reconnectingWebSocket = new ReconnectingWebSocket(this.url, [], wsOptions) this.reconnectingWebSocket.onmessage = this.onMessage } /* ... */ } 是预期的ReconnectingWebSocket类,但是reconnecting_websocket_1未定义,我认为这是失败的根本原因。

default

NB:

  1. 我正在编译到ES5 javascript(通过tsconfig.json target )。
  2. 我对reconnecting-websocket具有非开发依赖     this.reconnectingWebSocket = new reconnecting_websocket_1.default(this.url, [], wsOptions); this.reconnectingWebSocket.onmessage = this.onMessage;

1 个答案:

答案 0 :(得分:1)

将编译器选项esModuleInterop添加到tsconfig.json。
您可能还想看看socket.io-client,它稍大一些,但3年前没有被放弃。