What does the module keyword mean in typescript? What does the module keyword mean in typescript? angularjs angularjs

What does the module keyword mean in typescript?


The module keyword in TypeScript caused a bit of confusion, so it is going to be renamed namespace.

Rather than use namespaces, though, you can organise your code by files / file system (which means the actual file location will match the perceived namespace. This is how "exteral modules" (TypeScript) work - and also how ECMAScript 2015 modules work.

So with a quick adjustment, you'll have:

// file1.tsexport class MyClass {   constructor() { ... }   ...}

And then this will work:

import {MyClass} from 'file1';

If you compile targeting ES6, you'll notice that this line of code needs no translation, it matches the standard for module imports. If you target ES5 or lower, TypeScript will transform this statement for you (you can choose the transformation using the --module flag.

I tend to use the UMD compilation option, which means the output will work in web browsers (with RequireJS) or on Node. System JS is actually gaining a lot of traction currently, so you may want to consider that too. Eventually, browsers will simply natively support module loading.