typescript 是否可以在打字稿中将 * 导出为 foo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42728140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to export * as foo in typescript
提问by Lucas
I can:
我能:
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;
Any ideas?
有任何想法吗?
--- UPDATE ---
- - 更新 - -
What are you trying to achieve?
你想达到什么目的?
Basically, I am working on implementing an ngrx/store
backend for my app. I want to organize my code like this:
基本上,我正在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
And I want to use my index.ts
files to chain up all the exports from each subset (common paradigm).
我想使用我的index.ts
文件来链接每个子集(通用范例)的所有导出。
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-exportedas xxx
. This would allow me to:
但是,我想保留命名空间。每个 myxxx-reducer.ts
和xxx-actions.ts
files 都有相同名称的导出 ( 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());
}
Instead of the more verbose:
而不是更冗长:
import * as viewer from './core/viewer';
import * as viewerData from './core/viewer-data';
...
Thats the gist anyway...
无论如何,这就是要点......
回答by Shaun Luttin
Is it possible to export * as foo in typescript
是否可以在打字稿中将 * 导出为 foo
Nope. You can, however, use a two step process:
不。但是,您可以使用两步过程:
src/core/index.ts
src/核心/index.ts
import * as Foo from "./foo";
import * as Bar from "./bar";
export {
Foo,
Bar,
}
src/index.ts
源代码/索引.ts
import { Foo, Bar } from "./core";
function FooBarBazBop() {
Foo.Baz;
Foo.Bop;
Bar.Baz;
Bar.Bop;
}
src/core/foo/index.ts andsrc/core/bar/index.ts
src/core/foo/index.ts和src/core/bar/index.ts
export * from "./baz";
export * from "./bop";
src/core/foo/baz.ts andsrc/core/bar/baz.ts
src/core/foo/baz.ts和src/core/bar/baz.ts
export class Baz {
}
src/core/foo/bop.ts andsrc/core/bar/bop.ts
src/core/foo/bop.ts和src/core/bar/bop.ts
export class Bop {
}
See also: https://www.typescriptlang.org/docs/handbook/modules.html
另见:https: //www.typescriptlang.org/docs/handbook/modules.html
回答by Przemek Struciński
Since TypeScript 3.8has been released you are able to add alias for your exports.
由于TypeScript 3.8已发布,您可以为导出添加别名。
Example:
例子:
export * as utilities from "./utilities.js";
Ref: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html
参考:https: //www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html