Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare module vs declare namespace typescript

I am a little confused about the difference between declare module and declare namespace in typescript internal modules.

Another point which I am not clear: is it a good idea to have complex namespaces to typescript internal modules.

e.g.

declare module com.xyz.company.test.mynamespace {
    interface Iabc {
        // stuff  
    }

    interface Ixys{ 
       //stuff 
    }

    // or

    export interface Ipqr { 
        // stuff 
    }
}
like image 958
Ankur Soni Avatar asked Apr 15 '16 06:04

Ankur Soni


People also ask

Should I use TypeScript namespace?

Don't use Custom TypeScript Modules and Namespaces Since we have ES6 modules as a standard in JavaScript, we don't need custom TypeScript modules and namespaces to organize our code. Instead, we should use standard JavaScript modules with import and export instead.

What is declare module in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...

What is declare namespace in TypeScript?

The namespace is a way which is used for logical grouping of functionalities. It encapsulates the features and objects that share common relationships. It allows us to organize our code in a much cleaner way. A namespace is also known as internal modules.

What is the difference between namespace and module in TypeScript?

A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping.


1 Answers

I found this [1] handbook on namespaces and modules. First of all this clarification about terms:

A note about terminology: It’s important to note that in TypeScript 1.5, the nomenclature has changed. “Internal modules” are now “namespaces”. “External modules” are now simply “modules”, -

So, from the same book, different chapter [2] example of both:

Module

  interface StringValidator {
      isAcceptable(s: string): boolean;
  }
  ..
   // Validators to use
  let validators: { [s: string]: StringValidator; } = {};

Namespace

  namespace Validation {
      export interface StringValidator {
          isAcceptable(s: string): boolean;
      }
   ...
    // Validators to use
    let validators: { [s: string]: Validation.StringValidator; } = {};

Source [3] tells about their usage:

Namespaces that aren't in an external module can span files, so if you're in the same namespace you can refer to anything exported by the namespace without needing any sort of import.

This is case, where you maybe could use namespace but there are mentions in [4] this comparison:

Module:

   //typescript
   export class Validaton { 
       ... 
   }

   //becomes, in javascript:
   export class Validation {
       ... 
   }

Namespace:

   // typescript:
   namespace Validation {
       const foo = 123;
   }

  //This becomes, in es6:
  (function(Validation) {
         Validation.foo = 123;
   })(Validation || (Validation = {}))

and as you see, the second one becomes in JavaScript ES6 quite unnatural and thus another answer on [3] even states namespaces obsolete.


About second question:

Source [1] tells about a case, where overly using namespaces makes code look little bit uselessly complicated:

   import * as shapes from "./shapes";
   let t = new shapes.Shapes.Triangle(); // shapes.Shapes?

So, this makes me think: all useless or less meaningful for the code namespace levels should be avoided. The writer even says:

A key feature of modules in TypeScript is that two different modules will never contribute names to the same scope. Because the consumer of a module decides what name to assign it, there’s no need to proactively wrap up the exported symbols in a namespace.


Source:

[1] https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html

[2] https://www.typescriptlang.org/docs/handbook/namespaces.html#validators-in-a-single-file

[3] Module vs Namespace - Import vs Require Typescript

[4] https://michelenasti.com/2019/01/23/is-typescript-namespace-feature-deprecated.html

like image 179
mico Avatar answered Nov 05 '22 17:11

mico