class - 使用来自不同文件的类导出 TypeScript 模块

标签 class typescript module export systemjs

关于如何将带有类的 TypeScript 模块拆分到单独的文件中有几个问题,但到目前为止还没有解决方案适用于我的问题。

我有一个模块 (store),其中包含两个类 (Person & SerialisedRecord)。当两个类都在一个文件中时,编译和导出工作正常。

现在我想为每个类(Person.ts & SerialisedRecord.ts)创建一个文件,遵循我已有的相同导出模式。但我不知道如何实现这一目标。

这是我的初始情况:

store.ts

export module store {

  export class Person {
    public fullName: string = '';

    constructor(firstName: string, lastName: string) {
      this.fullName = `${firstName} ${lastName}`;
    }
  }

  export class SerialisedRecord {   
    constructor(public serialised: string, public id: string) {}
  }

}

当我将 store.ts 编译为 store.js (ES5) 时,我得到了我想要的(一个 SystemJS 模块在一个模块中导出两个类):

System.register([], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var store;
    return {
        setters:[],
        execute: function() {
            (function (store) {
                var Person = (function () {
                    function Person(firstName, lastName) {
                        this.fullName = '';
                        this.fullName = firstName + " " + lastName;
                    }
                    return Person;
                }());
                store.Person = Person;
                var SerialisedRecord = (function () {
                    function SerialisedRecord(serialised, id) {
                        this.id = id;
                        this.serialised = serialised;
                    }
                    return SerialisedRecord;
                }());
                store.SerialisedRecord = SerialisedRecord;
            })(store = store || (store = {}));
            exports_1("store", store);
        }
    }
});

现在我试着这样做:

export module store {
  export {Person} from "./Person";
  export {SerialisedRecord} from "./SerialisedRecord";
}

但它没有告诉我:

error TS1194: Export declarations are not permitted in a namespace.

你能告诉我我做错了什么吗?

这是我的tsconfig.json:

{
  "compilerOptions": {
    "module": "system",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "noImplicitReturns": true,
    "removeComments": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
  ]
}

最佳答案

如果您删除商店模块,效果会很好:

商店.ts:

export { Person } from "./Person";
export { SerialisedRecord } from "./SerialisedRecord";

索引.ts:

import { Person, SerialisedRecord } from "./store";

let p = new Person("first", "last");

编辑

如果你必须保留命名空间结构,你可以尝试这样的事情:

import Person from "./Person";
import SerialisedRecord from "./SerialisedRecord";

export default {
    store: {
        Person,
        SerialisedRecord
    }
}

关于class - 使用来自不同文件的类导出 TypeScript 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40217131/

相关文章:

c++ - 如何通过在 C++ 中创建对象来将参数传递给类?

typescript - 为什么永远不能分配给每种类型?

typescript - 带有 typescript 的 Mongoose :Object is possibly null

windows - 使用 Strawberry Perl 5.16.2.2(64 位)安装模块

vba - 在 VBA 中链接类

java - 有没有读取UNIX/etc/group文件的Java包?

javascript - 测试子类时如何监视祖父类中的导入函数?

javascript - 使用 Observables 保持对 JSON-API 的嵌套 http 调用之间的关系

powershell - 找不到 Microsoft.Powershell.LocalAccounts 模块(或运行 Get-LocalUser)

powershell - 导入模块时出错