typescript - 声明模块与声明命名空间 typescript

标签 typescript typescript1.5

我对 declare module 之间的区别有点困惑和 declare namespace在 typescript 内部模块中。

我不清楚的另一点:将复杂的 namespace 用于 typescript 内部模块是否是个好主意。

例如

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

    interface Ixys{ 
       //stuff 
    }

    // or

    export interface Ipqr { 
        // stuff 
    }
}

最佳答案

我找到了这本 [1] 关于命名空间和模块的手册。首先对术语进行澄清:

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”, -



因此,来自同一本书,不同章节 [2] 的示例:

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

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

来源 [3] 讲述了它们的用法:

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.



在这种情况下,您可能可以使用命名空间,但在 [4] 中提到了这个比较:

模块:
   //typescript
   export class Validaton { 
       ... 
   }

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

命名空间:
   // typescript:
   namespace Validation {
       const foo = 123;
   }

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

如您所见,第二个在 JavaScript ES6 中变得非常不自然,因此 [3] 上的另一个答案甚至表明命名空间已过时。

关于第二个问题:

Source [1] 讲述了一个案例,过度使用命名空间使代码看起来有点无用的复杂:
   import * as shapes from "./shapes";
   let t = new shapes.Shapes.Triangle(); // shapes.Shapes?

所以,这让我想到:应该避免所有对代码命名空间级别无用或意义不大的事情。作者甚至说:

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.



来源:

[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

关于typescript - 声明模块与声明命名空间 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36640005/

相关文章:

angular - 类型错误 : Cannot read property 'get' of undefined Ionic 3/Angular 5

typescript - 使用 npm run 时是否使用了不同版本的 tsc?

windows - 为什么在 Mac 上编译 TypeScript 比在 PC 上快得多?

typescript - 我怎样才能键入提示一个类,而不是类的一个实例?

angularjs - 如何使用 Typescript 1.5 和 AngularJS 1.4 实现模块?

angular - 为什么大多数 Angular UI 组件不使用对象来配置输入?

arrays - 在 TypeScript 中,我如何访问数组的成员?

javascript - 按多个属性进行无点分组

angularjs - 为什么此代码不会导致 TypeScript 类型错误?