【问题标题】:Typescript exporting namespace defined in multiple files打字稿导出命名空间在多个文件中定义
【发布时间】:2016-08-31 23:57:16
【问题描述】:

我目前正在处理我的第一个 Typescript 项目。在浏览了官方文档并观看了一些课程(蛋头)之后,我认为是时候编写真正的代码了——而不仅仅是示例。

我正在使用:

  • typescript@rc (~2.0.2)
  • Visual Studio 代码 (~1.4.0)
  • Windows 10

所以我正在开发一个节点模块。我的问题是关于代码结构的。

这是我的项目的样子:

 src/
|____ core/
|    |____ core.ts
|    |____ components.ts
|    |____ systems.ts
|____ input/
|    |____ input.system.ts
|    |____ keyboard.ts
|____ app.ts

以下是每个文件的示例:

  • core.ts
/// <reference path="../core/components.ts" />

namespace Core {

    export interface IEntity {
        x: number
        y: number
        components: [Components.IComponent]
        update(dt: number): void
    }

    export class Entity implements IEntity {
        x: number
        y: number
        components: [Components.IComponent]
        update(dt: number): void{
            // do something with the coordinates
        }
    }
}
  • components.ts
namespace Components{
    export interface IComponent {
        update(dt: number): void
        // some other stuff here...
    }
}
  • systems.ts
namespace Systems{

    export interface ISystem{
        update(dt: number): void
        // some other stuff here...
    }

}
  • input.system.ts
/// <reference path="../core/systems.ts" />

namespace Systems{

    export class InputSystem implements ISystem{
        update(dt: number): void{
            // do something here
        }
        // some other stuff here...
    }

}
  • keyboard.ts
/// <reference path="../core/components.ts" />

namespace Components{
    export class Keyboard implements IComponent{
        update(dt: number): void{
            // do something here - Catch key up / down
        }
        // some other stuff here...
    }
}
  • app.ts
/// <reference path="./core/core.ts" />
/// <reference path="./core/components.ts" />
/// <reference path="./core/systems.ts" />

/// <reference path="./input/input.system.ts" />
/// <reference path="./input/keyboard.ts" />

export = {Core, Components, Systems}

我在这里要做的是拥有 3 个主要的命名空间核心、组件和系统。然后如果在另一个项目中导入了这个模块,我们可以这样做:

  • other.module.ts
// load module from node

import * as mymodule from "mymodule"

module A {
    class A extends mymodule.Core.Entity{
        constructor() {
            this.components.push(new mymodule.Components.Keyboard());
        }
    }

    export function main(): void{
       var systems: [mymodule.Systems.ISystem];

       systems.push(new mymodule.Systems.InputSystem());
       // other systems could be pushed Here

       for(var system in systems){
           system.update(0.5);
       }
    }
}

我得到的错误是在 app.ts 中,所有命名空间的编译器都说:

cannot re export name that is not define

我在做什么有问题吗?

我还想知道是否应该在 app.ts 中使用默认值导出?喜欢:

export default {Core, Components, Systems}

这对导入我的模块有帮助吗?

谢谢, 编。

【问题讨论】:

    标签: node.js typescript


    【解决方案1】:

    不确定我的问题是否足够清楚。不过我找到了解决方案,并希望得到您的意见。

    所以,我从文件中删除了所有命名空间。这意味着现在我所有的文件都是模块。

    然后在我的解决方案的根目录中创建三个文件:

    • _core.ts
    export * from "./core/core"
    
    • _components.ts
    export * from "./core/components" 
    export * from "./input/keyboard"
    
    • _systems.ts
    export * from "./core/systems" 
    export * from "./input/input.controller"
    

    现在在我的 app.ts 中,我可以像这样简单地导入这些新文件。

    import * as Core from "./_core"
    import * as Components from "./_components.ts"
    import * as Systems from "._/systems.ts"
    
    export = {Core, Components, Systems}
    

    你怎么看?这样做有什么潜在的问题吗?

    谢谢, 编。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 2016-03-08
      • 2019-03-04
      • 2016-11-05
      • 1970-01-01
      • 2017-02-09
      • 2018-07-26
      相关资源
      最近更新 更多