pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.22k stars 197 forks source link

Constructing ReconnectingWebsocket error in TypeScript #24

Closed slikts closed 7 years ago

slikts commented 7 years ago

Code: new ReconnectingWebSocket(url)

Error: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.'

pladaria commented 7 years ago

Just published v3.2.0 with noImplicitAny set to true

alex-weaver commented 7 years ago

This still seems to be an issue in v3.2.0 and v3.2.1, unless there's a mistake in the import declaration? The code I'm using is as follows:

import ReconnectingWebSocket from 'reconnecting-websocket'
const socket = new ReconnectingWebSocket('ws://my.site.com');

This is using the npm package, referenced in package.json using: "reconnecting-websocket": "3.2.1" (Also tried with 3.2.0)

This yields two errors:

TS2350  (TS) Only a void function can be called with the 'new' keyword.
TS7009  (TS) 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

Is there a mistake in how I'm importing the library?

slikts commented 7 years ago

Is there a mistake in how I'm importing the library?

There isn't, it doesn't seem like the author would have tried actually using their code with TypeScript.

pladaria commented 7 years ago

Hi @alex-weaver,

Thanks for reporting. Should be fixed in v3.2.2

Don't hesitate to open a new ticket if you find any issue :)

pladaria commented 7 years ago

Although this lib was coded with a js app in mind, here you have a working ts example:

import * as WS from 'html5-websocket';
import * as RWS from 'reconnecting-websocket';

const rws = new RWS('ws://echo.websocket.org/', undefined, {constructor: WS});

rws.onopen = () =>
    rws.send('hello');

rws.onmessage = (e) => {
    console.log(e.data);
    rws.close(0, 'bye', {keepClosed: true});
};
pre-martin commented 7 years ago

I get an error message, when using "strict": true in the compiler options:

test.ts:

import * as ReconnectingWebsocket from 'reconnecting-websocket';

const websocket: WebSocket = new ReconnectingWebsocket('url', undefined, {constructor: WebSocket});

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "strict": true,
    "allowSyntheticDefaultImports": true
  }
}

Compile error:

node node_modules/typescript/lib/tsc.js -p tsconfig.json

test.ts(3,30): error TS7009: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.

If I remove "strict": true from tsconfig.json, the code compiles fine. But I want this option to be true.

tsc: 2.4.2 reconnecting-websocket: 3.2.2

fahdk commented 6 years ago

Same error. Typescript 2.7.2, reconnecting-websocket 3.2.2


  "compilerOptions": {
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true
  }
kalmi commented 6 years ago

My understanding is that changing noImplicitAny in the library's tsconfig will have no effect whatsoever when importing as only the importer project's tsconfig will be in effect.