-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Why these import/export statements behave differently...? #4770
Comments
Don't use them with es6 / commonjs / amd / system modules as they are not needed.
Don't use
Looks like a bug. Are you sure there are no errors? |
Thanks for answering.
I haven't used namespaces so far but why they aren't needed?
Okay I won't use it but why there's differences in behaviour? why do I need to go through additional scope to access the class/module? whereas in CommonJS style I don't. I'm not using ES6, I'm compiling to ES5.
I'm pretty sure I'm not getting errors! it's compiling. Here is the tsconfig.json file I'm using for this project. {
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"inlineSourceMap": true,
"experimentalDecorators": true,
"target": "es5",
"module": "commonjs",
"outDir": "./.out"
}
} |
Because if you have a root level export or import in your file ... your file gets a local scope. You are no longer polluting the global namespace and hence your file is already a module. You don't need to needlessly nest stuff inside again by using
Not a choice made by TypeScript. Rather by the ES6 committee. Its for static analysis : http://www.2ality.com/2014/09/es6-modules-final.html Note that it does not reflect opinions of the TypeScript team ... rather the ECMAScript committee.
Yup. Bug reported : #4772 |
The rational is quite odd here because when you have multiple functions in a single file then there's no doubt that it's reasonable to create a scope to contain all the functions in the file as if the file itself was somewhat a class However, when it comes down to classes it's very different, people don't tend to put many classes in a single file like you would with functions and in fact there's a consensus to have one class per file and many guidelines for many programming languages follow this tenet. Again, like I said if it was possible to import many files into the same scope then it was working fine because then we could split related classes into different files like we would normally do in other languages which would solve this problem. I guess I need to create a different post/proposal for this feature though but I'm not sure how to structure my project now, any ideas what's the best way to approach this? |
NPM / commonjs / Webpack all things .... at least that's my moto 👍 🌹 |
haha.. ;) Never mind! I read things more thoroughly and I found exactly what I was looking for!
|
Yup. That is my preferred syntax even when targeting commonjs/NPM. |
Why these import/export statements behave differently? I mean I understand they were originated in two different specs but should they behave differently?
What bothers me is that when I'm using the ES6 style to import the class I need to access it through another scope (module/namespace), is there a way to avoid it? or should I just use CommonJS modules style?
It was probably fine if I could import many classes/modules to the same scope.
Just another variation with modules.
Versus the following statement:
I'm not sure what the following does but it compiles, is it a bug?
The text was updated successfully, but these errors were encountered: