是否可以在打字稿中将 * 导出为 foo
Is it possible to export * as foo in typescript
我可以:
import * as foo from './foo'
但似乎无法导出相同的内容:
export * as foo from './foo'
这似乎也行不通...:[=27=]
import * as fooImport from './foo';
export const foo = fooImport;
有什么想法吗?
---更新---
What are you trying to achieve?
基本上,我正在为我的应用程序实施 ngrx/store
后端。我想这样组织我的代码:
app/core/
index.ts
viewer/
index.ts
viewer-actions.ts
viewer-reducer.ts
view-data/
index.ts
view-data-actions.ts
view-data-reducer.ts
我想使用我的 index.ts
文件将每个子集的所有导出链接起来(通用范例)。
但是,我想保留一些东西命名空间。我的每个 xxx-reducer.ts
和 xxx-actions.ts
文件都有相同名称的导出(reducer
、ActionTypes
、Actions
、...)所以正常的链接会导致名称冲突。我想要做的是允许 xxx-actions
和 xxx-reducer
的所有导出 重新导出 为 xxx
。这将使我能够:
import { viewer, viewerData } from './core';
...
private viewer: Observable<viewer.Viewer>;
private viewData: Observable<viewData.ViewData>;
ngOnInit() {
this.viewer = this.store.let(viewer.getViewer());
this.viewData = this.store.let(viewData.getViewData());
}
而不是更冗长的:
import * as viewer from './core/viewer';
import * as viewerData from './core/viewer-data';
...
这就是要点...
TypeScript 3.8 或更高版本
详情见。
TypeScript 3.7 或更早版本之前
Is it possible to export * as foo in typescript
没有。但是,您可以使用两步过程:
src/core/index.ts
import * as Foo from "./foo";
import * as Bar from "./bar";
export {
Foo,
Bar,
}
src/index.ts
import { Foo, Bar } from "./core";
function FooBarBazBop() {
Foo.Baz;
Foo.Bop;
Bar.Baz;
Bar.Bop;
}
src/core/foo/index.ts and src/core/bar/index.ts
export * from "./baz";
export * from "./bop";
src/core/foo/baz.ts and src/core/bar/baz.ts
export class Baz {
}
src/core/foo/bop.ts and src/core/bar/bop.ts
export class Bop {
}
另请参阅:https://www.typescriptlang.org/docs/handbook/modules.html
由于 TypeScript 3.8 已经发布,您可以为您的导出添加别名。
示例:
export * as utilities from "./utilities.js";
参考:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html
我可以:
import * as foo from './foo'
但似乎无法导出相同的内容:
export * as foo from './foo'
这似乎也行不通...:[=27=]
import * as fooImport from './foo';
export const foo = fooImport;
有什么想法吗?
---更新---
What are you trying to achieve?
基本上,我正在为我的应用程序实施 ngrx/store
后端。我想这样组织我的代码:
app/core/
index.ts
viewer/
index.ts
viewer-actions.ts
viewer-reducer.ts
view-data/
index.ts
view-data-actions.ts
view-data-reducer.ts
我想使用我的 index.ts
文件将每个子集的所有导出链接起来(通用范例)。
但是,我想保留一些东西命名空间。我的每个 xxx-reducer.ts
和 xxx-actions.ts
文件都有相同名称的导出(reducer
、ActionTypes
、Actions
、...)所以正常的链接会导致名称冲突。我想要做的是允许 xxx-actions
和 xxx-reducer
的所有导出 重新导出 为 xxx
。这将使我能够:
import { viewer, viewerData } from './core';
...
private viewer: Observable<viewer.Viewer>;
private viewData: Observable<viewData.ViewData>;
ngOnInit() {
this.viewer = this.store.let(viewer.getViewer());
this.viewData = this.store.let(viewData.getViewData());
}
而不是更冗长的:
import * as viewer from './core/viewer';
import * as viewerData from './core/viewer-data';
...
这就是要点...
TypeScript 3.8 或更高版本
详情见
TypeScript 3.7 或更早版本之前
Is it possible to export * as foo in typescript
没有。但是,您可以使用两步过程:
src/core/index.ts
import * as Foo from "./foo";
import * as Bar from "./bar";
export {
Foo,
Bar,
}
src/index.ts
import { Foo, Bar } from "./core";
function FooBarBazBop() {
Foo.Baz;
Foo.Bop;
Bar.Baz;
Bar.Bop;
}
src/core/foo/index.ts and src/core/bar/index.ts
export * from "./baz";
export * from "./bop";
src/core/foo/baz.ts and src/core/bar/baz.ts
export class Baz {
}
src/core/foo/bop.ts and src/core/bar/bop.ts
export class Bop {
}
另请参阅:https://www.typescriptlang.org/docs/handbook/modules.html
由于 TypeScript 3.8 已经发布,您可以为您的导出添加别名。
示例:
export * as utilities from "./utilities.js";
参考:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html