-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
"Cannot compile namespaces" error with --isolatedModules and no namespaces #15230
Comments
The error message needs to be clearer. instead of |
There should not be an error message in this situation. The --isolatedModules option should just turn off the internal module system completely, if tsc still errors out this way then the option didn't do its job. |
In #15839 I intended to modify the checking behavior but later realized that this check is just pointless and the ideal solution is to remove it.
Thus the check isn't doing anything in that (seemingly invalid) case, when it's supposed to complain. So why complain when the programmer isn't even using any namespace, doing nothing wrong? |
@wilonth The reason for this issue is that the error message is wrong: namespaces are perfectly fine if part of a (es6) module, but any non-module file should be an error. |
@andy-ms I was trying to point out that the whole code to check for this error is just pointless, and we should just remove it instead of complicating the code more. |
The semantics here is that a module is a file with at least one top-level import or export. it does not matter how you invoke the compiler. |
@mhegazy Yes and that rule is exactly the root of the problem we have here. The rule unfairly treats a file without import or export as a part of the legacy internal module system, so |
Yes, it will. The point of the The TypeScript codebase can't be successfully compiled through single-file transpilation. There is code like this everywhere: file1 namespace ts {
export var x = 10;
} file2 namespace ts {
var y = x; // transpiles to ts.x during whole-program compilation
} If we single-file transpiled file2, it would break. |
My bad, I screwed up my testing. I hereby retract my proposal. Thank you @RyanCavanaugh for reminding. |
So the solution should be: detect real namespace usage before throwing out this error message (i.e the "alternative" solution that @andy-ms proposed). We can't just change the text to something else because an error message in this case is totally wrong. |
|
Sorry for the necromancy, but surely this should, when targeting modules, fail with something like file1 var x = 10; file2 var y = x; Which also works when concatenating/globally evaluated and not with modules; nothing to do with Essentially, why does typescript bother guessing that files without When using I did notice that for some reason you can still use @DanielRosenwasser as an end user: what does "A global file" mean? I like the guidance though. Perhaps more like "Files without any import or export statements are not modules, and cannot be compiled with '--isolatedModules'. Add an 'export {}' statement if this file is only evaluated for it's side-effects." - though the language there doesn't seem very error message-y. |
I'm seeing same error for a simple js project. I have just created a sample js project below are the package and script files content. How can I resolve this issue? Package.json lib/script.js let card1 = "Ace of spades", console.log('Welcome to Blackjack!'); console.log("You are dealt: "); |
Any updates on this? |
I often find myself doing this in small tests.
|
I really want an option that causes TypeScript to consider every .ts file as an ES6 module, regardless of whether it contains import/export statements or not. I originally assumed the |
I wound up here while trying to fix a sort-of-related annoyance. Comments below are just to help any other VS Code users searching about this error with create-react-app and TypeScript. When configuring a proxy with create-react-app 2.x and TypeScript, you add a file named const proxy = require('http-proxy-middleware');
module.exports = function(app) { /* ... */ };
This works exactly as documented, but it has an annoying side effect. The Solution: Suppress the VS Code error message by excluding the {
// ...
"exclude": ["src/setupProxy.js"]
}
Alternative: Add a named export to `setupProxy.js: export const _ = ''; With a top-level export, it's now valid with --isolatedModules. |
Very often developers get this message because there are file that should be not included into copliation (gulp.js etc) |
ATTENTION - If you are using a create-react-app and you see this issue like I am... I was able to bypass this by making a change to my tsconfig.json file.... Changing the following "isolatedModules": true, to "isolatedModules": false, |
@UncleFifi And then the error moves from the type-check to the babel typescript transform, see the first caveat. @jtbennett you can just export nothing at all with |
Only if you're actually using namespaces. This issue is mainly about this error complaining about namespaces comes up when you're not even using namespaces and just happen to have a legal file with import/export. |
Right, sorry - it's been a while since I read this thread! That said, the reason they set |
@simonbuchan i've tried all the above before hand - no avail :( |
@simonbuchan Hmm okay so |
Here's a minimal repro case, using typescript@next (full repo at https://github.com/yang/sandbox-ts-namespaces-error): src/a.js: (notice, no import/export keywords needed) module.exports = "hello"; // you could really put anything here, e.g. console.log('hello'); tsconfig.js: {
"compilerOptions": {
"allowJs": true,
"isolatedModules": true,
"noEmit": true,
"strict": true
},
"include": ["src"]
} Error:
I too first encountered this via The workaround of adding .js files to your exclude set works when running tsc, but - annoyingly - you still see errors from those files in editors from the TS language service (tried both VS Code and Webstorm). (This seems to be a separate and more general issue with the TS language service not respecting excludes - but I wasn't able to find an existing issue for that.) To mask this error in the editors, I added .d.ts files for the *.js files. Furthermore, require-ing or import-ing the .js files renders the exclude ineffective as well, causing the error to be raised (outside your editor). Only disabling allowJs (against what create-react-app suggests) seems to be working for me. |
I keep getting this error for my web worker. I have tried excluding the individual file or folder and it isn't working for me. Is there no way to override this isolatedModules setting, CRA keeps rewriting the tsconfig.json. Any other suggestions? |
@jamespfarrell Put |
@Macil thanks for your advice. I get this error:
If I add:
I get:
|
Isn't there simply a way to bypass this 'isolatedModules' if not for one file, even for all files? |
Those errors you're getting now are unrelated to the isolatedModules setting. If you turned off isolatedModules, you'll still get those errors.
Where is this error happening? It sounds like you're trying to import the default export of a file that doesn't have one, like |
You're right it was something else @Macil ! thanks! |
Maybe somebody else will have the same issue: I had the same error appearing for a few files I've left as placeholders with no content in them but I've been importing them in other files, ie. I had empty styles.js file which was imported in another component file. |
I had this error for a module definition that I accidentally suffixed |
Same error. I am using CRA |
So I had this error because I had a
I had to switch it for this:
That fixed it, but I don't understand why, can someone explain it to me? My understanding was that including |
@thitemple Did you get that /* snippet from an article somewhere? The webpack manual seems to indicate that the first one is the correct one. I'm diagnosing a similar problem and came across your post. |
@vort3xxx I did not. I saw this post #15230 (comment) from @DaviSpindola and tried it. That was it |
Same problem, same solution. TS 3.5.2. |
Encountering the exact same issue. Did you ever figure out how to solve this? Defining Someone please help. How do I add custom declaration files to a create react app project? |
Just figured it out if anyone else is having the same issue. Simply add your declaration types to the |
问题得到解决,查看文章:https://blog.csdn.net/qingfeng812/article/details/120510673 |
If you're trying to test, you could just add this to your tsconfig.json.
and put all your tests in that folder |
TypeScript Version: nightly (2.3.0-dev.20170417)
Code
Expected behavior:
No error, or an error message about a non-module file in an
--isolatedModules
project.Actual behavior:
src/a.ts(1,1): error TS1208: Cannot compile namespaces when the '--isolatedModules' flag is provided.
This occurs because we in
program.ts
verifyCompilerOptions
to look for any source file that isn't an external module declaration, and fail on the first one, whatever it may be.The error message should be upgraded to reflect the real reason we issue this error, and not mention namespaces.
Alternately, we could just allow files without imports and actually look for a namespace before adding this error.
The text was updated successfully, but these errors were encountered: