20

I've used typescript from some months now and i have not understand the difference from namespaces and modules yet.

I know that before they were named internal and external modules, but with both i can import classes from other files. So what is the real difference?

Mauro Valvano
  • 903
  • 1
  • 10
  • 21
  • 3
    [Namespaces and Modules](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Namespaces%20and%20Modules.md) – Nitzan Tomer May 09 '16 at 15:14
  • As I understand it namespaces used to be called "modules" and came in TypeScript before ECMAScript had come up with a module standard. Now that ES has a module standard TS renamed their "modules" to "namespaces" and added support for ES modules. I don't think there's any real reason to use namespaces going forward. – Aaron Beall May 09 '16 at 15:15
  • 2
    Reading the "Namespace and Modules" from @Nitzan Tomer confused me more. So, do you think that i must use only modules? – Mauro Valvano May 09 '16 at 15:29
  • I have no idea what you want to do so I can't possibly answer what you should use. I think that that page explains the difference in a pretty straight forward way. If you need modules in the sense of loading them dynamically (using dependencies) and you use a module loader (require for example) then you need to go with modules. If however you want to have global variables with structure then you should go with namespaces. – Nitzan Tomer May 09 '16 at 16:17
  • 2
    it's funny that there still doesn't exist a simple to understand explanation of the difference and a guideline which one should be used. – tkit Jun 14 '16 at 10:01
  • Does this answer your question? [What's the difference between internal and external modules in TypeScript?](https://stackoverflow.com/questions/12841557/whats-the-difference-between-internal-and-external-modules-in-typescript) – Yogesh Umesh Vaity Aug 14 '20 at 15:39

2 Answers2

7

As it is stated in the TS-handbook there are 2 kind of modules: "internal" & "external". The code in the internal module is written in Typescript and the "external" is written in Javascript.

In order to align with new ECMAScript 2015's terminology they decided to rename them as follows:

  1. "Internal modules" are now "namespaces".
  2. "External modules" are now simply "modules", as to align with ECMAScript

So:

  • The way you write your code is different
  • When using modules the classes are not exposed in the global scope, while using namespaces:

Example:

Let's say you have public namespace sequence NamespaceA.NamespaceB.NamespaceC which exposes public class ClassD. You can access all of these globally this way:

window.NamespaceA
window.NamespaceA.NamespaceB
window.NamespaceA.NamespaceB.NamespaceC
window.NamespaceA.NamespaceB.NamespaceC.ClassD

without saying window.NamespaceA = NamespaceA

and if you use modules you have to use the "magic" above

3

Namespaces are TypeScript's way of structuring code when you don't want the outputed Javascript code to use a module loader.

You can find more about namespaces vs modules in the handbook here.

toskv
  • 30,680
  • 7
  • 72
  • 74