long.js icon indicating copy to clipboard operation
long.js copied to clipboard

fix type in umd module export

Open fs-eire opened this issue 2 years ago • 8 comments

import Long from "../index.js";
export = Long;

previous code generates errors complaining about cannot import from ESmodule to commonJS. a workaround is to duplicate the content of index.d.ts

node_modules/long/umd/index.d.ts:1:18 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("../index.js")' call instead.

1 import Long from "../index.js"; ~~~~~~~~~~~~~

Found 1 error in node_modules/long/umd/index.d.ts:1

fs-eire avatar Jul 14 '23 19:07 fs-eire

this may not be the best solution because it duplicates the index.d.ts. However I tried a few ways and this is the only one that works to me.

fs-eire avatar Jul 14 '23 23:07 fs-eire

This fixed the problem for me.

Silventino avatar Aug 21 '23 02:08 Silventino

This fixes my build too. What I am struggling to understand is why this is posing a problem only now, i've been running this code fine the past months

tzvc avatar Aug 24 '23 07:08 tzvc

any news about it? it should be merged and populated to npm

pumano avatar Oct 06 '23 09:10 pumano

It has been about 4 months since this PR is out and as you can see we really need this change to be merged and publish to NPM so that a lot of users (#124, #125) will be happy.

I understand that as the code owner you may have some concerns about this PR (eg. duplicated source file for the .d.ts). How about we apply this fix first and then try to improve it later? A not-so-good solution is better than none.

fs-eire avatar Nov 07 '23 23:11 fs-eire

@fs-eire @henriquecarv I think if you invert the import dependencies this should work without duplication:

// umd/index.d.ts

declare class Long {
  // ...
}

export = Long;
// index.d.ts

import Long from './umd/index.js';

export default Long;

alecgibson avatar Jan 19 '24 16:01 alecgibson

Oh never mind I see that's what they've proposed in https://github.com/dcodeIO/long.js/pull/130

Would recommend closing this PR in favour of that one.

alecgibson avatar Jan 19 '24 16:01 alecgibson

@fs-eire this is not work for me when I use protobuf.

case 4:
  if (tag !== 32) {
    break;
  }

  message.int64Field = longToNumber(reader.int64() as Long);
  continue;
src/tdo/type_test.ts:228:63 - error TS2709: Cannot use namespace 'Long' as a type.

228           message.int64Field = longToNumber(reader.int64() as Long);

bobcyw avatar Jul 02 '24 00:07 bobcyw