-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. #2919
Comments
Not entirely sure why, but using |
Thanks for bringing this up. So in Lines 3 to 5 in 7950d9c
For reference: https://stackoverflow.com/questions/48708410/typescript-error-an-export-assignment-cannot-be-used-in-a-module-with-other-exp Anyone able to see if we can/should improve on this? |
No worries :) Not sure, I've tried the approach from the answer: import { Decimal } from 'decimal.js'
type NoLiteralType<T> = T extends number
? number
: T extends string
? string
: T extends boolean
? boolean
: T
declare module "math" {
namespace math {
//...
}
export = math;
} And it resulted in the same error as in OP: |
Also npm packages like |
Thanks for giving it a try Maxim. From what I understand from it, we should get rid of the |
Hey @josdejong Option 1 potentially breaks integrations and changes public API. Option 2 is safer, but ideally requires automation. I couldn't find a way to replicate previous behavior, my guess is that such broad ambiguous exports are not in ESM philosophy so likely aren't supported. I just ran a few regexes to extract exports, two of them trigger eslint errors, and there's also |
This also might help: https://arethetypeswrong.github.io/?p=mathjs%4011.8.0 |
Hmm. So both potential solutions are not ideal yet. Thanks for working them out! Do you have any understanding on why Would it work to remove the namespace and only export the interface |
I'm sorry, I've completed the transition to CJS from ESM because NX doesn't have proper support for ESM yet, so this issue is no longer relevant to me, and a good solution isn't apparent to me, so I hope someone else can pick it up from here, unsubscribing for now, cheers! |
Good to hear you have a solution for your case. Thanks for your work on solving this! I'll close your two PR's for the time being until someone can pick this up. |
I will try to pick this up. Let's hope that I find a solution. |
Thanks for your offer @nalply ! Maxim did explore a couple of approaches already, that may serve as a good start. |
Option 2 compiles fine for me, however the generated file in lib/esm/type/complex/Complex.js has SyntaxError: ambiguous indirect export: default, because of this line: https://github.com/josdejong/mathjs/blob/develop/src/type/complex/Complex.js#L1 I will need to clone mathjs and tinker a bit. Please tell me if I am barking up the wrong tree. |
Option 2 seems to work for me, but I had to solve the problems with complex.js and fraction.js first to continue. However my use case is different again: I want to program in TypeScript for the browser without bundling. I have some free time to develop an RPN calculator., it's a hobby. Because I hate bundling, I am stubborn in not allowing bundling during development. I am using only a small part of mathjs, so I won't give a recommendation to use option 2. And the problem with "ambiguous indirect export": it is because Firefox expected an ES module and the Node trick to import CommonJS into ES module does not work in the browser. I cloned fraction.js and made it an ES module by making the IIFE return the class and having I know that this change is a breaking one and won't suggest it. But for my RPN calculator this is a quick, nice change. EDIT: Update what I did |
🤔 hm are you trying to load non-bundled code in the browser? I'm not sure but I guess there are two different issues in play then: one is the |
Yeah! I forked complex.js and fraction.js yesterday and all is well, development has been ensuing since then rather smoothly. I am using https://modern-web.dev/docs/dev-server/overview/ which compiles TypeScript on the fly, and I hacked a small plugin to do some more on-the-fly processing and this makes me happy. YOLO. |
😂 👍 |
As a temporary workaround until this issue is fixed, one way we were able to solve it was by doing this. We're suppressing the typescript error, but the types will still show up correctly. // @ts-expect-error suppress typescript errors until issue is resolved https://github.com/josdejong/mathjs/issues/2919
const mathjs: typeof import('mathjs') = require('mathjs') |
This is partly because TypeScript expects you to ship two different .d.ts files, one for cjs, and one for esm.
As of now, the package.json looks like this:
It should look like this:
with the
with No other changes are necessary. |
O wow, that easy? That would be great! @hfhchan-plb so can the I've prepared a PR in #3021, anyone able to have a look or even better: verify whether this PR solves the issue? |
Yes. The
No. If you do that, then TS will start throwing errors. 🤷 |
LGTM |
Thanks for checking it out! Will merge it soon. |
I've done some testing with #3021, having alternative entry points in the package.json file. This does not solve the issue unfortunately. So I think we should resort to one of the options worked out by Maxim, see #2919 (comment)). I think the second option is best since it does not need any special tricks: it is just the dumb way to define that function I've now created an new PR #3079 using the approach of option 2 of Maxim, and added documentation. Still need to do testing and some last thingies. Feedback would be welcome. |
Fixed now in |
Describe the bug
Getting the following error:
To Reproduce
I have
"type": "module"
in mypackage.json
andin my TSConfig, using TS v5.0.2, it used to work with TS v4.9.5
The text was updated successfully, but these errors were encountered: