Skip to content
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

Generated types are incompatible with ECMAScript modules #793

Closed
benesjan opened this issue Nov 17, 2022 · 7 comments
Closed

Generated types are incompatible with ECMAScript modules #793

benesjan opened this issue Nov 17, 2022 · 7 comments

Comments

@benesjan
Copy link

benesjan commented Nov 17, 2022

Hello,
when having "module": "NodeNext" in tsconfig.json, I get the following error:
image

There doesn't seem to be an option to make typescript generate imports with file extensions. When I manually modify the typescript files and the extensions (.js or /index.js) to all the imports the issue disappears. Is there a way to enable this? If not would be great to have the support.

The repository I am working on is open-source so I can send a link if somebody would like to reproduce it.

Thank you

@benesjan benesjan changed the title Generated types are incompatible with nodenext moduleResolution Generated types are incompatible with ECMAScript modules Nov 21, 2022
@silasdavis
Copy link
Contributor

I have implemented support for this here: #799

@benesjan
Copy link
Author

benesjan commented Jan 8, 2023

@silasdavis Thank you! 👍

@benesjan benesjan closed this as completed Jan 8, 2023
@pablomendezroyo
Copy link

This should not be closed as long as the PR is still opened

@krzkaczor
Copy link
Member

Merged as: #840

@pablomendezroyo
Copy link

pablomendezroyo commented Jun 9, 2023

Im still running into the same issue: the .js extension not been added even with the flag --node16-modules flag provided

Typechain command

"typechain": "typechain --node16-modules --target ethers-v6 --out-dir ./src/typechain './truffle/contracts_build/contracts*/*.json'",

Multiple errors with the format:

src/typechain/index.ts:92:32 - error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './factories/contracts_v0.5/Owned__factory.js'?

92 export { Owned__factory } from "./factories/contracts_v0.5/Owned__factory";

Versions:

   "@typechain/ethers-v6": "^0.4.0",
   "typechain": "^8.2.0",

@pablomendezroyo
Copy link

For those running into this issue, I just execute a js script that add the .js extension to relative imports right after the typechain command as follows

&& node addExtensionJs.js

The script command: https://github.com/dappnode/toolkit/blob/main/addExtensionJs.js

@jmrossy
Copy link

jmrossy commented Apr 1, 2024

@krzkaczor The output when --node16-modules is enabled is almost correct but is still not valid ESM code. The output includes both direct exports and rolled up exports using the * as Foo syntax, which imports from barrel files that do not include the needed file extension. See here for an example: https://github.com/dethcrypto/TypeChain/pull/840/files#diff-a13cd542099b350a17167268ac727e5d459fae33143fd0a1baf4cbb77f191f51R5

The line should be export * as factories from "./factories.js", or preferably excluded altogether.

Is there a way to disable the * as exports which are unnecessary and undesirable as they reduce the effectiveness of tree shaking?

I see this is the same issue that was raised here and was supposedly fixed but I'm still seeing it use the latest typechain version

Edit: I've found the problem and submitted a PR to fix here: #898

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants