如何从打字稿中的模块中重新导出所有导入?

时间:2018-07-13 21:19:50

标签: typescript

我正在将JS代码库迁移到TS。在JS中,我们有一些index.js文件,这些文件会重新导出模块中的所有导入:

export * as users from './users';

等效的TS是什么?

4 个答案:

答案 0 :(得分:2)

我还没有找到一种方法可以单行执行,但是一位同事指出这种方法可以工作:

import * as users from './users';

export {
  users,
};

答案 1 :(得分:1)

现在可以!

Since TypeScript 3.8,您现在可以将所有导入内容“以x格式”导出为

export * as users from "./users";
export * as another from "./another";
// usage: import { users } from './reexport'

作为参考,您已经可以重新导出

export * from "./users"; // everything from users
export * from "./another"; // everything from another
export { a, b } from "./third"; // pick what to export
// usage: import * as all from './reexport' or import { userProp, a, b } from './reexport'

据我所知,仍然无法将导入重新导出为默认导出。这是次佳的选择:

import * as users from './users'
export default users
// usage: import users from './reexport'

文档:
·Re-export all
·Re-export as named object

答案 2 :(得分:0)

@Patrick Finnigan的答案并不完全正确。 您将必须使用users.XXX来访问符号。

似乎没有简单的方法可以导出所有符号。

答案 3 :(得分:0)

另一种方法是如何从One模块中的Second模块的export * from './one'模块重新导出所有命名的导出(而不是默认导出),请使用one.ts,请参见示例:

// Will be re-exported in Two.ts export interface One { one: string; } // Will be re-exported in Two.ts export const abc = 'abc'; const magicNumber = 123; // Will NOT be re-exported in Two.ts export default magicNumber; 中,我们有

two.ts

import * as fromOne from './one' export interface Two { two: number; one: fromOne.One } 中,我们有

import * as fromTwo from './two'

const mNumber = fromTwo.magicNumber; // this will fail, default export is not available

const objectFromOneAndTwo: fromTwo.Two = {
    two: 456,
    one: {
        one: fromTwo.abc
    }
}

最后我们有了index.ts,可以在其中进行

return $this->createQueryBuilder('aff')
        ->select(     "DATE_FORMAT(aff.dateFacture,'%Y-%m')AS anneeMois")
        ->addSelect(  "SUM(aff.factureTtc)AS total")