是否可以在打字稿中将 * 导出为 foo [英] Is it possible to export * as foo in typescript
问题描述
我可以:
import * as foo from './foo'
但似乎无法导出相同的内容:
But can't seem to export the same:
export * as foo from './foo'
这似乎也不起作用...:
This doesn't seem to work either...:
import * as fooImport from './foo';
export const foo = fooImport;
有什么想法吗?
--- 更新---
你想达到什么目的?
基本上,我正在为我的应用程序实现一个 ngrx/store
后端.我想像这样组织我的代码:
Basically, I am working on implementing an ngrx/store
backend for my app. I want to organize my code like this:
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
文件来链接每个子集(通用范例)的所有导出.
And I want to use my index.ts
files to chain up all the exports from each subset (common paradigm).
但是,我想保持命名空间.我的每个 xxx-reducer.ts
和 xxx-actions.ts
文件都有相同名称的导出(reducer
、ActionTypes
, Actions
, ...) 所以正常的链接会导致名称冲突.我想要做的是允许 xxx-actions
和 xxx-reducer
的所有导出作为 re-exported>xxx
.这将使我能够:
However, I want to keep things namespaced. Each of my xxx-reducer.ts
and xxx-actions.ts
files have exports of the same name (reducer
, ActionTypes
, Actions
, ...) so normal chaining would result in a name collision. What I am trying to do is allow for all of the exports from xxx-actions
and xxx-reducer
to be re-exported as xxx
. This would allow me to:
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';
...
无论如何,这就是要点......
Thats the gist anyway...
推荐答案
TypeScript 3.8 或更高版本
请参阅https://stackoverflow.com/a/59712387/1108891了解详情.
是否可以在打字稿中将 * 导出为 foo
Is it possible to export * as foo in typescript
没有.但是,您可以使用两步流程:
Nope. You can, however, use a two step process:
src/core/index.ts
src/core/index.ts
import * as Foo from "./foo";
import * as Bar from "./bar";
export {
Foo,
Bar,
}
src/index.ts
src/index.ts
import { Foo, Bar } from "./core";
function FooBarBazBop() {
Foo.Baz;
Foo.Bop;
Bar.Baz;
Bar.Bop;
}
src/core/foo/index.ts 和 src/core/bar/index.ts
src/core/foo/index.ts and src/core/bar/index.ts
export * from "./baz";
export * from "./bop";
src/core/foo/baz.ts 和 src/core/bar/baz.ts
src/core/foo/baz.ts and src/core/bar/baz.ts
export class Baz {
}
src/core/foo/bop.ts 和 src/core/bar/bop.ts
src/core/foo/bop.ts and src/core/bar/bop.ts
export class Bop {
}
另见:https://www.typescriptlang.org/docs/handbook/modules.html
这篇关于是否可以在打字稿中将 * 导出为 foo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!