可以将*作为foo导出到打字机中 [英] Is it possible to export * as foo in typescript

查看:187
本文介绍了可以将*作为foo导出到打字机中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以:

import * as foo from './foo'

但似乎不能同样导出:

export * as foo from './foo'

这似乎不起作用...:

This doesn't seem to work either...:

import * as fooImport from './foo';
export const foo = fooImport;

任何想法?

---更新---


你想实现什么?

What are you trying to achieve?

基本上,我正在为我的应用程序实现一个 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 操作,...)所以正常链接将导致名称冲突。我想要做的是允许从 xxx-actions xxx-reducer 的所有出口为重新导出 xxx 。这将允许我:从'./core'中导入{/ p>

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...

推荐答案


是否有可能导出*作为foo在typescript

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆