跨多文件的 Typescript 命名空间

标签 typescript typescript2.0

我尝试在单个命名空间但在多个文件中编写 typescript 代码。但是当我在 AAA 类中使用类 config 时,Config 没有定义,我有语法错误。为什么?

检查这个例子:

文件:services/Config/index.ts:

export namespace Services {
    export class Config {
      /* class methods */
    }
}

文件:services/AAA/index.ts:

/// <reference path = "./../Config/index.ts" />
export namespace Service {
    export class AAAA {
      private config = new Config(); <-- Error is here
      /* class methods */
    }
}

最佳答案

您不导出命名空间:

namespace Services {
 export class Config {
  /* class methods */
 }
}

/// <reference path = "../Config/index.ts"/> 
namespace Service {
 export class AAAA {
  private config = new Config(); <-- Error is here
  /* class methods */
 }
}

https://www.typescriptlang.org/docs/handbook/namespaces.html

关于跨多文件的 Typescript 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521789/

相关文章:

typescript : `Parameters<F>` 如何保留参数的名称?

javascript - Typescript + Angular 公开服务方法

typescript - commonjs 的单个 .js 和 .d 文件

字符串文字的 TypeScript 类型推断问题

typescript - typescript的不同导入导出形式

javascript - 使用类验证器中的 @MinDate 会导致 TypeError : Cannot read property 'getTime' of undefined?

Angular 7 - HTML5 全屏 API 如何工作?我有很多错误

angular - 错误 TS2300 : Duplicate identifier 'export='

typescript - 类型中缺少 Object.entries 属性 'length'

javascript - 来自 webpack 的 typescript 中未定义全局变量