gpt4 book ai didi

javascript - 从 Typescript 中的模块导出所有定义的速记

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:06 24 4
gpt4 key购买 nike

我想知道是否有导出模块中所有定义的速记。文档中没有建议这样做的方法。我正在寻找类似 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

最佳答案

查看此答案 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'; 

关于javascript - 从 Typescript 中的模块导出所有定义的速记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45649260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com