reactiverse / es4x

🚀 fast JavaScript 4 Eclipse Vert.x
https://reactiverse.io/es4x/
Apache License 2.0
877 stars 75 forks source link

error TS2451: Cannot redeclare block-scoped variable 'vertx' #573

Closed namdao2000 closed 2 years ago

namdao2000 commented 2 years ago

Description

Im trying to use the router module from @vertx/web and I am running into 7 errors. I followed the starter guide here

Steps to reproduce this bug:

  1. Create a new project using es4x project --ts and run npm install @vertx/web --save-prod
  2. Run npm install
  3. Paste in the index.ts code from below
  4. Run npm start

Error


9036   const vertx: Vertx;
             ~~~~~

  node_modules/@vertx/web/node_modules/@vertx/core/index.d.ts:9036:9
    9036   const vertx: Vertx;
                 ~~~~~
    'vertx' was also declared here.

node_modules/@vertx/core/index.d.ts:9062:9 - error TS2451: Cannot redeclare block-scoped variable 'Java'.

9062   const Java: {
             ~~~~

  node_modules/@vertx/web/node_modules/@vertx/core/index.d.ts:9062:9
    9062   const Java: {
                 ~~~~
    'Java' was also declared here.

... and few more ...

Found 7 errors.

Code

My index.ts code:

/// <reference types="@vertx/core" />
// @ts-check
import { Router } from '@vertx/web';

const app = Router.router(vertx);

app.route('/').handler(ctx => {
  ctx.response()
    .end('Hello from Vert.x Web!');
});

vertx.createHttpServer()
  .requestHandler(app)
  .listen(8080);

console.log('Server listening at: http://localhost:8080/')
pmlopes commented 2 years ago

@namdao2000 I see that there are some versions mixed together that are conflicting. From your report:

node_modules/@vertx/web/node_modules/@vertx/core/index.d.ts:9036:9
node_modules/@vertx/core/index.d.ts:9062:9 

If I look up the sources, the latest reference seems correct to the latest version 0.17.0 but the previous one doesn't match, so I'm assuming it's from an older release.

Usually, NPM should have created a flat dependency node_modules, so I'm guessing that there's some misalignment with your project?

Maybe a rm -Rf node_modules and npm install may fix it?

namdao2000 commented 2 years ago

Hello @pmlopes

Firstly, I want to say thank you so much for your prompt reply. I've been down the rabbit hole of trying to build the most performant REST API server using the Nodejs/Typescript ecosystem. This is awesome!

As to your suggested fix, I tried it and it did not work for me. So I then tried to rm -rf node_mdules again and changed my dependency versions in package.json file to latest instead of specific versions.

  "dependencies": {
    "@vertx/core": "latest",
    "@vertx/web": "latest"
  },
  "devDependencies": {
    "@es4x/create": "latest",
    "@vertx/unit": "latest",
    "typescript": "^4.5.5"
  },

Then I ran npm install, npm start and the program is now only throwing 1 error:

node_modules/@vertx/web/options.d.ts:40:10 - error TS2305: Module '"@vertx/bridge-common"' has no exported member 'BridgeOptions'.

40 import { BridgeOptions } from '@vertx/bridge-common';
            ~~~~~~~~~~~~~

Found 1 error.

Thank you once again!

namdao2000 commented 2 years ago

Update: I found that when I edited the node_modules/@vertx/web/options.d.ts:40 from:

import { BridgeOptions } from '@vertx/bridge-common';

to:

import { BridgeOptions } from '@vertx/bridge-common/options';

Fixes the issue

pmlopes commented 2 years ago

@namdao2000 indeed, this is a bug in the definition file generator!

namdao2000 commented 2 years ago

Thank you for working on that @pmlopes! I see you have a PR opened - as soon as that is fixed, I can start using es4x + typescript 😁

pmlopes commented 2 years ago

@namdao2000 I understand, the only thing blocking me atm is that i keep the es4x releases aligned with the upstream vertx releases (because the generated code must match the underlying implementation).

Once there is a vert.x 4.2.5 or 4.3.0 i can release this. For the moment, the only way to hack this is to manually patch the offending file like you noticed.