TypeScript modules work like ES2015/2017 modules. At the end of the day, modules are files and they're loaded asynchornously.
In the other hand, modules create an scope like the manual module pattern implementation:
Foo = (function() {
var export = {};
export.x = 11;
})();
You need to use import
because a function scope won't import other files' (i.e. modules) variables, functions, ... classes by default: there should be imported manually.
When you need something from other module, that operation is equivalent to this:
// File1.js
Foo = (function(export) {
export.x = 11;
return export;
})(Foo || {});
// File2.js
Foo = (function(export) {
// You access the "x" variable exported from the other file on the Foo module
export.pow2 = function() {
return export.x * export.x;
};
return export;
})(Foo || {});
At the end of the day, you can't expect the behavior of a Java *.jar
or .NET assembly *.dll
, because these runtime environments run the applications or systems on a local machine and the modules are linked during runtime based on hints to local paths. For example, when B.dll
requires A.dll
at some point, because some class derives a class defined in A.dll
, the runtime will load the assembly A.dll
into memory.
Sadly, this mechanism isn't still available on JavaScript and TypeScript is transpiled into that target language for now, because your modules are just code files, they're not byte code or an intermediate language.
Maybe there's a hope with WebAssembly!