I'm trying to go through typescript documentation to understand modules which are the same as ES6 modules.
typescript-modules - documentation for typescript modules. It says that
Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms. Conversely, to consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the import forms.
And
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
It says anything inside a file without import or export statements is available globally. But that's not true.
- folder
- script1.js
- script2.js
script1.js
var variable = "Hello";
script2.js
console.log(variable);
According to the statement written in the documentation, when I run script2.js, it should not give any error and console the value of the variable as script1.js does not have import, export statements and thus variable is available in global scope. But it gives an error. Then what does it mean by the script's contents are available in global scope?