Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Modules: Sustainability of TypeScript internal modules & Bundles

Full names of classes in TypeScript

TypeScript 1.X allows the developer to use internal modules as namespaces.

module MyProject.MySubProject {
    export class A {
    }
    export function f() {
        var a1 = new A(); // valid
        var a2 = new MySubProject.A(); // valid too
        var a3 = new MyProject.MySubProject.A(); // valid too, again
    }
}
MyProject.MySubProject.f();
var a4 = new MyProject.MySubProject.A(); // ... and valid

When transpiled to ES5, this code creates an object MyProject that contains an object MySubProject that contains two functions A and f.

From inside the module, a class can be designed with a relative name (A, MySubProject.A) or the full name (MyProject.MySubProject.A). From outside the module, the exported class is used with the full name.

But nested modules no longer exists in ES6. And TypeScript is going to align to the ES6 modules.

Are the full names of classes (MyModule1.MyModule2.MyClass) compatible to ES6?

If we want to use namespaces in the old way, can we declare an ES6 class in a closure or an object?

Bundles and ES6 modules

Here is a similar code in the ES6 way:

//------ MyProject/MySubProject.js ------
export class A {
}
export function f() {
    var a1 = new A(); // valid
}

//------ main.js ------
import { A, f } from 'MyProject/MySubProject';
f();
var a2 = new A();

After concatenation of the two JavaScript files into a single minified file, how to operate the import?

like image 809
Paleo Avatar asked Oct 31 '22 09:10

Paleo


1 Answers

TypeScript is only going to align with ES6 modules as a new sytanx based on TypeScript external modules. This is based off of what work is being done on GitHub. Current internal modules do not conflict with the new syntax and therefore will continue to work as is.

like image 81
basarat Avatar answered Nov 09 '22 06:11

basarat