typescript - 访问 "root"模块与实际子模块同名

标签 typescript

我在 TypeScript 中有两个模块:

module superModule {
  export interface myInterface { /*...*/ }
}

module app.superModule {
  var x: superModule.myInterface;
}

请注意,有两个不同的“superModule”模块。第一个位于根目录,第二个作为应用程序模块的子模块。

上面的代码被评估为缺少“myInterface”接口(interface),因为编译器仅在 app.superModule 中搜索。

如何从子模块 superModule 访问“root”superModule?

TypeScript Playground :http://goo.gl/BdOJ1D

最佳答案

TypeScript 中没有“global”关键字或等效关键字,但您可以使用 import 为类型或变量创建别名:

module superModule {
  export interface myInterface { /*...*/ }
}

// Can be placed anywhere where the top definition
// of 'superModule' is visible
import superModule_myInterface = superModule.myInterface;
module app.superModule {
  var x: superModule_myInterface;
}

关于typescript - 访问 "root"模块与实际子模块同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653984/

相关文章:

typescript - 如何使用 CDK 正确配置 CORS 的 APIGateway

typescript - 如何将语法 "inputs"属性更改为 "@Input"属性装饰器

typescript - 是否已经可以在 react-native 中使用顶级 await?

javascript - 类型 'number' 不可分配给类型 'Date' - Typescript 未编译

javascript - TypeScript:在构造函数内部和外部声明变量有什么区别?

javascript - 使用 create-react-app 通过 JEST 运行测试时不显示 TypeScript 错误

typescript - 选中/未选中时如何选中/取消选中ion-select中的所有选项选择ionic3中的所有选项

javascript - 我想跳过对象中的最后一个属性并将其值分配给上一个属性

Typescript 生成用于简单类继承的 javascript 代码

typescript - 是否可以在 typescript 中递归地将一种类似 JSON 的类型映射到另一种类型?