Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Modules and Namespaces in TypeScript/JavaScript?

I tried to compile the following code in TypeScript to JavaScript:

namespace MyNamespace {
    class MyClass {
        public test() {
            return 1;
        }
    }
}

If I replace the keyword namespace with module, it still generates exactly the same JavaScript code. So what is the difference between a module and namespace and when should I choose one over another?

like image 896
James Smith Avatar asked Jul 17 '26 06:07

James Smith


1 Answers

So what is the difference between a module and namespace and when should I choose one over another

They are both exactly the same. module causes confusion with Native JavaScript (e.g ES6 module) so they decided to rename it to namespace.

In new code prefer to use namespace.

like image 187
basarat Avatar answered Jul 19 '26 20:07

basarat