从不同的文件导出带有 类 的 TypeScript 模块
Export a TypeScript module with classes from different files
关于如何将带有 classes 的 TypeScript 模块拆分到单独的文件中有几个问题,但到目前为止还没有解决方案适用于我的问题。
我有一个模块 (store
),其中包含两个 classes (Person
& SerialisedRecord
)。当两个 classes 在一个文件中时,编译和导出工作正常。
现在我想为每个 class(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 模块在一个模块中导出了两个 classes):
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"
]
}
如果删除商店模块,效果会很好:
store.ts:
export { Person } from "./Person";
export { SerialisedRecord } from "./SerialisedRecord";
index.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
}
}
关于如何将带有 classes 的 TypeScript 模块拆分到单独的文件中有几个问题,但到目前为止还没有解决方案适用于我的问题。
我有一个模块 (store
),其中包含两个 classes (Person
& SerialisedRecord
)。当两个 classes 在一个文件中时,编译和导出工作正常。
现在我想为每个 class(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 模块在一个模块中导出了两个 classes):
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"
]
}
如果删除商店模块,效果会很好:
store.ts:
export { Person } from "./Person";
export { SerialisedRecord } from "./SerialisedRecord";
index.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
}
}