【问题标题】:Shorthand to export all definitions from a module in Typescript从 Typescript 中的模块导出所有定义的简写
【发布时间】:2018-01-20 19:13:54
【问题描述】:

我想知道是否有导出模块中所有定义的简写。文档中没有建议这样做。我正在寻找类似export* 的东西来导出模块中的所有定义。

/* this is one way according to docs*/
//module1 
const foo = () => {console.log('function foo')}
const bar = () => {console.log('function bar')}
export {foo, bar} 

// module2
import * as m1 from './module1'
m1.foo() 
m1.bar()

/* this is another way according to docs*/
// module1
export const foo = () => {console.log('function foo')}
export const bar = () => {console.log('function bar')}

// module2
import * as m1 from './module1'
m1.foo() 
m1.bar()

/* looking for something like this */
// module1
const foo = () => {console.log('function foo')}
const bar = () => {console.log('function bar')}
export * // looking for something like this

// module2
import * as m1 from './module1'
m1.foo() //doesn't work 
m1.bar() // doesn't work

【问题讨论】:

  • ECMAScript 不支持这个
  • Typescript,无法在单个表达式中导出所有属性*

标签: javascript node.js typescript typescript2.0


【解决方案1】:

看到这个答案https://stackoverflow.com/a/30714301/977206

// export the default export of a legacy (`export =`) module
export import MessageBase = require('./message-base');

// export the default export of a modern (`export default`) module
export { default as MessageBase } from './message-base';

这也应该有效,也许只适用于某些类型的模块?不确定

export * from 'foo'; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2017-04-03
    • 2017-09-29
    • 2020-12-14
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多