typescript - 访问同级命名空间打字稿时遇到问题
问题描述
假设我有一个项目,我把它分成一堆部分,可能看起来像这样:
Project
Core
entity.ts
assert.ts
Math
matrix.ts
Assert.ts 可能如下所示:
namespace Project.Core {
export const assert = (expression: any, message: string) => !expression && console.error(message)
}
太好了,没有什么疯狂的事情发生,现在看看 entity.ts:
namespace Project.Core {
export class Entity {
id: string;
constructor(id: string) {
assert(!globalIDDatabase.has(id), "Entity with duplicate ID was created")
this.id = id;
}
}
}
这也有效,不需要导入任何东西,assert 在Project.Core
命名空间中,所以它会自动被拾取,但是如果我们首先在 matrix.ts 中添加如下:
namespace Project.Math {
class Matrix {
arr: number[][];
// Math stuff
}
}
然后尝试在 entity.ts 中使用它:
namespace Project.Core {
export class Entity {
id: string;
transform: Project.Math.Matrix;
constructor(id: string) {
assert(!globalIDDatabase.has(id), "Entity with duplicate ID was created")
this.id = id;
}
}
}
TSC 找到Project
命名空间,但它会在Math
子命名空间出错。如果我在文件顶部添加一个空白namespace Project.Math{}
,它会将问题转移到 Matrix 上,如下所示:
Property "Matrix" does not exist in namespace Project.Math
如何在保持这种结构的同时访问矩阵类?
解决方案
推荐阅读
- kotlin - 如何在kotlin中使数据类中的属性为非空
- javascript - 选择 JQuery 的下一个和上一个按钮有问题
- java - 如何从一个有效地保存特定参考值的类中找到一个字段?
- c# - MongoDb 驱动程序。通过 SCRAM-SHA-1 进行身份验证
- javascript - 在 SAP Cloud Foundry 上远程调试 node.js 应用程序
- android - Android BottomSheetBehavior 只允许向上拖动
- r - 如何使用 purrr:map 函数改变具有动态变量的多个列?
- php - 使用占位符时 PDO 慢执行()阶段
- c# - 在 Azure 表中重试批处理操作
- android - 每次我在 android studio 上运行我的项目时,我都会在模拟器上得到相同的输出