Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between namespaces and modules in typescript

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?

like image 483
Mauro Valvano Avatar asked May 09 '16 15:05

Mauro Valvano


People also ask

Is a module a namespace?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.

What is namespace in TypeScript?

The namespace is used for logical grouping of functionalities. A namespace can include interfaces, classes, functions and variables to support a single or a group of related functionalities. A namespace can be created using the namespace keyword followed by the namespace name.

What is a module in TypeScript?

In TypeScript, a module is a file containing values, functions, or classes. You can make some of them public, i.e. visible from other modules, by exporting them. Non exported objects are private.

Should you use namespaces in TypeScript?

TypeScript is an extension of the JavaScript language that uses JavaScript's runtime with a compile-time type checker. In TypeScript, you can use namespaces to organize your code. Previously known as internal modules, namespaces in TypeScript are based on an early draft of the ECMAScript modules.


2 Answers

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

like image 106
Искрен Станиславов Avatar answered Sep 20 '22 08:09

Искрен Станиславов


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.

like image 26
toskv Avatar answered Sep 19 '22 08:09

toskv